This repository has been archived by the owner on Nov 17, 2019. It is now read-only.
forked from hellokuku/xiaohuangji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
140 lines (114 loc) · 4.06 KB
/
controller.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
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
#-*-coding:utf-8-*-
"""
Copyright (c) 2012 wong2 <wonderfuly@gmail.com>
Copyright (c) 2012 Qijiang Fan <fqj1994@gmail.com>
Original Author:
Wong2 <wonderfuly@gmail.com>
Changes Statement:
Changes made by Qijiang Fan <fqj1994@gmail.com> on
Jan 6 2013:
Add keywordfilter bindings.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
# 小黄鸡们
from renren import RenRen
from ai import magic
from ntype import NTYPES
from filter_manager import questionfilter, answerfilter
import re
import sys
import redis
try:
from my_accounts import accounts
except:
from accounts import accounts
try:
from settings import REDIS_HOST
except:
REDIS_HOST = 'localhost'
# 匹配自己名字的正则
self_match_pattern = re.compile('<a.*@小黄鸡.*</a>')
# 登录账号得到bot
def getBots(accounts):
if 'main.py' in sys.argv[0]:
bots = []
for account in accounts:
bot = RenRen()
bot.login(account[0], account[1])
print bot.email, 'login'
bots.append(bot)
return bots
else:
r = redis.Redis(REDIS_HOST)
bot = RenRen()
bot._loginByCookie(r.get('xiaohuangji_cookies'))
bot.email = ''
if bot.token:
return [bot]
else:
return []
bots = getBots(accounts)
# 根据通知得到该回复的更详细信息
def getNotiData(bot, data):
owner_id, doing_id = data['owner_id'], data['doing_id']
payloads = {
'owner_id': owner_id,
'doing_id': doing_id
}
ntype = data['ntype']
content = ''
if ntype == NTYPES['at_in_status']:
doing = bot.getDoingById(owner_id, doing_id)
if doing:
content = self_match_pattern.sub('', doing['content'].encode('utf-8'))
else:
return None, None
elif ntype == NTYPES['reply_in_status_comment']:
reply_id = data['reply_id']
comment = bot.getCommentById(owner_id, doing_id, reply_id)
if comment:
payloads.update({
'author_id': comment['ownerId'],
'author_name': comment['ubname'],
'reply_id': reply_id
})
content = comment['replyContent']
content_s = content.split(u'\uff1a', 1)
if len(content_s) == 1:
content_s = content.split(': ', 1)
if len(content_s) == 1:
content_s = content.split(':', 1)
content = content_s[-1]
print content
else:
return None, None
return payloads, content
# 得到数据,找到答案,发送回复
def reply(data):
bot = bots[0] # 现在只有一只小鸡了,且没了评论限制
data, message = getNotiData(bot, data)
if not data:
return
# 不要自问自答
if 'author_name' in data and '小黄鸡' in data['author_name'].encode('utf-8'):
return
print 'handling comment', data, '\n'
data['message'] = answerfilter(magic(questionfilter(message)))
result = bot.addComment(data)
if result['code'] != 0:
raise Exception('Error sending comment by bot %s' % bot.email)