def get_contract(self): url = '%s/webwxgetcontact?r=%s&seq=0&skey=%s' % (self.loginInfo['url'], int(time.time()), self.loginInfo['skey']) headers = { 'ContentType': 'application/json; charset=UTF-8' } r = self.s.get(url, headers = headers) memberList = json.loads(r.content.decode('utf-8', 'replace'))['MemberList'] i = 1 # ISSUE 1.3 while i != 0: i = 0 for m in memberList: if m['Sex'] == 0 or m['Sex'] == '0': memberList.remove(m);i+=1 # deal with emoji memberList = tools.emoji_dealer(memberList) # PYQuanPin & RemarkPYQuanPin is used as identifier voidUserList = [] for m in memberList: if m['PYQuanPin'] == '': if m['UserName'] == self.storageClass.userName: m['PYQuanPin'] = m['NickName'] elif m['RemarkPYQuanPin'] == '': voidUserList.append(m) else: m['PYQuanPin'] = m['RemarkPYQuanPin'] insertResult = self.storageClass.update_user(m['PYQuanPin'], NickName = m['RemarkName'] or m['NickName'], UserName = m['UserName']) if insertResult != True: out.print_line(str(insertResult) + ' need a new RemarkName') if DEBUG: with open('MemberList.txt', 'w') as f: f.write(str(memberList))
def check_login(self): url = '%s/cgi-bin/mmwebwx-bin/login'%BASE_URL # add tip so that we can get many reply, use string payloads to avoid auto-urlencode payloads = 'tip=1&uuid=%s&_=%s'%(self.uuid, int(time.time())) r = self.s.get(url, params = payloads) regx = r'window.code=(\d+)' data = re.search(regx, r.text) if data and data.group(1) == '200': regx = r'window.redirect_uri="(\S+)";' self.loginInfo['url'] = re.search(regx, r.text).group(1) r = self.s.get(self.loginInfo['url'], allow_redirects=False) self.loginInfo['url'] = self.loginInfo['url'][:self.loginInfo['url'].rfind('/')] self.get_login_info(r.text) tools.clear_screen() return False if data and data.group(1) == '201': tools.clear_screen() out.print_line('Please press confirm', True) if data and data.group(1) == '408': tools.clear_screen() out.print_line('Reloading QR Code\n', True) while 1: while self.get_QRuuid(): time.sleep(1) if self.get_QR(): break return True
def load_login_status(self, fileDir): try: with open(fileDir, 'rb') as f: j = pickle.load(f) except Exception as e: return False self.loginInfo = j['loginInfo'] self.s.cookies = requests.utils.cookiejar_from_dict(j['cookies']) self.storageClass.loads(j['storage']) if self.__sync_check(): out.print_line('Login successfully as %s\n'%self.storageClass.nickName, False) self.start_receiving() return True else: return False
def open_QR(): for get_count in range(10): out.print_line('Getting uuid', True) while not self.get_QRuuid(): time.sleep(1) out.print_line('Getting QR Code', True) if self.get_QR(): break elif get_count >= 9: out.print_line('Failed to get QR Code, please restart the program') sys.exit() out.print_line('Please scan the QR Code', True)
def start_receiving(self): def maintain_loop(): i = self.__sync_check() count = 0 pauseTime = 1 while i and count <4: try: if pauseTime < 5: pauseTime += 2 if i != '0': msgList = self.__get_msg() if msgList: msgList = self.__produce_msg(msgList) for msg in msgList: self.msgList.insert(0, msg) pauseTime = 1 time.sleep(pauseTime) i = self.__sync_check() count = 0 except Exception, e: count += 1 time.sleep(count*3) out.print_line('LOG OUT', False)
def auto_login(self): def open_QR(): for get_count in range(10): out.print_line('Getting uuid', True) while not self.get_QRuuid(): time.sleep(1) out.print_line('Getting QR Code', True) if self.get_QR(): break elif get_count >= 9: out.print_line('Failed to get QR Code, please restart the program') sys.exit() out.print_line('Please scan the QR Code', True) open_QR() while 1: status = self.check_login() if status == '200': break elif status == '201': out.print_line('Please press confirm', True) elif status == '408': out.print_line('Reloading QR Code\n', True) open_QR() self.web_init() self.show_mobile_login() tools.clear_screen() self.get_contract() out.print_line('Login successfully as %s\n'%( self.storageClass.find_nickname(self.storageClass.userName)), False) self.start_receiving()
def login(self): for get_count in range(10): out.print_line('Getting uuid', True) while self.get_QRuuid(): time.sleep(1) out.print_line('Getting QR Code', True) if self.get_QR(): break elif get_count >= 9: out.print_line('Failed to get QR Code, please restart the program') sys.exit() out.print_line('Please scan the QR Code', True) while self.check_login(): time.sleep(1) self.web_init() self.show_mobile_login() while 1: voidUserList = self.get_contract() if not voidUserList: break out.print_line('These uses need new RemarkNames and are added to a group', True) chatRoomName = self.create_chatroom(voidUserList, 'RemarkNames') self.delete_member(chatRoomName, [voidUserList[0]]) self.add_member(chatRoomName, [voidUserList[0]]) while raw_input('Enter "ready" after you rename all of them and DELETE the group: ') != 'ready': pass out.print_line('Start reload contract list', False) out.print_line('Login successfully as %s\n'%( self.storageClass.find_nickname(self.storageClass.userName)), False) log.log('SIGN IN', True) thread.start_new_thread(self.maintain_loop, ())
def __produce_msg(self, l): rl = [] srl = [40, 43, 50, 52, 53, 9999] # 40 msg, 43 videochat, 50 VOIPMSG, 52 voipnotifymsg, 53 webwxvoipnotifymsg, 9999 sysnotice for m in l: if '@@' in m['FromUserName']: m = self.__produce_group_chat(m) if m['MsgType'] == 1: # words if m['Url']: regx = r'(.+?\(.+?\))' data = re.search(regx, m['Content']) msg = { 'Type': 'Map', 'Text': data.group(1),} else: msg = { 'Type': 'Text', 'Text': m['Content'],} elif m['MsgType'] == 3 or m['MsgType'] == 47: # picture def download_picture(picDir): url = '%s/webwxgetmsgimg'%self.loginInfo['url'] payloads = { 'MsgID': m['NewMsgId'], 'skey': self.loginInfo['skey'],} r = self.s.get(url, params = payloads, stream = True) with open(picDir, 'wb') as f: for block in r.iter_content(1024): f.write(block) msg = { 'Type': 'Picture', 'Text': download_picture,} elif m['MsgType'] == 34: # voice def download_voice(voiceDir): url = '%s/webwxgetvoice'%self.loginInfo['url'] payloads = { 'msgid': m['NewMsgId'], 'skey': self.loginInfo['skey'],} r = self.s.get(url, params = payloads, stream = True) with open(voiceDir, 'wb') as f: for block in r.iter_content(1024): f.write(block) msg = { 'Type': 'Recording', 'Text': download_voice,} elif m['MsgType'] == 37: # friends msg = { 'Type': 'Friends', 'Text': { 'Status': m['Status'], 'UserName': m['RecommendInfo']['UserName'], 'Ticket': m['Ticket'], }, } elif m['MsgType'] == 42: # name card msg = { 'Type': 'Card', 'Text': m['RecommendInfo'], } elif m['MsgType'] == 49: # sharing if m['AppMsgType'] == 6: def download_atta(attaDir): cookiesList = {name:data for name,data in self.s.cookies.items()} url = 'https://file%s.wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetmedia'%('2' if '2' in self.loginInfo['url'] else '') payloads = { 'sender': m['FromUserName'], 'mediaid': m['MediaId'], 'filename': m['FileName'], 'fromuser': self.loginInfo['wxuin'], 'pass_ticket': 'undefined', 'webwx_data_ticket': cookiesList['webwx_data_ticket'],} r = self.s.get(url, params = payloads, stream = True) with open(attaDir, 'wb') as f: for block in r.iter_content(1024): f.write(block) msg = { 'Type': 'Attachment', # 'FileName': m['FileName'], 'Text': download_atta, } elif m['AppMsgType'] == 17: msg = { 'Type': 'Note', 'Text': m['FileName'], } elif m['AppMsgType'] == 2000: regx = r'\[CDATA\[(.+?)\].+?\[CDATA\[(.+?)\]' data = re.search(regx, m['Content']) msg = { 'Type': 'Note', 'Text': data.group(2), } else: msg = { 'Type': 'Sharing', 'Text': m['FileName'], } elif m['MsgType'] == 51: # phone init msg = { 'Type': 'Init', 'Text': m['ToUserName'], } elif m['MsgType'] == 62: # tiny video def download_video(videoDir): url = '%s/webwxgetvideo'%self.loginInfo['url'] payloads = { 'msgid': m['MsgId'], 'skey': self.loginInfo['skey'],} headers = { 'Range:': 'bytes=0-'} r = self.s.get(url, params = payloads, headers = headers, stream = True) with open(videoDir, 'wb') as f: for chunk in r.iter_content(chunk_size = 1024): if chunk: f.write(chunk) f.flush() os.fsync(f.fileno()) msg = { 'Type': 'Video', 'Text': download_video, } elif m['MsgType'] == 10000: msg = { 'Type': 'Note', 'Text': m['Content'],} elif m['MsgType'] == 10002: regx = r'[CDATA[(.+?)]]' data = re.search(regx, m['Content']) msg = { 'Type': 'Note', 'Text': data.group(1).replace('\\', ''), } elif m['MsgType'] in srl: msg = { 'Type': 'Useless', 'Text': 'UselessMsg', } else: out.print_line('MsgType Unknown: %s\n%s'%(m['MsgType'], str(m)), False) srl.append(m['MsgType']) msg = { 'Type': 'Useless', 'Text': 'UselessMsg', } m = dict(m, **msg) rl.append(m) return rl
def login(self): while 1: out.print_line('Getting uuid', True) while self.get_QRuuid(): time.sleep(1) out.print_line('Getting QR Code', True) if self.get_QR(): break out.print_line('Please scan the QR Code', True) while self.check_login(): time.sleep(1) self.web_init() self.show_mobile_login() while 1: voidUserList = self.get_contract() if not voidUserList: break for voidUser in voidUserList: out.print_line('This user needs a new RemarkName', False) out.print_line(str(voidUser), False) raw_input('Continue?') out.print_line('Start reload contract list', False) out.print_line('Login successfully as %s\n'%( self.storageClass.find_nickname(self.storageClass.userName)), False) log.log('SIGN IN', True) thread.start_new_thread(self.maintain_loop, ())