Skip to content

Commit 5188192

Browse files
committed
Merge pull request #38 from halfcrazy/weather-patch
支持最近3日天气查询
2 parents aaaf63a + a872f34 commit 5188192

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

slack_bot/plugins/weather.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* 上海天气
1616
"""
1717

18+
DAY = {0: u'今天', 1: u'明天', 2: u'后天'}
1819
TEMPERATURE_REGEX = re.compile(ur'(\d+)℃')
1920

2021

@@ -38,15 +39,20 @@ def handle(data, app=None, **kwargs):
3839
city = get_city(data)
3940
if not city:
4041
return '不会自己去看天气预报啊'
41-
res = weather(ak, city)[0]
42-
current = TEMPERATURE_REGEX.search(res['date']).groups()[0]
43-
text = u'当前: {0} {1} {2} 温度: {3}'.format(
44-
current, res['weather'], res['wind'], res['temperature'])
45-
type = 'dayPictureUrl' if check_time() == 'day' else 'dayPictureUrl'
46-
attaches = [gen_attachment(text, res[type], image_type='thumb',
47-
title=u'{}天气预报'.format(city),
48-
title_link='')]
49-
return text, attaches
42+
res = weather(ak, city)[:3]
43+
ret = []
44+
attaches = []
45+
for idx, day in enumerate(res):
46+
current = TEMPERATURE_REGEX.search(day['date']).groups()[0]
47+
text = u'{0}: {1} {2} {3} 温度: {4}'.format(
48+
DAY[idx], current, day['weather'],
49+
day['wind'], day['temperature'])
50+
ret.append(text)
51+
type = 'dayPictureUrl' if check_time() == 'day' else 'dayPictureUrl'
52+
attaches.append(gen_attachment(text, day[type], image_type='thumb',
53+
title=u'{}天气预报'.format(city),
54+
title_link=''))
55+
return '\n'.join(ret), attaches
5056

5157

5258
if __name__ == '__main__':

0 commit comments

Comments
 (0)