-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherAPI.py
More file actions
241 lines (211 loc) · 8.34 KB
/
Copy pathWeatherAPI.py
File metadata and controls
241 lines (211 loc) · 8.34 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# -*- coding: UTF-8 -*-
'''
utils.WeatherAPI is a part of the project bangu.
bangu is an open-source project which follows MVC design pattern mainly based on python.
Copyright (C) 2014 - 2017, Vlon Jang(WeChat:wangqingbaidu)
Institute of Computing Technology, Chinese Academy of Sciences, Beijing, China.
The codes are mainly developed by Zhiwei Zhang.
As an open-source project, your can use or modify it as you want.
Contact Info: you can send an email to 564326047@qq.com(Vlon)
or visit my website www.wangqingbaidu.cn
Note: Please keep the above information whenever or wherever the codes are used.
'''
from datetime import datetime
import urllib2
from bs4 import BeautifulSoup
def obj2dict(obj, ept=[]):
"""
summary: Convert object to dict except callable method and private attributes.
"""
memberlist = [m for m in dir(obj)]
_dict = {}
for m in memberlist:
if m[0] != "_" and not callable(getattr(obj, m)) and not m in ept:
# print getattr(obj,m)
_dict[m] = getattr(obj, m)
return _dict
class weather:
"""
---------
Attribute
---------
@__attrs_map: Map from attribute to Chinese.
-------------
Public Method
-------------
@__str__: Convert non-empty attribute to string
"""
__attrs_map = {'humidity': '湿度:',
'temperature': '温度:',
'wind': '风力:',
'condition': '天气:',
'pm25': 'pm25:',
'pm25_desc': '空气质量:',
'tmp_max': '最高气温:',
'tmp_min': '最低气温:'}
humidity = None
temperature = None
wind = None
condition = None
condition_code = None
pm25 = None
pm25_desc = None
tmp_max = None
tmp_min = None
def __init__(self, t=None, h=None, w=None, c=None, cc=None,
p=None, pd=None, tmax=None, tmin=None):
self.temperature = t
self.humidity = h
self.wind = w
self.condition = c
self.condition_code = cc
self.pm25 = p
self.pm25_desc = pd
self.tmp_max = tmax
self.tmp_min = tmin
def __str__(self):
attrs_dict = obj2dict(self, ept=['condition_code'])
attrs_list = ['{key}{value}'.format(key=self.__attrs_map[k],
value=attrs_dict[k].strip().encode('utf8'))
for k in attrs_dict if attrs_dict[k]]
return '\t'.join(sorted(attrs_list))
def getConditionCode(condition=None):
"""
This model is used to compute condition code with the given condition
"""
condition_str = str(condition.encode('utf8'))
if u'晴'.encode('utf8') in condition_str or \
u'云'.encode('utf8') in condition_str:
return 2
elif u'雾'.encode('utf8') in condition_str or \
u'阴'.encode('utf8') in condition_str:
return 1
elif u'雨'.encode('utf8') in condition_str or \
u'雪'.encode('utf8') in condition_str or \
u'霾'.encode('utf8') in condition_str:
return 0
else:
return -1
class WeatherAPI:
"""
---------
Attribute
---------
@url_map: dict. Key is parser type, value is url of this type.
@last_update: datetime. Last update time of this instance.
@suggestion: str. Suggestion of current weather.
@now: weather. Weather info of now.
@forecast: weather list. Weather info of former three days.
@month: weather list. Weather info of this month.
-------------
Public Method
-------------
@refresh:
input: None
output: None
Refresh weather info.
--------------
Private Method
--------------
@__parser_XXX:
input: Block of XXX
output: None
Parser XXX block to weather of weather list
"""
urlmap = {'moji': 'https://tianqi.moji.com/weather/china/beijing/haidian-district'}
# urlmap = {'moji':'https://tianqi.moji.com'}
last_update = None
suggestion = None
now = weather()
forecast = []
month = []
def __init__(self, api_type='moji', debug=False):
self.type = api_type
self.debug = debug
self.api_type = api_type
if self.urlmap.has_key(api_type):
self.refresh()
# self.content = urllib2.urlopen(self.urlmap[self.api_type]).read()
# self.last_update = datetime.now()
# self.__parser_content()
else:
raise Exception("API type not found! Now tianqi.moji.com api supports only.")
def refresh(self):
try:
# Empty all
self.suggestion = None
self.now = weather()
self.forecast = []
self.month = []
self.content = urllib2.urlopen(self.urlmap[self.api_type]).read()
self.last_update = datetime.now()
self.__parser_content()
except Exception, e:
print e
raise Exception('Web connection error.')
def __parser_content(self):
soup = BeautifulSoup(self.content, 'html.parser')
now_block = soup.select('div[class="wrap clearfix wea_info"]')
forecast_block = soup.select('div[class="forecast clearfix"]')
month_block = soup.find_all(attrs={'class': "grid clearfix", 'id': "calendar_grid"})
self.suggestion = soup.select(
'meta[name="description"]')[0]['content'].replace('墨迹天气'.decode('utf8'), 'Bangu')
if self.debug:
print self.suggestion
self.__parser_now_block(now_block[0])
self.__parser_forecast_block(forecast_block[0])
self.__parser_month_block(month_block[0])
def __parser_now_block(self, now_block=None):
s = now_block.select('div[class="left"] div[class="wea_alert clearfix"] ul li a em')[0].string.split()
self.now.pm25 = s[0]
self.now.pm25_desc = s[1]
self.now.temperature = now_block.select(
'div[class="left"] div[class="wea_weather clearfix"] em')[0].string + '℃'.decode('utf8')
self.now.condition = now_block.select(
'div[class="left"] div[class="wea_weather clearfix"] b')[0].string
self.now.condition_code = getConditionCode(self.now.condition)
self.now.humidity = now_block.select(
'div[class="left"] div[class="wea_about clearfix"] span')[0].string.split()[-1]
self.now.wind = now_block.select(
'div[class="left"] div[class="wea_about clearfix"] em')[0].string
if self.debug:
print self.now
def __parser_forecast_block(self, forecast_block=None):
days = forecast_block.find_all(class_="days clearfix")
for d in days:
w = weather()
item = d.select('li')
w.condition = item[1].get_text().lstrip()
w.condition_code = getConditionCode(w.condition)
w.tmp_min = item[2].string.split()[0][:-1] + '℃'.decode('utf8')
w.tmp_max = item[2].string.split()[-1][:-1] + '℃'.decode('utf8')
w.wind = item[3].em.string + item[3].b.string
w.pm25 = item[4].strong.string.split()[0]
w.pm25_desc = item[4].strong.string.split()[-1]
self.forecast.append(w)
if self.debug:
print w
# log_f = open("/root/bangu/log_f", 'a')
# log_f.write("%s\t%s\t%s\n".encode('utf8') %(datetime.now().strftime('%b-%d-%y %H:%M:%S'),
# self.forecast[0].tmp_max.encode('utf8'),
# self.forecast[0].tmp_min.encode('utf8')))
# log_f.close()
def __parser_month_block(self, month_block=None):
days = month_block.select('ul li')
for d in days:
w = weather()
try:
w.condition = d.img['alt']
w.condition_code = getConditionCode(w.condition)
w.tmp_min = str(int(d.p.string.split('/')[0])) + '℃'.decode('utf8')
w.tmp_max = str(int(d.p.string.split('/')[1][:-1])) + '℃'.decode('utf8')
self.month.append(w)
if self.debug:
print w
except:
pass
# weather_info = WeatherAPI(api_type='moji')
if __name__ == '__main__':
w = WeatherAPI(debug=True, api_type='moji')
print w.now.condition_code
# w.refresh()