def sync_session(client, input_queue, login_event, exit_event): """Sync wechat session.""" client_log = getLogger('client') authorize_url = client.open_authorize_url() authorization_prompt = 'Authorization url: {}'.format(authorize_url) client_log.info(authorization_prompt) while True: try: authorize_success = client.authorize() except WaitScanQRCode: continue if authorize_success: break client_log.info('Waiting for authorize...') time.sleep(2) client.login() build_contact(client) client_log.debug('Login success...') login_event.set() while True: try: sync_ret = client.sync_check() if sync_ret != 0: msgs = client.sync_message() for msg in msgs['AddMsgList']: try: msg_obj = parse_message(msg) except UnsupportedMessage: client_log.info('unsupported message %s', msg) continue else: if msg_obj.message: input_queue.put(msg_obj) client.flush_sync_key() except (RequestError, APIResponseError): client_log.info('api error.') except SessionExpiredError: client_log.error('wechat session is expired....') exit_event.set() break except: continue
def sync_session(client, input_queue, login_event, exit_event): """同步微信会话。""" authorize_url = client.get_authorize_url() client_log = getLogger('client') client_log.info('Open this %s in web browser', authorize_url) print('Open this %s in web browser' % authorize_url) while True: try: authorize_success = client.authorize() except WaitScanQRCode: continue if authorize_success: break client_log.info('Waiting for authorize...') time.sleep(2) client.login() build_contact(client) client_log.debug('Login success...') login_event.set() while True: try: sync_ret = client.sync_check() if sync_ret != 0: msgs = client.sync_message() for msg in msgs['AddMsgList']: try: msg_obj = parse_message(msg) except UnsupportedMessage: continue else: if msg_obj.message: input_queue.put(msg_obj) client.flush_sync_key() except (RequestError, APIResponseError): client_log.info('api error.') except SessionExpiredError: client_log.error('wechat session is expired....') exit_event.set() break except: continue
def sync_session(client): """Sync wechat session.""" client_log = getLogger('client') authorize_url = client.open_authorize_url() client_log.info('Authorization url: {}'.format(authorize_url)) while True: try: authorize_success = client.authorize() except WaitScanQRCode: continue except AuthorizeTimeout: client_log.warn('Waiting for authorization timeout.') sys.exit(0) if authorize_success: break client_log.info('Waiting for authorization...') time.sleep(2) client.login() client_log.info('Login success...') while True: try: sync_ret = client.sync_check() if sync_ret != 0: msgs = client.sync_message() for msg in msgs['AddMsgList']: try: msg_obj = parse_message(msg) except UnsupportedMessage: client_log.info('unsupported message %s', msg) continue else: client_log.info('receive message %s, %s', msg_obj, msg_obj.message) client.flush_sync_key() except (RequestError, APIResponseError): client_log.info('api error.') except SessionExpiredError: client_log.warn('wechat session is expired....') break
def sync_session(client): """Sync wechat session.""" client_log = getLogger('client') authorize_url = client.open_authorize_url() client_log.info('Authorization url: {}'.format(authorize_url)) while True: try: authorize_success = client.authorize() except WaitScanQRCode: continue except AuthorizeTimeout: client_log.warning('Waiting for authorization timeout.') sys.exit(0) if authorize_success: break client_log.info('Waiting for authorization...') time.sleep(2) client.login() client_log.info('Login success...') _client_contacts = {} """Get user WeChat contact.""" contacts = client.get_contact() for user in contacts: _client_contacts[user['UserName']] = user _client_contacts[client.user['UserName']] = client.user client_log.info(_client_contacts) # name = random.choice(list(_client_contacts.keys())) # client_log.info(_client_contacts[name]['UserName']) # client_log.info(_client_contacts[name]['RemarkName']) # client_log.info(client.user) # try: # msg = TextMessage(client.user['UserName'], _client_contacts[name]['UserName'], "新年快乐") # client.send_message(msg) # except: # client_log.info("wrong") for name in list(_client_contacts.keys()): time.sleep(1) client_log.info(_client_contacts[name]['UserName']) client_log.info(_client_contacts[name]['RemarkName']) try: msg = TextMessage(client.user['UserName'], _client_contacts[name]['UserName'], "新年快乐") client.send_message(msg) except: client_log.info("wrong") while True: try: sync_ret = client.sync_check() if sync_ret != 0: msgs = client.sync_message() for msg in msgs['AddMsgList']: try: msg_obj = parse_message(msg) except UnsupportedMessage: client_log.info('unsupported message %s', msg) continue else: client_log.info('receive message %s, %s', msg_obj, msg_obj.message) client.flush_sync_key() except (RequestError, APIResponseError): client_log.info('api error.') except SessionExpiredError: client_log.warning('wechat session is expired....') break