-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_weather_api.py
61 lines (50 loc) · 1.92 KB
/
_weather_api.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
import logging
import json
import os
import requests
from cachetools.func import ttl_cache
CONFIG_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.json")
WEATHER_API_FIELD = "weather_api"
logger = logging.getLogger(__name__)
def create_weather_api(controller, args):
class api:
def __init__(self):
config = json.loads(open(CONFIG_PATH, "r").read())
self.api_key = config["accuweather_api_key"]
self.zipcode = config["zipcode"]
loc_key_url = "http://dataservice.accuweather.com/locations/v1/postalcodes/search?apikey={0}&q={1}".format(
self.api_key, self.zipcode
)
location = requests.get(loc_key_url).json()
self.location_key = location[0]["Key"]
@ttl_cache(maxsize=1, ttl=60 * 60)
def get_current(self):
data = requests.get(
"http://dataservice.accuweather.com/currentconditions/v1/{0}?apikey={1}".format(
self.location_key, self.api_key
)
).json()
return data
@ttl_cache(maxsize=1, ttl=60 * 60)
def get_next_day(self):
data = requests.get(
"http://dataservice.accuweather.com/forecasts/v1/daily/1day/{0}?apikey={1}".format(
self.location_key, self.api_key
)
).json()
return data
@ttl_cache(maxsize=1, ttl=60 * 60)
def get_previous_day(self):
data = requests.get(
"http://dataservice.accuweather.com/currentconditions/v1/{0}/historical/24?details=true&apikey={1}".format(
self.location_key, self.api_key
)
).json()
return data
return api()
weather_api_schema = {
WEATHER_API_FIELD: {
"value": {"type": "weather_api", "default_gen": create_weather_api},
"user_input": False,
},
}