-
Notifications
You must be signed in to change notification settings - Fork 0
/
meteostat.py
31 lines (23 loc) · 1.12 KB
/
meteostat.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
import requests
# https://api.meteostat.net/
#runtime cache for stations
station_cache = {}
def get_weather_for_date_at_location(date, latitude, longitude, api_key):
station_id = None
key = (latitude, longitude)
if key in station_cache:
station_id = station_cache[key]
else :
station_api_call = 'https://api.meteostat.net/v1/stations/nearby?lat={latitude}&lon={longitude}&limit=1&key={api_key}'
response = requests.get(station_api_call.format(latitude=latitude, longitude=longitude, api_key=api_key))
if response.status_code == 200:
content=response.json()['data'][0]
station_id = content['id']
station_cache[(latitude,longitude)] = station_id
if station_id is None:
return None
weather_api_call = 'https://api.meteostat.net/v1/history/daily?station={station}&start={start_date}&end={end_date}&key={api_key}'
response = requests.get(weather_api_call.format(station=station_id, start_date=date, end_date=date, api_key=api_key))
if response.status_code == 200:
return response.json()['data'][0]
return None