diff options
Diffstat (limited to 'library.py')
-rw-r--r-- | library.py | 68 |
1 files changed, 58 insertions, 10 deletions
@@ -23,12 +23,14 @@ def get_grid_data(latitude, longitude): | |||
23 | 23 | ||
24 | 24 | ||
25 | def get_raw_data(grid_id, grid_x, grid_y): | 25 | def get_raw_data(grid_id, grid_x, grid_y): |
26 | raw_data = json.loads(requests.get(f"https://api.weather.gov/gridpoints/{grid_id}/{grid_x},{grid_y}").text) | 26 | headers = {"User-Agent": "pywttr 0.1"} |
27 | raw_data = json.loads(requests.get(f"https://api.weather.gov/gridpoints/{grid_id}/{grid_x},{grid_y}", headers=headers).text) | ||
27 | return raw_data | 28 | return raw_data |
28 | 29 | ||
29 | 30 | ||
30 | def get_raw_forecast(grid_id, grid_x, grid_y): | 31 | def get_raw_forecast(grid_id, grid_x, grid_y): |
31 | raw_data = json.loads(requests.get(f"https://api.weather.gov/gridpoints/{grid_id}/{grid_x},{grid_y}/forecast", user_agent="my-test-app").text) | 32 | headers = {"User-Agent": "pywttr 0.1"} |
33 | raw_data = json.loads(requests.get(f"https://api.weather.gov/gridpoints/{grid_id}/{grid_x},{grid_y}/forecast", headers=headers).text) | ||
32 | return raw_data | 34 | return raw_data |
33 | 35 | ||
34 | 36 | ||
@@ -136,11 +138,22 @@ def get_daily_lows(raw_data): | |||
136 | time_str, duration_str = low["validTime"].split('/') | 138 | time_str, duration_str = low["validTime"].split('/') |
137 | time = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S%z") | 139 | time = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S%z") |
138 | duration = parse_duration(duration_str) | 140 | duration = parse_duration(duration_str) |
139 | daily_lows.append({"time":time,"duration":duration,"low_celc":low_celc,"low_fahr":low_far}) | 141 | daily_lows.append({"time":time,"duration":duration,"low_celc":low_celc,"low_fahr":low_fahr}) |
140 | 142 | ||
141 | return daily_lows | 143 | return daily_lows |
142 | 144 | ||
143 | 145 | ||
146 | def get_daily_forecast(raw_data): | ||
147 | daily_forecast_raw = raw_data["properties"]["periods"] | ||
148 | |||
149 | daily_forecast = [] | ||
150 | for point in daily_forecast_raw: | ||
151 | time_str = point["startTime"] | ||
152 | time = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S%z") | ||
153 | daily_forecast.append({"time":time, "short_forecast":point["shortForecast"], "detailed_forecast":point["detailedForecast"]}) | ||
154 | return make_current(set_timezone(daily_forecast)) | ||
155 | |||
156 | |||
144 | def get_temperature(raw_data): | 157 | def get_temperature(raw_data): |
145 | raw_values = raw_data["properties"]["temperature"]["values"] | 158 | raw_values = raw_data["properties"]["temperature"]["values"] |
146 | ret = [] | 159 | ret = [] |
@@ -334,11 +347,9 @@ def get_hourly_data(raw_data, end_time): | |||
334 | precip_amount = get_precip_amount(raw_data) | 347 | precip_amount = get_precip_amount(raw_data) |
335 | wind_speed = get_wind_speed(raw_data) | 348 | wind_speed = get_wind_speed(raw_data) |
336 | wind_direction = get_wind_direction(raw_data) | 349 | wind_direction = get_wind_direction(raw_data) |
337 | 350 | ret_list = [] | |
338 | ret = [] | ||
339 | i = 0 | 351 | i = 0 |
340 | while i < len(temps) and temps[i]["time"] < end_time: | 352 | while i < len(temps) and temps[i]["time"] < end_time: |
341 | |||
342 | if i >= len(temps): | 353 | if i >= len(temps): |
343 | temps.append({"value":"N/A"}) | 354 | temps.append({"value":"N/A"}) |
344 | if i >= len(humidity): | 355 | if i >= len(humidity): |
@@ -352,8 +363,6 @@ def get_hourly_data(raw_data, end_time): | |||
352 | if i >= len(wind_direction): | 363 | if i >= len(wind_direction): |
353 | wind_direction.append({"value":"N/A"}) | 364 | wind_direction.append({"value":"N/A"}) |
354 | 365 | ||
355 | |||
356 | |||
357 | val_dict = { "time": temps[i]["time"], | 366 | val_dict = { "time": temps[i]["time"], |
358 | "temp": temps[i]["value_fahr"], | 367 | "temp": temps[i]["value_fahr"], |
359 | "humidity": humidity[i]["value"], | 368 | "humidity": humidity[i]["value"], |
@@ -361,7 +370,46 @@ def get_hourly_data(raw_data, end_time): | |||
361 | "precip_amount": precip_amount[i]["value"], | 370 | "precip_amount": precip_amount[i]["value"], |
362 | "wind_speed": wind_speed[i]["value"], | 371 | "wind_speed": wind_speed[i]["value"], |
363 | "wind_direction": wind_direction[i]["value"] } | 372 | "wind_direction": wind_direction[i]["value"] } |
364 | ret.append(val_dict) | 373 | ret_list.append(val_dict) |
365 | i+=1 | 374 | i+=1 |
366 | 375 | ||
367 | return ret | 376 | return ret_list |
377 | |||
378 | |||
379 | def get_daily_data(raw_data, raw_forecast, end_time): | ||
380 | daily_highs = get_daily_highs(raw_data) | ||
381 | daily_lows = get_daily_lows(raw_data) | ||
382 | daily_forecasts = get_daily_forecast(raw_forecast) | ||
383 | ret_list = [] | ||
384 | i = 0 | ||
385 | while i < len(daily_highs) and daily_highs[i]["time"] < end_time: | ||
386 | val_dict = { "time": daily_highs[i]["time"], | ||
387 | "high": daily_highs[i]["high_fahr"], | ||
388 | "low": daily_lows[i]["low_fahr"], | ||
389 | "short_forecast_am": daily_forecasts[i]["short_forecast"], | ||
390 | "detailed_forecast_am": daily_forecasts[i]["detailed_forecast"], | ||
391 | "short_forecast_pm": daily_forecasts[i+1]["short_forecast"], | ||
392 | "detailed_forecast_pm": daily_forecasts[i+1]["detailed_forecast"] } | ||
393 | ret_list.append(val_dict) | ||
394 | i+=1 | ||
395 | return ret_list | ||
396 | |||
397 | |||
398 | def hourly_forecast(raw_data, days): | ||
399 | init_time = get_current_rounded_time() | ||
400 | if days > 0: | ||
401 | delta = datetime.timedelta(days=days) | ||
402 | else: | ||
403 | delta = datetime.timedelta(hours=(24-init_time.hour)) | ||
404 | end_time = init_time + delta | ||
405 | return get_hourly_data(raw_data, end_time) | ||
406 | |||
407 | |||
408 | def daily_forecast(raw_data, raw_forecast, days): | ||
409 | init_time = get_current_rounded_time() | ||
410 | if days > 0: | ||
411 | delta = datetime.timedelta(days=days) | ||
412 | else: | ||
413 | delta = datetime.timedelta(days=5) | ||
414 | end_time = init_time + delta | ||
415 | return get_daily_data(raw_data, raw_forecast, end_time) | ||