def load_login_status(self, fileDir, loginCallback=None, exitCallback=None): try: with open(fileDir, 'rb') as f: j = pickle.load(f) except Exception as e: logger.debug('No such file, loading login status failed.') return ReturnValue({'BaseResponse': { 'ErrMsg': 'No such file, loading login status failed.', 'Ret': -1002, }}) if j.get('version', '') != VERSION: logger.debug(('you have updated itchat from %s to %s, ' + 'so cached status is ignored') % ( j.get('version', 'old version'), VERSION)) return ReturnValue({'BaseResponse': { 'ErrMsg': 'cached status ignored because of version', 'Ret': -1005, }}) self.loginInfo = j['loginInfo'] self.loginInfo['User'] = templates.User(self.loginInfo['User']) self.loginInfo['User'].core = self self.s.cookies = requests.utils.cookiejar_from_dict(j['cookies']) self.storageClass.loads(j['storage']) try: msgList, contactList = self.get_msg() except: msgList = contactList = None if (msgList or contactList) is None: self.logout() load_last_login_status(self.s, j['cookies']) logger.debug('server refused, loading login status failed.') return ReturnValue({'BaseResponse': { 'ErrMsg': 'server refused, loading login status failed.', 'Ret': -1003, }}) else: if contactList: for contact in contactList: if '@@' in contact['UserName']: update_local_chatrooms(self, [contact]) else: update_local_friends(self, [contact]) if msgList: msgList = produce_msg(self, msgList) for msg in msgList: self.msgList.put(msg) self.start_receiving(exitCallback) logger.debug('loading login status succeeded.') if hasattr(loginCallback, '__call__'): loginCallback() return ReturnValue({'BaseResponse': { 'ErrMsg': 'loading login status succeeded.', 'Ret': 0, }})
def maintain_loop(): retryCount = 0 while self.alive: try: i = sync_check(self) if i is None: self.alive = False elif i == '0': pass else: msgList, contactList = self.get_msg() if msgList: msgList = produce_msg(self, msgList) for msg in msgList: self.msgList.put(msg) if contactList: chatroomList, otherList = [], [] for contact in contactList: if '@@' in contact['UserName']: chatroomList.append(contact) else: otherList.append(contact) chatroomMsg = update_local_chatrooms( self, chatroomList) chatroomMsg['User'] = self.loginInfo['User'] self.msgList.put(chatroomMsg) update_local_friends(self, otherList) retryCount = 0 except requests.exceptions.ReadTimeout: pass except: retryCount += 1 logger.error(traceback.format_exc()) if self.receivingRetryCount < retryCount: self.alive = False else: time.sleep(1) self.logout() if hasattr(exitCallback, '__call__'): exitCallback() else: logger.info('LOG OUT!')
def web_init(self): url = '%s/webwxinit?r=%s' % (self.loginInfo['url'], int(time.time())) data = { 'BaseRequest': self.loginInfo['BaseRequest'], } headers = { 'ContentType': 'application/json; charset=UTF-8', 'User-Agent': config.USER_AGENT, } print data r = self.s.post(url, data=json.dumps(data), headers=headers) dic = json.loads(r.content.decode('utf-8', 'replace')) # deal with login info utils.emoji_formatter(dic['User'], 'NickName') self.loginInfo['InviteStartCount'] = int(dic['InviteStartCount']) self.loginInfo['User'] = wrap_user_dict( utils.struct_friend_info(dic['User'])) self.memberList.append(self.loginInfo['User']) self.loginInfo['SyncKey'] = dic['SyncKey'] self.loginInfo['synckey'] = '|'.join([ '%s_%s' % (item['Key'], item['Val']) for item in dic['SyncKey']['List'] ]) self.storageClass.userName = dic['User']['UserName'] self.storageClass.nickName = dic['User']['NickName'] # deal with contact list returned when init contactList = dic.get('ContactList', []) chatroomList, otherList = [], [] for m in contactList: if m['Sex'] != 0: otherList.append(m) elif '@@' in m['UserName']: m['MemberList'] = [] # don't let dirty info pollute the list chatroomList.append(m) elif '@' in m['UserName']: # mp will be dealt in update_local_friends as well otherList.append(m) if chatroomList: update_local_chatrooms(self, chatroomList) if otherList: update_local_friends(self, otherList) return dic