This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather.py
62 lines (47 loc) · 2.74 KB
/
weather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import requests
from datetime import datetime, timedelta
# TODO: Find rain probability
def time_slot_datapoint(openweather_response, day_to_predict, starting_timings):
for datapoint in openweather_response['list']:
if datetime.fromtimestamp(datapoint['dt']).date() == day_to_predict and datetime.fromtimestamp(
datapoint['dt']).hour in starting_timings:
return datapoint
def time_datapoint(openweather_response, day_to_predict, time):
# The best (3 hr) window of time to define our targeet timing will start at t-1, t or t+1
return time_slot_datapoint(openweather_response, day_to_predict, [time-1, time, time+1])
def extract_timeslot_weather_data(datapoint):
return {'dt': datetime.fromtimestamp(datapoint['dt']),
'min_temperature': datapoint['main']['temp_min'],
'max_temperature': datapoint['main']['temp_max'],
'humidity': datapoint['main']['humidity'],
'wind': datapoint['wind']['speed'],
'meteorology': datapoint["weather"][0]['description'],
# TODO: Look to use "Main" rather than id
'meteorology_code': datapoint["weather"][0]['id']}
# It returns weather forecast data for today (if ran before 7am) or for tomorrow otherwise
# List of weather conditions: https://openweathermap.org/weather-conditions
def weather_data(config, morning_time=7, afternoon_time=18):
weather_api_key = config['DEFAULT']['open_weather_key']
# Sunrise / Sunset data (note: these times are computed for the day when the call is made)
base_url = 'http://api.openweathermap.org/data/2.5/weather'
city = 'London,uk'
request = base_url + '?q={}&appid={}&units=metric'.format(city, weather_api_key)
data = requests.get(request).json()
day_weather = {'sunrise': datetime.fromtimestamp(data['sys']['sunrise']),
'sunset': datetime.fromtimestamp(data['sys']['sunset'])}
# Forecast 3-hour windows data
base_url = 'http://api.openweathermap.org/data/2.5/forecast'
city = 'London,uk'
request = base_url + '?q={}&appid={}&units=metric'.format(city, weather_api_key)
data = requests.get(request).json()
now = datetime.now()
# If we run this after the morning time, we will predict for tomorrow
if now.hour > morning_time:
day_to_predict = (now + timedelta(days=1)).date()
else:
day_to_predict = now.date()
weather = {'morning': {'sunrise': day_weather['sunrise']},
'afternoon': {'sunset': day_weather['sunset']}}
weather['morning'].update(extract_timeslot_weather_data(time_datapoint(data, day_to_predict, morning_time)))
weather['afternoon'].update(extract_timeslot_weather_data(time_datapoint(data, day_to_predict, afternoon_time)))
return weather