Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import datetime
- import os
- def nome_fase_lunare(moon_phase_value):
- if moon_phase_value == 0 or moon_phase_value == 1:
- return 'luna nuova'
- elif moon_phase_value == 0.25:
- return 'primo quarto'
- elif moon_phase_value == 0.5:
- return 'luna piena'
- elif moon_phase_value == 0.75:
- return 'ultimo quarto'
- elif 0 < moon_phase_value < 0.25:
- return 'crescente'
- elif 0.25 < moon_phase_value < 0.5:
- return 'gibbosa crescente'
- elif 0.5 < moon_phase_value < 0.75:
- return 'gibbosa calante'
- elif 0.75 < moon_phase_value < 1:
- return 'calante'
- else:
- return 'Errore: valore non valido'
- def get_weather_data(city, state, api_key):
- url = f"https://api.openweathermap.org/geo/1.0/direct?q={city},{state}&appid={api_key}"
- response = requests.get(url)
- data = response.json()
- lat, lon = data[0]['lat'], data[0]['lon']
- url = f"https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&appid={api_key}&units=metric"
- response = requests.get(url)
- data = response.json()
- sunrise = datetime.datetime.fromtimestamp(data['daily'][0]['sunrise'])
- sunset = datetime.datetime.fromtimestamp(data['daily'][0]['sunset'])
- temperature = data['daily'][1]['temp']
- moon_phase = data['daily'][0]['moon_phase']
- weather = data['daily'][0]['weather'][0]['description']
- return sunrise, sunset, temperature, moon_phase, weather
- api_key = os.getenv('WEATHER_API_KEY')
- city = input("Scrivi la tua cittò: ")
- state = input("Scrivi il tuo Stato(ISO 3166. es. ITA): ")
- sunrise, sunset, temperature, moon_phase, weather = get_weather_data(city, state, api_key)
- print(f"Fase lunare: {nome_fase_lunare(moon_phase)}")
- temperature_str = ', '.join(f"{k}: {v}°C" for k, v in temperature.items())
- print(f"Temperature: {temperature_str}")
- print(f"Alba: {sunrise: %D %H:%M:%S}")
- print(f"Tramonto: {sunset: %D %H:%M:%S}")
- print(f"Tempo: {weather}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement