-
Notifications
You must be signed in to change notification settings - Fork 0
/
owm.py
executable file
·96 lines (73 loc) · 2.39 KB
/
owm.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/env python
# -*- coding: utf-8 -*-
def parse_weather(response):
import json
data = {}
resj = ''
for L in response.readlines():
resj = resj + L
res = json.loads(resj)
dt = int(res['dt'])
dttxt = ''
temp = float(res['main']['temp']) - 273.15
weather = res['weather'][0]['main']
data[dt] = { 'date': dttxt, 'temp': temp, 'weather': weather }
return data
def parse_forecast(response):
import json
data = {}
resj = ''
for L in response.readlines():
resj = resj + L
res = json.loads(resj)
for d in res['list']:
dt = int(d['dt'])
dttxt = d['dt_txt']
temp = float(d['main']['temp']) - 273.15
weather = d['weather'][0]['main']
data[dt] = { 'date': dttxt, 'temp': temp, 'weather': weather }
return data
def getLochNess():
import os
import json
import time
import urllib
import urllib2
from datetime import datetime
from datetime import timedelta
cache_file = 'LochNess.cache'
dn = int(time.mktime(datetime.now().timetuple()))
ct = int(os.stat(cache_file).st_mtime) if os.path.isfile(cache_file) else 0
if ct < dn - 600:
# キャッシュが無い OR 古いので、Webからフェッチする
with open('owm.setting','r') as fd:
for L in fd.readlines():
exec(L)
weather_url = "%s/weather?q=Loch Ness,uk&APPID=%s" % (BASE_URL,API_KEY)
forecast_url = "%s/forecast?q=Loch Ness,uk&APPID=%s" % (BASE_URL,API_KEY)
# query
weather_response = urllib2.urlopen(weather_url)
forecast_response = urllib2.urlopen(forecast_url)
weather_data = parse_weather(weather_response)
forecast_data = parse_forecast(forecast_response)
data = {}
for k in weather_data.keys():
data[k] = weather_data[k]
for k in forecast_data.keys():
data[k] = forecast_data[k]
jstr = json.dumps(data)
with open(cache_file,'w') as fd:
fd.write(jstr)
else:
# キャッシュを参照
data = {}
with open(cache_file,'r') as fd:
jstr = ""
for L in fd.readlines():
jstr = jstr + L
data = json.loads(jstr)
return data
if __name__ == '__main__':
data = getLochNess()
for k in sorted(data.keys()):
print data[k]