-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.py
63 lines (45 loc) · 1.7 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
63
import json
import pprint
from datetime import datetime
import requests
import os
def get_key(filename="darkskyapikey.txt"):
# first line should be the api key
return open(filename, "r").read()
def get_data(day):
key = get_key()
# coordinates for BCA
latitude = 40.9014937
longitude = -74.034276
r = requests.get("https://api.darksky.net/forecast/%s/%f,%f,%s" % (key, latitude, longitude, str(day).replace(" ", "T")))
r_obj = json.loads(r.text)
return r_obj
def dealWithTheData(response):
output = {}
stuffs = ["cloudCover", "humidity", "precipIntensity", "precipProbability", "precipType", "visibility", "windSpeed"]
trimmedResponse = response["daily"]["data"][0]
for stuff in stuffs:
if stuff in trimmedResponse:
if stuff == "precipType":
output[stuff] = {"": 0, "rain": 1, "snow": 2, "sleet": 3}[trimmedResponse[stuff]]
else:
output[stuff] = trimmedResponse[stuff]
else:
output[stuff] = 0
output["sunsetTime"] = str(datetime.fromtimestamp(response['daily']['data'][0]["sunsetTime"]))[11:].replace(":", "")
return output
def addToFile(data, day):
f = open("weather.json", "a")
f.seek(f.tell() - 1, os.SEEK_SET)
f.truncate()
# this assumes that weather.json already contains something
# if it is empty, add "{}" to it, run the script and then remove the leading comma
toWrite = ", \"" + str(day)[0:10] + "\": " + str(data) + "}"
f.write(toWrite.replace("'", "\""))
f.close()
def main(date):
response = get_data(date)
data = dealWithTheData(response)
addToFile(data, date)
if __name__ == "__main__":
main(datetime(2007, 12, 4))