33import time
44import urllib .request
55from datetime import date , datetime , timedelta
6+ from pathlib import Path
67
78import requests
89from bs4 import BeautifulSoup
@@ -198,6 +199,15 @@ def get_utc_difference():
198199
199200def download_forecast (type = 'daily' ):
200201 if type == 'daily' :
202+ if not Path ('data/forecast_daily.json' ).exists ():
203+ print (f"{ datetime .today ()} : forecast_daily.json doesn't exist, downloading new one" )
204+ 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 :
207+ data = json .loads (url .read ())
208+ with open ("data/forecast_daily.json" , "w" ) as new_forecast :
209+ json .dump (data , new_forecast )
210+
201211 with open ("data/forecast_daily.json" ) as file :
202212 data = json .load (file )
203213 date_of_download = data ["DailyForecasts" ][0 ]["Date" ][:10 ]
@@ -216,6 +226,14 @@ def download_forecast(type='daily'):
216226
217227 return data
218228 if type == 'hourly' :
229+ 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" ) \
232+ as url :
233+ data = json .loads (url .read ())
234+ with open ("data/forecast_12_hours.json" , "w" ) as new_forecast :
235+ json .dump (data , new_forecast )
236+
219237 with open ("data/forecast_12_hours.json" ) as file :
220238 data = json .load (file )
221239 date_of_download = data [0 ]["DateTime" ][:10 ]
0 commit comments