Skip to content

Commit 9904a3b

Browse files
authored
Merge pull request #34 from Mixss/data-files-quickfix
logic: download forecast data if file does not exist
2 parents d006623 + efdb86d commit 9904a3b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

logic/logic.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
import urllib.request
55
from datetime import date, datetime, timedelta
6+
from pathlib import Path
67

78
import requests
89
from bs4 import BeautifulSoup
@@ -198,6 +199,15 @@ def get_utc_difference():
198199

199200
def 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

Comments
 (0)