diff --git a/app.py b/app.py index 158476c..e3daf9d 100644 --- a/app.py +++ b/app.py @@ -52,42 +52,45 @@ def worker(): -def fetch_and_save_contacts2(): +def fetch_and_save_contacts(): """ 获取联系人列表并保存到缓存 """ wxchat=gewe_chat.wxchat while True: - - login_keys = list(redis_helper.redis_helper.client.scan_iter(match='__AI_OPS_WX__:LOGININFO:*')) - logger.info(f"Fetching login keys: {login_keys}") - - # 遍历每一个获取到的登录键 - for k in login_keys: - r= redis_helper.redis_helper.get_hash(k) - # print(r) - token_id = r.get('tokenId') - app_id = r.get('appId') - - wxid = r.get('wxid') - status=r.get('status') - if status=='1': - ret,msg,contacts_list = wxchat.fetch_contacts_list(token_id, app_id) - friend_wxids = contacts_list['friends'][3:] # 可以调整截取范围 - #friend_wxids.remove('weixin') - wxchat.save_contacts_brief_to_cache(token_id, app_id, wxid, friend_wxids) - logger.info(f'微信ID {wxid} 登录APPID {app_id} 成功,联系人已定时保存') - chatrooms=contacts_list['chatrooms'] - wxchat.save_groups_info_to_cache(token_id, app_id, wxid, chatrooms) - wxchat.save_groups_members_to_cache(token_id, app_id, wxid, chatrooms) - logger.info(f'微信ID {wxid} 登录APPID {app_id} 成功,群信息已定时保存') - - else: - logger.info(f'微信ID {wxid} 未登录 {app_id} ,联系人不能定时保存') - - time.sleep(3) - - #time.sleep(60*10) + try: + login_keys = list(redis_helper.redis_helper.client.scan_iter(match='__AI_OPS_WX__:LOGININFO:*')) + logger.info(f"Fetching login keys: {login_keys}") + + # 遍历每一个获取到的登录键 + for k in login_keys: + try: + r= redis_helper.redis_helper.get_hash(k) + # print(r) + token_id = r.get('tokenId') + app_id = r.get('appId') + + wxid = r.get('wxid') + status=r.get('status') + if status=='1': + ret,msg,contacts_list = wxchat.fetch_contacts_list(token_id, app_id) + if ret==200: + friend_wxids = [c for c in contacts_list['friends'] if c not in ['fmessage', 'medianote','weixin']] # 可以调整截取范围 + wxchat.save_contacts_brief_to_cache(token_id, app_id, wxid, friend_wxids) + logger.info(f'微信ID {wxid} 登录APPID {app_id} 成功,联系人已定时保存') + chatrooms=contacts_list['chatrooms'] + wxchat.save_groups_info_to_cache(token_id, app_id, wxid, chatrooms) + wxchat.save_groups_members_to_cache(token_id, app_id, wxid, chatrooms) + logger.info(f'微信ID {wxid} 登录APPID {app_id} 成功,群信息已定时保存') + else: + logger.warning(f'{msg}-微信ID {wxid} 登录APPID {app_id} 不能获取好友和群资料') + else: + logger.info(f'微信ID {wxid} 未登录 {app_id} ,联系人不能定时保存') + time.sleep(3) + except Exception as e: + logger.error(f'处理好友和群资料出错 login key {k}: {str(e)}', exc_info=True) + except Exception as e: + logger.error(f'发送错误 {str(e)}', exc_info=True) time.sleep(3600*1) def auto_add_contacts_from_chatrooms(): @@ -171,7 +174,7 @@ def start_wxchat_thread(): # gewe_chat.start() scan_wx_login_info() # 启动同步联系人线程 - threading.Thread(target=fetch_and_save_contacts2).start() + threading.Thread(target=fetch_and_save_contacts).start() threading.Thread(target=auto_add_contacts_from_chatrooms).start()