@@ -154,8 +154,8 @@ def get_day_of_week_capitalized():
154154
155155def get_sunset_sunrise ():
156156 data = download_forecast (type = "daily" )
157- sunrise = data ["DailyForecasts" ][ 0 ][ "Sun" ][ "Rise" ][11 :16 ]
158- sunset = data ["DailyForecasts" ][ 0 ][ "Sun" ][ "Set" ][11 :16 ]
157+ sunrise = data ['daily' ][ 'sunrise' ][ 0 ][11 :16 ]
158+ sunset = data ['daily' ][ 'sunset' ][ 0 ][11 :16 ]
159159
160160 return sunrise , sunset
161161
@@ -202,49 +202,46 @@ def download_forecast(type='daily'):
202202 if not Path ('data/forecast_daily.json' ).exists ():
203203 print (f"{ datetime .today ()} : forecast_daily.json doesn't exist, downloading new one" )
204204 with urllib .request .urlopen (
205- "http://dataservice.accuweather.com/forecasts/v1/daily/1day/275174?apikey"
206- "=GZcekJNnnT8F1qo8VJteym6lRa54mH2b&language=pl-pl&details=true&metric=true" ) as url :
205+ "https://api.open-meteo.com/v1/forecast?latitude=54.3523&longitude=18.6491&daily=sunrise,sunset,uv_index_max&hourly=temperature_2m,rain,wind_speed_10m,wind_direction_10m,relative_humidity_2m,weather_code,apparent_temperature,wind_gusts_10m&timezone=auto&forecast_days=1" ) as url :
207206 data = json .loads (url .read ())
208207 with open ("data/forecast_daily.json" , "w" ) as new_forecast :
209208 json .dump (data , new_forecast )
210209
211210 with open ("data/forecast_daily.json" ) as file :
212211 data = json .load (file )
213- date_of_download = data ["DailyForecasts" ][ 0 ][ "Date" ][:10 ]
212+ date_of_download = data ['hourly' ][ 'time' ][ 0 ][:10 ]
214213 todays_date = f"{ datetime .today ()} " [:10 ]
215214
216215 if date_of_download != todays_date :
217216 # forecast data is outdated
218217 print ("Downloading new file: forecast_daily.json" )
219218
220219 with urllib .request .urlopen (
221- "http://dataservice.accuweather.com/forecasts/v1/daily/1day/275174?apikey"
222- "=GZcekJNnnT8F1qo8VJteym6lRa54mH2b&language=pl-pl&details=true&metric=true" ) as url :
220+ "https://api.open-meteo.com/v1/forecast?latitude=54.3523&longitude=18.6491&daily=sunrise,sunset,uv_index_max&hourly=temperature_2m,rain,wind_speed_10m,wind_direction_10m,relative_humidity_2m,weather_code,apparent_temperature,wind_gusts_10m&timezone=auto&forecast_days=1" ) as url :
223221 data = json .loads (url .read ())
224222 with open ("data/forecast_daily.json" , "w" ) as new_forecast :
225223 json .dump (data , new_forecast )
226224
227225 return data
228226 if type == 'hourly' :
229227 if not Path ('data/forecast_12_hours.json' ).exists ():
230- with urllib .request .urlopen ("http://dataservice.accuweather.com/forecasts/v1/hourly/12hour/275174?apikey"
231- "=GZcekJNnnT8F1qo8VJteym6lRa54mH2b&language=pl-pl&details=true&metric=true" ) \
228+ with urllib .request .urlopen ("https://api.open-meteo.com/v1/forecast?latitude=54.3523&longitude=18.6491&daily=sunrise,sunset,uv_index_max&hourly=temperature_2m,rain,wind_speed_10m,wind_direction_10m,relative_humidity_2m,weather_code,apparent_temperature,wind_gusts_10m&timezone=auto&forecast_days=1" ) \
232229 as url :
233230 data = json .loads (url .read ())
234231 with open ("data/forecast_12_hours.json" , "w" ) as new_forecast :
235232 json .dump (data , new_forecast )
236233
237234 with open ("data/forecast_12_hours.json" ) as file :
238235 data = json .load (file )
239- date_of_download = data [0 ]["DateTime" ][:10 ]
236+
237+ date_of_download = data ['hourly' ]['time' ][0 ][:10 ]
240238 todays_date = f"{ datetime .today ()} " [:10 ]
241239
242240 if date_of_download != todays_date :
243241 # forecast data is outdated
244242 print ("Downloading new file: forecast_12_hours.json" )
245243
246- with urllib .request .urlopen ("http://dataservice.accuweather.com/forecasts/v1/hourly/12hour/275174?apikey"
247- "=GZcekJNnnT8F1qo8VJteym6lRa54mH2b&language=pl-pl&details=true&metric=true" ) \
244+ with urllib .request .urlopen ("https://api.open-meteo.com/v1/forecast?latitude=54.3523&longitude=18.6491&daily=sunrise,sunset,uv_index_max&hourly=temperature_2m,rain,wind_speed_10m,wind_direction_10m,relative_humidity_2m,weather_code,apparent_temperature,wind_gusts_10m&timezone=auto&forecast_days=1" ) \
248245 as url :
249246 data = json .loads (url .read ())
250247 with open ("data/forecast_12_hours.json" , "w" ) as new_forecast :
@@ -258,37 +255,30 @@ def download_forecast(type='daily'):
258255def get_daily_forecast ():
259256 data = download_forecast (type = 'daily' )
260257
261- headline = data ["Headline" ]["Text" ]
262- minTemp = round (data ["DailyForecasts" ][0 ]["Temperature" ]["Minimum" ]["Value" ])
263- maxTemp = round (data ["DailyForecasts" ][0 ]["Temperature" ]["Maximum" ]["Value" ])
264- info = data ["DailyForecasts" ][0 ]["Day" ]["LongPhrase" ]
265- rain = data ["DailyForecasts" ][0 ]["Day" ]["RainProbability" ]
266- air = data ["DailyForecasts" ][0 ]["AirAndPollen" ][0 ]["Category" ]
267- uvindex = data ["DailyForecasts" ][0 ]["AirAndPollen" ][5 ]["Value" ]
268- uvdanger = data ["DailyForecasts" ][0 ]["AirAndPollen" ][5 ]["Category" ]
269- try :
270- moon = moon_phases [data ["DailyForecasts" ][0 ]["Moon" ]["Phase" ]]
271- except KeyError :
272- moon = data ["DailyForecasts" ][0 ]["Moon" ]["Phase" ]
258+ # headline = data["Headline"]["Text"]
259+ # minTemp = round(data["DailyForecasts"][0]["Temperature"]["Minimum"]["Value"])
260+ # maxTemp = round(data["DailyForecasts"][0]["Temperature"]["Maximum"]["Value"])
261+ # info = data["DailyForecasts"][0]["Day"]["LongPhrase"]
262+ # rain = data["DailyForecasts"][0]["Day"]["RainProbability"]
263+ # air = data["DailyForecasts"][0]["AirAndPollen"][0]["Category"]
264+ # uvindex = data["DailyForecasts"][0]["AirAndPollen"][5]["Value"]
265+ # uvdanger = data["DailyForecasts"][0]["AirAndPollen"][5]["Category"]
266+ # try:
267+ # moon = moon_phases[data["DailyForecasts"][0]["Moon"]["Phase"]]
268+ # except KeyError:
269+ # moon = data["DailyForecasts"][0]["Moon"]["Phase"]
273270
274271 return headline , minTemp , maxTemp , info , rain , air , uvindex , uvdanger , moon
275272
276273
277274def get_hourly_forecast ():
278275 data = download_forecast (type = 'hourly' )
279276
280- hours = []
281- temperatures = []
282- icons = []
283- wind_speeds = []
284- wind_directions = []
285-
286- for one_hour in data :
287- hours .append (one_hour ["DateTime" ][11 :13 ])
288- temperatures .append (round (one_hour ["Temperature" ]["Value" ]))
289- icons .append (one_hour ["WeatherIcon" ])
290- wind_speeds .append (one_hour ["Wind" ]["Speed" ]["Value" ])
291- wind_directions .append (one_hour ["Wind" ]["Direction" ]["Degrees" ])
277+ hours = [h [11 :13 ] for h in data ['hourly' ]['time' ]]
278+ temperatures = [t for t in data ['hourly' ]['temperature_2m' ]]
279+ icons = [i for i in data ['hourly' ]['weather_code' ]]
280+ wind_speeds = [w for w in data ['hourly' ]['wind_speed_10m' ]]
281+ wind_directions = [w for w in data ['hourly' ]['wind_direction_10m' ]]
292282
293283 return hours , temperatures , icons , wind_speeds , wind_directions
294284
@@ -299,24 +289,15 @@ def get_advanced_hourly_forecast():
299289 # hours, temperature, temperature_feel, rainfall, humidity, wind_speed, wind_gust
300290
301291 forecast = {
302- 'hours' : [],
303- 'temperature' : [],
304- 'temperature_feel' : [],
305- 'rainfall' : [],
306- 'humidity' : [],
307- 'wind_speed' : [],
308- 'wind_gust' : [],
292+ 'hours' : [h [ 11 : 13 ] for h in data [ 'hourly' ][ 'time' ] ],
293+ 'temperature' : [t for t in data [ 'hourly' ][ 'temperature_2m' ] ],
294+ 'temperature_feel' : [t for t in data [ 'hourly' ][ 'apparent_temperature' ] ],
295+ 'rainfall' : [r for r in data [ 'hourly' ][ 'rain' ] ],
296+ 'humidity' : [h for h in data [ 'hourly' ][ 'relative_humidity_2m' ] ],
297+ 'wind_speed' : [w for w in data [ 'hourly' ][ 'wind_speed_10m' ] ],
298+ 'wind_gust' : [w for w in data [ 'hourly' ][ 'wind_gusts_10m' ] ],
309299 }
310300
311- for sample in data :
312- forecast ['hours' ].append (sample ['DateTime' ][11 :13 ])
313- forecast ['temperature' ].append (sample ['Temperature' ]['Value' ])
314- forecast ['temperature_feel' ].append (sample ['RealFeelTemperature' ]['Value' ])
315- forecast ['rainfall' ].append (sample ['TotalLiquid' ]['Value' ])
316- forecast ['humidity' ].append (sample ['RelativeHumidity' ])
317- forecast ['wind_speed' ].append (sample ['Wind' ]['Speed' ]['Value' ])
318- forecast ['wind_gust' ].append (sample ['WindGust' ]['Speed' ]['Value' ])
319-
320301 return forecast
321302
322303
0 commit comments