-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather.py
65 lines (54 loc) · 2.07 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
64
65
import wap
from datetime import date
from datetime import timedelta as td
import pickle
months=["January","February","March","April","May","June","July","August","September","October","November","December"]
dayWeatherList=list()
__APPID__="X8JJ7X-8R4Q7GJJ42"
class DayWeather():
def __init__(self,date='',temp='',conditions='',humidity='',wind=''):
self.date=date
self.temp=temp
self.conditions=conditions
self.humidity=humidity
self.wind=wind
def collect(startTime=date(2012,10,11),stopTime=date(2013,02,12)):
delta=stopTime-startTime
dateList=list()
for i in range(delta.days+1):
day=startTime+td(days=i)
dateList.append(day)
client=wap.WolframAlphaEngine(__APPID__,'http://api.wolframalpha.com/v1/query.jsp')
for day in dateList:
inputQuery=str("weather in stockholm on "+months[day.month-1]+" "+str(day.day)+", "+str(day.year))
print "Using query: "+inputQuery
q=wap.WolframAlphaQuery(inputQuery,__APPID__)
q.ScanTimeout = '3.0'
q.Async = False
q.ToURL()
result = client.PerformQuery(q.Query)
qresult = wap.WolframAlphaQueryResult(result)
#print qresult
pod=qresult.Pods()[1]
#print pod
np = wap.Pod(pod)
subpods=np.Subpods()
data=subpods[0][1][1]
temp=data[data.find("temperature | ")+14:data.find("conditions | ")]
conditions=data[data.find("conditions | ")+13:data.find("relative humidity | ")]
humidity=data[data.find("relative humidity | ")+20:data.find("wind speed | ")]
wind=data[data.find("wind speed | ")+13:]
dayWeatherList.append(DayWeather(day.__str__(),temp,conditions,humidity,wind))
try:
newfile=open("Weatherdata2.pkl","wb")
pickle.dump(dayWeatherList,newfile)
newfile.close()
except IOError:
for x in dayWeatherList:
print x.date
print x.temp
print x.conditions
print x.humidity
print x.wind
if __name__=="__main__":
collect()