-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherData.py
39 lines (32 loc) · 1.12 KB
/
weatherData.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
import json
import requests
import os
thisDir = os.path.dirname(os.path.realpath(__file__))
configFile = thisDir + "/config.json"
dataFile = thisDir + "/data.txt"
configFileData = {}
def getConfigFileDict():
global configFileData
"""
Attempts to cache the config file, and will fetch it if its not cached.
"""
if (not configFileData):
with open(configFile, "r") as f:
configFileData = json.loads(f.read())
return configFileData
class WeatherData(object):
WEATHER_URL = getConfigFileDict()['weatherUrl']
def __init__(self):
self.desc = "ND"
self.tempF = "ND"
self.updateData()
def updateData(self):
#get weather data
try:
res = requests.get(self.WEATHER_URL)
if res.status_code == 200:
weatherObj = res.json()
self.desc = weatherObj['currently']['summary']
self.tempF = weatherObj['currently']['temperature']
except requests.exceptions.connectionError as e:
print "Couldn't connect to weather API: ({0}) {1}".format(e.errno, e.strerror)