-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather.py
61 lines (47 loc) · 2.09 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
"""
This module is a class wrapper for the OpenWeatherMap API and time-based affordances.
"""
from __future__ import print_function
from __future__ import absolute_import
import requests
class Weather(object):
"""
Manages queries to the OpenWeatherMap API (https://openweathermap.org/api) for weather affordances and computes
additional time-based affordances.
Attributes:
api_key (string): API key to authenticate requests with.
"""
def __init__(self, api_key):
"""
Returns a Weather object with class variables initialized.
:param api_key: string for OpenWeatherMap API Key.
"""
# setup keys
self.api_key = api_key
def get_weather_at_location(self, lat, lng):
"""
Makes a request to the weather API for the weather at the current location.
:param lat: float latitude to center request around.
:param lng: float longitude to center request around.
:return: JSON response as dict from weather API for current weather at current location
"""
# make request
url = 'http://api.openweathermap.org/data/2.5/weather?lat={latitude}&lon={longitude}&appid={api_key}'
resp = requests.get(url.format(latitude=str(lat), longitude=str(lng), api_key=self.api_key))
# return if request is valid
if resp.status_code == requests.codes.ok:
return resp.json()
return None
def get_forecast_at_location(self, lat, lng):
"""
Makes a request to the weather API for the forecast at the current location.
:param lat: latitude, as a float
:param lng: longitude, as a float
:return: JSON response as dict from weather API for current forecast at current location
"""
url = 'http://api.openweathermap.org/data/2.5/forecast?lat={latitude}&lon={longitude}&appid={api_key}'
resp = requests.get(url.format(latitude=str(lat), longitude=str(lng), api_key=self.api_key))
# return if request is valid
if resp.status_code == requests.codes.ok:
return resp.json()
return None