def Send(self, ctype, uin, content): self.msgId += 1 sendUrl = { 'buddy': 'http://d1.web2.qq.com/channel/send_buddy_msg2', 'group': 'http://d1.web2.qq.com/channel/send_qun_msg2', 'discuss': 'http://d1.web2.qq.com/channel/send_discu_msg2' } sendTag = {'buddy':'to', 'group':'group_uin', 'discuss':'did'} self.smartRequest( url = sendUrl[ctype], data = { 'r': JsonDumps({ sendTag[ctype]: int(uin), 'content': JsonDumps([ content, ['font', {'name': '宋体', 'size': 10, 'style': [0,0,0], 'color': '000000'}] ]), 'face': 522, 'clientid': self.clientid, 'msg_id': self.msgId, 'psessionid': self.psessionid }) }, Referer = ('http://d1.web2.qq.com/proxy.html?v=20151105001&' 'callback=1&id=2') ) if self.msgId % 10 == 0: INFO('已连续发送10条消息,强制 sleep 10秒,请等待...') time.sleep(10) else: time.sleep(random.randint(1, 3))
def _send(self, msgType, to_uin, msg): self.msgId += 1 if self.msgId % 10 == 0: INFO('已连续发送10条消息,强制 sleep 10秒,请等待...') time.sleep(10) else: time.sleep(random.randint(3, 5)) sendUrl = { 'buddy': 'http://d1.web2.qq.com/channel/send_buddy_msg2', 'group': 'http://d1.web2.qq.com/channel/send_qun_msg2', 'discuss': 'http://d1.web2.qq.com/channel/send_discu_msg2' } sendTag = {'buddy': 'to', 'group': 'group_uin', 'discuss': 'did'} self.smartRequest( url=sendUrl[msgType], data={ 'r': JsonDumps({ sendTag[msgType]: to_uin, 'content': JsonDumps([ msg, [ 'font', { 'name': '宋体', 'size': 10, 'style': [0, 0, 0], 'color': '000000' } ] ]), 'face': 522, 'clientid': self.clientid, 'msg_id': self.msgId, 'psessionid': self.psessionid }) }, Referer=('http://d1.web2.qq.com/proxy.html?v=20151105001&' 'callback=1&id=2')) if msgType == 'buddy': INFO('向 好友“%s” 发消息成功', self.getBuddyByUin(to_uin)['name']) elif msgType == 'group': INFO('向 群“%s” 发消息成功', self.getGroupByUin(to_uin)['name']) else: INFO('向 讨论组“%s” 发消息成功', self.getDiscussByUin(to_uin)['name'])
def Poll(self): result = self.smartRequest( url='https://d1.web2.qq.com/channel/poll2', data={ 'r': JsonDumps({ 'ptwebqq': self.ptwebqq, 'clientid': self.clientid, 'psessionid': self.psessionid, 'key': '' }) }, Referer=('http://d1.web2.qq.com/proxy.html?v=20151105001&' 'callback=1&id=2')) if not result or 'errmsg' in result: return 'timeout', '', '', '' else: result = result[0] ctype = { 'message': 'buddy', 'group_message': 'group', 'discu_message': 'discuss' }[result['poll_type']] fromUin = str(result['value']['from_uin']) memberUin = str(result['value'].get('send_uin', '')) content = ''.join( ('[face%d]' % m[1]) if isinstance(m, list) else str(m) for m in result['value']['content'][1:]) return ctype, fromUin, memberUin, content
def fetchBuddies(self): INFO('登录 Step6 - 获取好友列表') result = self.smartRequest( url='http://s.web2.qq.com/api/get_user_friends2', data={ 'r': JsonDumps({ 'vfwebqq': self.vfwebqq, 'hash': self.hash }) }, Referer=('http://d1.web2.qq.com/proxy.html?v=20151105001&' 'callback=1&id=2')) ss, self.buddies, self.buddiesDictU, self.buddiesDictQ = [], [], {}, {} for info in result.get('info', []): uin = info['uin'] name = info['nick'] qq = self.fetchBuddyQQ(uin) buddy = {'uin': uin, 'qq': qq, 'name': name} self.buddies.append(buddy) self.buddiesDictU[uin] = buddy self.buddiesDictQ[qq] = buddy s = '%d, %s, uin%d' % (qq, name, uin) INFO('好友: ' + s) ss.append(s) self.buddyStr = '好友列表:\n' + '\n'.join(ss) INFO('获取朋友列表成功,共 %d 个朋友' % len(self.buddies))
def fetchBuddies(self, contacts, silence=True): if not silence: INFO('登录 Step6 - 获取好友列表') result = self.smartRequest( url='http://s.web2.qq.com/api/get_user_friends2', data={ 'r': JsonDumps({ 'vfwebqq': self.vfwebqq, 'hash': self.hash }) }, Referer=('http://d1.web2.qq.com/proxy.html?v=20151105001&' 'callback=1&id=2')) markDict = dict((d['uin'], d['markname']) for d in result['marknames']) qqResult = self.smartRequest( url='http://qun.qq.com/cgi-bin/qun_mgr/get_friend_list', data={'bkn': self.bkn}, Referer='http://qun.qq.com/member.html') qqDict = defaultdict(list) for blist in qqResult.values(): for d in blist.get('mems', []): name = d['name'].replace(' ', ' ').replace('&', '&') qqDict[name].append(d['uin']) for info in result['info']: uin = info['uin'] nick = info['nick'] mark = markDict.get(uin, '') name = mark or nick qqlist = qqDict.get(name, []) if len(qqlist) == 1: qq = qqlist.pop() else: qq = self.fetchBuddyQQ(uin) try: qqlist.remove(qq) except ValueError: pass contact = contacts.Add('buddy', str(uin), name, str(qq), nick, mark) if not silence: INFO(repr(contact)) if not silence: INFO('获取朋友列表成功,共 %d 个朋友' % len(result['info']))
def poll(self): result = self.smartRequest( url='https://d1.web2.qq.com/channel/poll2', data={ 'r': JsonDumps({ 'ptwebqq': self.ptwebqq, 'clientid': self.clientid, 'psessionid': self.psessionid, 'key': '' }) }, sessionObj=self.pollSession, Referer=('http://d1.web2.qq.com/proxy.html?v=20151105001&' 'callback=1&id=2')) if not result or 'errmsg' in result: DEBUG('无消息') return ('', 0, 0, '') # 无消息 else: result = result[0] msgType = { 'message': 'buddy', 'group_message': 'group', 'discu_message': 'discuss' }[result['poll_type']] from_uin = result['value']['from_uin'] buddy_uin = result['value'].get('send_uin', from_uin) msg = ''.join( ('[face%d]' % m[1]) if isinstance(m, list) else str(m) for m in result['value']['content'][1:]) if msgType == 'buddy': bName = self.getBuddyByUin(buddy_uin)['name'] INFO('来自 好友“%s” 的消息: "%s"' % (bName, msg)) elif msgType == 'group': group = self.getGroupByUin(from_uin) gName = group['name'] bName = group['member'].get(buddy_uin, 'unknown') INFO('来自 群“%s”[成员“%s”] 的消息: "%s"' % (gName, bName, msg)) else: discuss = self.getDiscussByUin(from_uin) gName = discuss['name'] bName = discuss['member'].get(buddy_uin, 'unknown') INFO('来自 讨论组“%s”[成员“%s”] 的消息: "%s"' % (gName, bName, msg)) return (msgType, from_uin, buddy_uin, msg)
def getUinAndPsessionid(self): INFO('登录 Step5 - 获取uin和psessionid') result = self.smartRequest( url = 'http://d1.web2.qq.com/channel/login2', data = { 'r': JsonDumps({ 'ptwebqq': self.ptwebqq, 'clientid': self.clientid, 'psessionid': '', 'status': 'online' }) }, Referer = ('http://d1.web2.qq.com/proxy.html?v=20151105001' '&callback=1&id=2'), Origin = 'http://d1.web2.qq.com' ) self.uin = result['uin'] self.psessionid = result['psessionid'] self.hash = qHash(self.uin, self.ptwebqq) self.bkn = bknHash(self.session.cookies['skey'])
def fetchGroups(self): INFO('登录 Step7 - 获取群列表') INFO('=' * 60) result = self.smartRequest( url='http://s.web2.qq.com/api/get_group_name_list_mask2', data={ 'r': JsonDumps({ 'vfwebqq': self.vfwebqq, 'hash': self.hash }) }, Referer=('http://d1.web2.qq.com/proxy.html?v=20151105001&' 'callback=1&id=2')) ss, self.groups, self.groupsDictU, self.groupsDictQ = [], [], {}, {} for info in result.get('gnamelist', []): uin = info['gid'] name = info['name'] qq = self.fetchGroupQQ(uin) member = self.fetchGroupMember(info['code']) group = {'uin': uin, 'qq': qq, 'name': name, 'member': member} self.groups.append(group) self.groupsDictU[uin] = group self.groupsDictQ[qq % 1000000] = group s = '%d, %s, uin%d' % (qq, name, uin) ss.append(s) INFO('群: ' + s) mss = [] for uin, name in member.items(): ms = '%s, uin%d' % (name, uin) INFO(' 成员: %s', ms) mss.append(ms) INFO('=' * 60) group['memberStr'] = '群 %s 的成员列表:\n%s' % (name, '\n'.join(mss)) self.groupStr = '群列表:\n' + '\n'.join(ss) INFO('获取群列表成功,共 %d 个朋友' % len(self.groups))
def fetchGroups(self, contacts, silence=True): if not silence: INFO('登录 Step7 - 获取群列表') INFO('=' * 60) for i in range(5): result = self.smartRequest( url='http://s.web2.qq.com/api/get_group_name_list_mask2', data={ 'r': JsonDumps({ 'vfwebqq': self.vfwebqq, 'hash': self.hash }) }, Referer=('http://d1.web2.qq.com/proxy.html?v=20151105001&' 'callback=1&id=2')) if 'gmarklist' in result: break else: ERROR('获取群列表出错,等待 3 秒后再次尝试一次') time.sleep(3) else: CRITICAL('无法获取到群列表') raise QSession.Error markDict = dict((d['uin'], d['markname']) for d in result['gmarklist']) qqResult = self.smartRequest( url='http://qun.qq.com/cgi-bin/qun_mgr/get_group_list', data={'bkn': self.bkn}, Referer='http://qun.qq.com/member.html') qqDict = defaultdict(list) for k in ('create', 'manage', 'join'): for d in qqResult.get(k, []): name = d['gn'].replace(' ', ' ').replace('&', '&') qqDict[name].append(d['gc']) for info in result['gnamelist']: uin = info['gid'] name = info['name'] mark = markDict.get(uin, '') qqlist = qqDict.get(name, []) if len(qqlist) == 1: qq = qqlist.pop() else: qq = self.fetchGroupQQ(uin) for x in qqlist: if (qq - x) % 1000000 == 0: qq = x break try: qqlist.remove(qq) except ValueError: pass members = self.fetchGroupMember(info['code']) c = contacts.Add('group', str(uin), name, str(qq), '', mark, members) if not silence: INFO(repr(c)) for uin, name in members.items(): INFO(' 成员: %s, uin%s', name, uin) INFO('=' * 60) if not silence: INFO('获取群列表成功,共 %d 个群' % len(result))