Skip to content

Commit db6f999

Browse files
committed
Merge pull request #40 from halfcrazy/remove-app-paramater
移除传递的app参数,在具体plugin中使用current_app来替代
2 parents ea87b14 + 954714f commit db6f999

20 files changed

+98
-76
lines changed

slack_bot/app.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# coding=utf-8
22
import os
33
import re
4-
from functools import partial
54

65
from flask import Flask
76

@@ -31,18 +30,18 @@ def create_app(config=None):
3130

3231
redis_store.init_app(app)
3332
cache.init_app(app)
33+
app.cache = cache
3434
app.plugin_modules = plugin_modules
3535

3636
slackbot = SlackBot(app)
37-
_callback = partial(callback, app=app)
38-
slackbot.set_handler(_callback)
37+
slackbot.set_handler(callback)
3938
slackbot.filter_outgoing(_filter)
4039

4140
return app
4241

4342

4443
@timeout(30.0)
45-
def callback(kwargs, app):
44+
def callback(kwargs):
4645
s = convert2str(kwargs['text'])
4746
trigger_word = convert2str(kwargs['trigger_word'])
4847
# remove trigger_words
@@ -61,7 +60,7 @@ def callback(kwargs, app):
6160

6261
for plugin_module in plugin_modules:
6362
if plugin_module.test(data):
64-
ret = plugin_module.handle(data, cache=cache, app=app)
63+
ret = plugin_module.handle(data)
6564
if not isinstance(ret, tuple):
6665
text = ret
6766
attaches = None

slack_bot/plugins/airpollution.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import json
3030
from bs4 import BeautifulSoup
3131

32+
from slack_bot.ext import cache
3233
from utils import gen_attachment
3334

3435
description = """
@@ -52,7 +53,7 @@ def test(data):
5253
return len(req) > 0
5354

5455

55-
def get_desc(cityname, cityshort, cache=None, app=None):
56+
def get_desc(cityname, cityshort):
5657
if cache is not None:
5758
r = cache.get('airpollution.%s' % (cityshort))
5859
if r:
@@ -77,21 +78,21 @@ def get_desc(cityname, cityshort, cache=None, app=None):
7778
image_url = soup.find(id='tr_pm25').find(id='td_pm25').find(
7879
'img').attrs.get('src')
7980
title = soup.find('title').text
80-
attaches = [gen_attachment(text, image_url, app=app, title=title,
81+
attaches = [gen_attachment(text, image_url, title=title,
8182
title_link=title_link)]
8283
return text, attaches
8384

8485

85-
def handle(data, cache=None, app=None, **kwargs):
86+
def handle(data):
8687
message = data['message']
8788
reqs = filter(lambda p: p[0].encode('utf-8') in message, city)
8889
req = reqs[0]
8990
try:
90-
return get_desc(req[0], req[1], cache, app=app)
91+
return get_desc(req[0], req[1])
9192
except Exception as e:
9293
print 'Error: {}'.format(e)
9394
return '空气查询失败, 请重试!', []
9495

9596

9697
if __name__ == '__main__':
97-
print handle({'message': '北京空气'}, None)
98+
print handle({'message': '北京空气'})

slack_bot/plugins/baidumap.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import cPickle as pickle
77
from datetime import datetime
88

9+
from flask import current_app as app
910
import requests
1011

1112
from utils import to_pinyin
@@ -193,7 +194,7 @@ def test(data):
193194
return REGEX.search(message)
194195

195196

196-
def handle(data, cache, app, **kwargs):
197+
def handle(data):
197198
if app is None:
198199
ak = '18691b8e4206238f331ad2e1ca88357e'
199200
else:
@@ -239,9 +240,9 @@ def handle(data, cache, app, **kwargs):
239240

240241

241242
if __name__ == '__main__':
242-
print handle({'message': '我想从兆维工业园到北京南站'}, None, None)
243-
print handle({'message': '我想从人大到北京南站'}, None, None)
244-
print handle({'message': '我想从人大到豆瓣'}, None, None)
245-
print handle({'message': '我想从兆维工业园到北京南站 步行'}, None, None)
246-
print handle({'message': '我想从兆维工业园到北京南站 开车'}, None, None)
247-
print handle({'message': '从酒仙桥去798'}, None, None)
243+
print handle({'message': '我想从兆维工业园到北京南站'})
244+
print handle({'message': '我想从人大到北京南站'})
245+
print handle({'message': '我想从人大到豆瓣'})
246+
print handle({'message': '我想从兆维工业园到北京南站 步行'})
247+
print handle({'message': '我想从兆维工业园到北京南站 开车'})
248+
print handle({'message': '从酒仙桥去798'})

slack_bot/plugins/dianping.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import hashlib
44
from functools import partial
55

6+
from flask import current_app as app
67
import requests
78

89
from baidumap import address2geo
@@ -110,7 +111,7 @@ def test(data):
110111
for i in ['有什么美食', '大众点评', '附近美食']])
111112

112113

113-
def handle(data, app=None, **kwargs):
114+
def handle(data):
114115
message = data['message']
115116
if app is None:
116117
appkey = '41502445'

slack_bot/plugins/earthquake.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def jw(a, b):
5656
return ','.join((aa, bb))
5757

5858

59-
def handle(data, **kwargs):
59+
def handle(data):
6060
r = urllib2.urlopen(
6161
'http://data.earthquake.cn/datashare/globeEarthquake_csn.html',
6262
timeout=5)
@@ -66,4 +66,4 @@ def handle(data, **kwargs):
6666
(t[7], jw(t[2], t[3]), ' '.join(t[0:2]), t[5], t[4], t[6])
6767

6868
if __name__ == '__main__':
69-
print handle({'message': '地震了吗?'}, None)
69+
print handle({'message': '地震了吗?'})

slack_bot/plugins/events.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import requests
77
from bs4 import BeautifulSoup
88

9+
from slack_bot.ext import cache
910
from utils import check_cache
1011

1112
description = """
@@ -224,7 +225,7 @@ def test(data):
224225
return '最近有什么活动' in data['message']
225226

226227

227-
def handle(data, cache=None, **kwargs):
228+
def handle(data):
228229
message = data['message']
229230
if not isinstance(message, unicode):
230231
message = message.decode('utf-8')
@@ -237,5 +238,5 @@ def handle(data, cache=None, **kwargs):
237238

238239

239240
if __name__ == '__main__':
240-
print handle({'message': '最近有什么活动'}, None, None)
241-
print handle({'message': '最近有什么活动 上海'}, None, None)
241+
print handle({'message': '最近有什么活动'})
242+
print handle({'message': '最近有什么活动 上海'})

slack_bot/plugins/github_issue.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
from flask import current_app as app
34
import requests
45

56
description = """
@@ -15,7 +16,7 @@ def test(data):
1516
return 'issue' in data['message']
1617

1718

18-
def handle(data, app, **kwargs):
19+
def handle(data):
1920
org_name = app.config.get('ORG_NAME', 'python-cn')
2021
repos = requests.get(REPO_API.format(org=org_name)).json()
2122
rv = ''
@@ -39,4 +40,4 @@ def handle(data, app, **kwargs):
3940
from flask import Flask
4041
app = Flask(__name__)
4142
app.config['org_name'] = 'python-cn'
42-
print handle(None, None, app)
43+
print handle(None)

slack_bot/plugins/help.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# coding=utf-8
2+
from flask import current_app
23

34
description = """
45
帮助信息,触发条件: "help [私聊]". 比如:
@@ -20,7 +21,8 @@ def test(data):
2021
return 'help' in data['message']
2122

2223

23-
def handle(data, app, **kwargs):
24+
def handle(data):
25+
app = current_app
2426
plugin_modules = app.plugin_modules if app else []
2527
docs = []
2628
for plugin in plugin_modules:

slack_bot/plugins/movie.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# coding=utf-8
22

3+
from flask import current_app as app
34
import requests
45
from bs4 import BeautifulSoup
56

@@ -17,7 +18,7 @@
1718
LATER_URL = 'http://movie.douban.com/later/{0}/'
1819

1920

20-
def get_later_movie_info(city, app):
21+
def get_later_movie_info(city):
2122
r = requests.get(LATER_URL.format(city))
2223
soup = BeautifulSoup(r.text)
2324
items = soup.find(id='showing-soon').findAll('div', {'item'})
@@ -28,13 +29,13 @@ def get_later_movie_info(city, app):
2829
content = '|'.join([li.text for li in i.findAll('li')[:4]])
2930
image_url = i.find('a').find('img').attrs.get('src', '')
3031
# SA好变态, 感觉是防盗链了,下同
31-
image_url = upload_image(image_url, 'thumb', app)
32+
image_url = upload_image(image_url, 'thumb')
3233
yield u'<{url}|{title}> {content}'.format(**locals()), gen_attachment(
3334
content, image_url, image_type='thumb', title=title,
3435
title_link=url)
3536

3637

37-
def get_current_movie_info(city, app):
38+
def get_current_movie_info(city):
3839
r = requests.get(CURRENT_URL.format(city))
3940
soup = BeautifulSoup(r.text)
4041
items = soup.find(id='nowplaying').find('ul', {'class': 'lists'}).findAll(
@@ -48,7 +49,7 @@ def get_current_movie_info(city, app):
4849
content = '|'.join([li.text for li in i.findAll('li')[:4]])
4950
url = i.find('a').attrs.get('href', '')
5051
image_url = img.attrs.get('src', '')
51-
image_url = upload_image(image_url, 'thumb', app)
52+
image_url = upload_image(image_url, 'thumb')
5253
count += 1
5354
yield u'<{url}|{title}>'.format(**locals()), gen_attachment(
5455
content, image_url, image_type='thumb', title=title,
@@ -60,7 +61,7 @@ def test(data):
6061
any([i in data['message'] for i in ['上映', '热映', '有什么', '将']])
6162

6263

63-
def handle(data, app, **kwargs):
64+
def handle(data):
6465
message = data['message']
6566
if not isinstance(message, unicode):
6667
message = message.decode('utf-8')
@@ -78,5 +79,5 @@ def handle(data, app, **kwargs):
7879

7980

8081
if __name__ == '__main__':
81-
print handle({'message': '最近要将上映的电影'}, None)
82-
print handle({'message': '有什么电影 上海'}, None)
82+
print handle({'message': '最近要将上映的电影'})
83+
print handle({'message': '有什么电影 上海'})

slack_bot/plugins/orz.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test(data):
4242
return False
4343

4444

45-
def handle(data, **kwargs):
45+
def handle(data):
4646
mobai_icon = ':mb:'
4747
return mobai_icon * random.randrange(1, 10)
4848

0 commit comments

Comments
 (0)