Skip to content

Commit

Permalink
把消息处理的一部分提到了worker的步骤,希望能快一些
Browse files Browse the repository at this point in the history
  • Loading branch information
wong2 committed Jan 6, 2013
1 parent 3f25271 commit 8c8271f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.pyc
my_accounts.py
my_filterconfig.py
settings.py
60 changes: 55 additions & 5 deletions controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@

from renren import RenRen
from ai import magic
from ntype import NTYPES
from filter_manager import questionfilter, answerfilter
import redis
import re
try:
from my_accounts import accounts
except:
from accounts import accounts

# 匹配自己名字的正则
self_match_pattern = re.compile('<a.*@小黄鸡.*</a>')

# 登录账号得到bot
def getBots(accounts):
bots = []
for account in accounts:
Expand All @@ -55,18 +59,64 @@ def getBots(accounts):

bots = getBots(accounts)

r = redis.Redis()

# 根据通知得到该回复的更详细信息
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, message):
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)))

bot = bots[0] # 现在只有一只小鸡了,且没了评论限制
result = bot.addComment(data)

if result['code'] != 0:
Expand Down
54 changes: 11 additions & 43 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import requests
import time
import re
import sys
from controller import bots, reply

# 匹配自己名字的正则
Expand Down Expand Up @@ -68,50 +69,12 @@ def parseNotification(notification):
def handle(bot, notification):
data = parseNotification(notification)

print time.strftime('%Y-%m-%d %I:%M:%S', time.localtime(time.time())), data
print time.strftime('%Y-%m-%d %I:%M:%S', time.localtime(time.time())), 'got notification'
ntype = data['ntype']

if not ntype in NTYPES.values():
return

owner_id, doing_id = data['owner_id'], data['doing_id']

payloads = {
'owner_id': owner_id,
'doing_id': doing_id
}

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

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

print ''
# 进入消息队列
q.enqueue(reply, payloads, content)
if ntype in NTYPES.values():
# 进入消息队列
q.enqueue(reply, data)


# 得到人人上的通知,处理之
Expand Down Expand Up @@ -140,9 +103,14 @@ def process(bot, just_clear=False):

print ''

if __name__ == '__main__':
def main():
while True:
try:
map(process, bots)
except KeyboardInterrupt:
sys.exit()
except:
pass

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion rqworker.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from rq.scripts import read_config_file
from rq.scripts import setup_default_arguments

from controller import bots, r
from controller import bots, getNotiData, self_match_pattern


def format_colors(record, handler):
Expand Down

0 comments on commit 8c8271f

Please sign in to comment.