Skip to content

Commit

Permalink
format log & fix send msg retcode 108
Browse files Browse the repository at this point in the history
  • Loading branch information
coldnight committed May 29, 2014
1 parent f85735a commit 275a938
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 46 deletions.
2 changes: 1 addition & 1 deletion twqq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
# Desc :
#

__version__ = '0.2.5'
__version__ = '0.2.7'
10 changes: 5 additions & 5 deletions twqq/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def log_group_message(self, member_nick, content, group_code,
:param send_uin: 发送人的uin
:param source: 消息原包
"""
logger.info(u"获取{0} 群的{1} 发送消息: {2}"
logger.info(u"[群消息] {0}: {1} ==> {2}"
.format(group_code, member_nick, content))

@buddy_message_handler
Expand All @@ -75,7 +75,7 @@ def log_buddy_message(self, from_uin, content, source):
:param content: 内容
:param source: 消息原包
"""
logger.info(u"获取 {0} 发送的好友消息: {1}"
logger.info(u"[好友消息] {0} ==> {1}"
.format(from_uin, content))

@sess_message_handler
Expand All @@ -87,7 +87,7 @@ def log_sess_message(self, qid, from_uin, content, source):
:param content: 内容
:param source: 消息原包
"""
logger.info(u"获取 {0} 发送的临时消息: {1}"
logger.info(u"[临时消息] {0} ==> {1}"
.format(from_uin, content))

@discu_message_handler
Expand All @@ -99,7 +99,7 @@ def log_discu_message(self, did, from_uin, content, source):
:param content: 内容
:param source: 源消息
"""
logger.info(u"获取 {0} 发送的讨论组消息: {1}"
logger.info(u"[讨论组消息] {0} ==> {1}"
.format(did, content))

@kick_message_handler
Expand All @@ -112,7 +112,7 @@ def log_kick_message(self, message):
def log_system_message(self, typ, from_uin, account, source):
""" 记录系统消息日志
"""
logger.info("系统消息: 类型:{0}, 发送人:{1}, 发送账号:{2}, 源:{3}"
logger.info("[系统消息]: 类型:{0}, 发送人:{1}, 发送账号:{2}, 源:{3}"
.format(type, from_uin, account, source))

def setup_msg_handlers(self):
Expand Down
3 changes: 2 additions & 1 deletion twqq/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
import random

try:
from urllib import urlencode
import urllib
except ImportError:
import urllib.parse as urllib
import urllib.parse as urllib # py3

import logging
import tempfile
Expand Down
10 changes: 6 additions & 4 deletions twqq/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,15 @@ def set_group_detail(self, data):
""" 设置组详细信息, 包括群成员信息, 等.
"""
for kw in data.get("minfo", []):
tmp = GroupMInfo(**kw)
self._uin_name_map[tmp.nick] = tmp.uin
self._uin_map[tmp.uin] = tmp
nick, uin = kw.get("nick"), kw.get("uin")
if nick is not None:
tmp = GroupMInfo(**kw)
self._uin_name_map[tmp.nick] = tmp.uin
self._uin_map[tmp.uin] = tmp

for item in data.get("cards", []):
uin = item.get("muin")
if uin in self._uin_map:
if uin in self._uin_map and item.get("card") is not None:
self._uin_map[uin].card = item.get("card")
self._uin_name_map[item.get("card")] = uin
else:
Expand Down
60 changes: 25 additions & 35 deletions twqq/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ def __init__(self, *args, **kwargs):
def handle_exc(self, type, value, trace):
pass

def handle_retcode(self, data, msg):
if isinstance(data, dict):
retcode = data.get("retcode")
if retcode == 0:
logger.info(u"{0} 成功".format(msg))
elif retcode == 8:
logger.error(u"{0} 失败, 需要重新登录".format(msg))

return

logger.warn(u"{0} 失败 <{1}>".format(msg, data))


class LoginSigRequest(WebQQRequest):
url = "https://ui.ptlogin2.qq.com/cgi-bin/login"
Expand Down Expand Up @@ -490,7 +502,8 @@ def init_params(self, group_sig):
("psessionid", self.hub.psessionid))

def callback(self, resp, data):
logger.info(u"发送给 {0} 临时消息成功: {1}".format(self.to, data))
self.handle_retcode(data, u"[临时消息] {0} ==> {1}"
.format(self.content, self.to))
self.hub.consume_delay(self.number)


Expand Down Expand Up @@ -519,8 +532,8 @@ def init(self, group_uin, content, style):
self.headers.update(Origin=const.D_ORIGIN, Referer=const.D_REFERER)

def callback(self, resp, data):
logger.info(u"发送群消息 {0} {1} 成功: {2}"
.format(self.source, self.group_uin, data))
self.handle_retcode(data, u"[群消息] {0} ==> {1}"
.format(self.source, self.group_uin))
self.hub.consume_delay(self.number)


Expand All @@ -537,36 +550,14 @@ def init(self):
self.headers.update(Referer=const.S_REFERER)

def callback(self, resp, data):
logger.info(u"获取讨论组列表: {0!r}".format(data))
logger.info(u"[群列表] ==> {0!r}".format(data))
if data.get("retcode") == 0:
self.hub.set_discu(data.get("result", {}))
dids = self.hub.get_discu().dids
for did in dids:
self.hub.load_next_request(DiscuInfoRequest(did))


class QQNumberRequest(WebQQRequest):
""" 查看好友QQ号码
"""
url = "http://s.web2.qq.com/api/get_friend_uin2"

def init(self, uin):
self.params = {"code": "", "t": time.time() * 1000,
"tuin": uin, "type": 0,
"verifysession": "",
"vfwebqq": self.hub.vfwebqq}
self.headers.update(Referer=const.S_REFERER)

def callback(self, response, data):
if isinstance(data, dict) and data.get("retcode") == 0:
r = data.get('result')
uin = r.get("uin")
account = r.get("account")
self.hub.get_friends().set_account(uin, account)
else:
logger.warn(u"获取好友QQ号码失败: {0!r}".format(data))


class DiscuInfoRequest(WebQQRequest):
""" 获取讨论组详细信息
Expand All @@ -586,7 +577,7 @@ def init(self, did):
def callback(self, resp, data):
if data.get("retcode") == 0:
discu = self.hub.get_discu()
logger.info(u"获取讨论组 {0} 的详细信息: {1}"
logger.info(u"[讨论组] ==> {0} 的详细信息: {1}"
.format(discu.get_name(self._did), data))
discu.set_detail(self._did, data.get("result", {}))

Expand Down Expand Up @@ -614,8 +605,8 @@ def init(self, did, content, style):
self.headers.update(Referer=const.D_REFERER)

def callback(self, resp, data):
logger.info(u"发送讨论组消息 {0} {1} 成功: {2}"
.format(self.source, self.did, data))
self.handle_retcode(data, u"[讨论组消息] {0} ==> {1}"
.format(self.source, self.did))
self.hub.consume_delay(self.number)


Expand Down Expand Up @@ -645,9 +636,8 @@ def init(self, to_uin, content, style):
self.delay, self.number = self.hub.get_delay(content)

def callback(self, resp, data):
logger.info(u"发送好友消息 {0} 给 {1} 成功: {2}"
.format(self.source, self.to_uin, data))

self.handle_retcode(data, u"[好友消息] {0} ==> {1}"
.format(self.source, self.to_uin))
self.hub.consume_delay(self.number)


Expand All @@ -667,7 +657,7 @@ def init(self, signature):
self.headers.update(Referer=const.S_REFERER)

def callback(self, resp, data):
logger.info(u"设置签名成功: {0}".format(data))
logger.info(u"[设置签名] {0}".format(data))


class AcceptVerifyRequest(WebQQRequest):
Expand All @@ -692,11 +682,11 @@ def init(self, uin, qq_num, markname=""):

def callback(self, resp, data):
if data.get("retcode") == 0:
logger.info(u"添加 {0} 成功".format(self.qq_num))
logger.info(u"[好友添加] 添加 {0} 成功".format(self.qq_num))
# if self.markname:
# self.hub.mark_to_uin[self.markname] = self.uin
else:
logger.info(u"添加 {0} 失败".format(self.qq_num))
logger.info(u"[好友添加] 添加 {0} 失败".format(self.qq_num))


class FileRequest(WebQQRequest):
Expand Down

0 comments on commit 275a938

Please sign in to comment.