|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- from celery_app import celery_app
- from fastapi import Request,FastAPI
- import time,datetime
- from celery import Celery
- import celery.schedules
- from redbeat import RedBeatSchedulerEntry
- from datetime import timedelta
-
- from services.redis_service import RedisService
- from services.kafka_service import KafkaService
- from services.gewe_service import GeWeService
- from common.log import logger
- import asyncio,random
- from model.models import AddGroupContactsHistory
-
-
- @celery_app.task(name='tasks.add_task', bind=True, acks_late=True)
- def add_task(self, x, y):
- time.sleep(5)
- logger.info('add')
- return x + y
-
-
- @celery_app.task(name='tasks.mul_task', bind=True, acks_late=True)
- def mul_task(self, x, y):
- time.sleep(5)
- return x * y
-
-
-
-
-
-
-
-
-
- @celery_app.task(name='tasks.sync_contacts', bind=True, acks_late=True)
- async def sync_contacts_task(self, redis_service):
-
- login_keys = list(await redis_service.client.scan_iter(match='__AI_OPS_WX__:LOGININFO:*'))
- return login_keys
-
-
- @celery_app.task(name='tasks.background_worker_task', bind=True, acks_late=True)
- def background_worker_task(self, redis_config, kafka_config, gewe_config):
- async def task():
- redis_service = RedisService()
- await redis_service.init(**redis_config)
- login_keys = []
- async for key in redis_service.client.scan_iter(match='__AI_OPS_WX__:LOGININFO:*'):
- login_keys.append(key)
- print(login_keys)
-
- asyncio.run(task())
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- @celery_app.task(name='tasks.scheduled_task_sync_wx_info', bind=True, acks_late=True)
- def scheduled_task_sync_wx_info(self, redis_config, kafka_config, gewe_config):
- '''
- 定时获取微信号资料
- '''
- async def task():
- try:
- redis_service = RedisService()
- await redis_service.init(**redis_config)
- gewe_service = await GeWeService.get_instance(redis_service,gewe_config['api_url'])
- login_keys = []
- async for key in redis_service.client.scan_iter(match='__AI_OPS_WX__:LOGININFO:*'):
- login_keys.append(key)
-
- print(login_keys)
- for k in login_keys:
- r = await redis_service.get_hash(k)
- app_id = r.get("appId")
- token_id = r.get("tokenId")
- wxid = r.get("wxid")
- status = r.get('status')
- if status == '0':
- logger.warning(f"微信号 {wxid} 已经离线")
- continue
- ret, msg, profile = await gewe_service.get_profile_async(token_id, app_id)
- if ret != 200:
- logger.warning(f"同步微信号 {wxid} 资料失败: {ret}-{msg}")
- continue
- nickname=profile.get("nickName")
- head_img_url=profile.get("smallHeadImgUrl")
-
-
- nickname=profile.get("nickName")
- head_img_url=profile.get("smallHeadImgUrl")
- r.update({"nickName":nickname,"headImgUrl":head_img_url,"modify_at":int(time.time())})
- cleaned_login_info = {k: (v if v is not None else '') for k, v in r.items()}
- await redis_service.set_hash(k, cleaned_login_info)
- logger.info(f"定时同步微信号{wxid}-昵称{nickname} 资料成功")
-
- except Exception as e:
- logger.error(f"任务执行过程中发生异常: {e}")
-
- loop = asyncio.get_event_loop()
- if loop.is_closed():
- loop = asyncio.new_event_loop()
- asyncio.set_event_loop(loop)
-
- loop.run_until_complete(task())
-
-
-
- @celery_app.task(name='tasks.scheduled_task_add_contacts_from_chatrooms', bind=True, acks_late=True)
- def scheduled_task_add_contacts_from_chatrooms(self, redis_config, kafka_config, gewe_config):
- async def task():
- try:
- logger.info('定时群成员定时添好友任务开始')
- redis_service = RedisService()
- await redis_service.init(**redis_config)
- gewe_service = await GeWeService.get_instance(redis_service,gewe_config['api_url'])
- login_keys = []
- async for key in redis_service.client.scan_iter(match='__AI_OPS_WX__:LOGININFO:*'):
- login_keys.append(key)
-
-
- for k in login_keys:
- r = await redis_service.get_hash(k)
- app_id = r.get("appId")
- token_id = r.get("tokenId")
- wxid = r.get("wxid")
- status = r.get('status')
- if status == '0':
- logger.warning(f"微信号 {wxid} 已经离线,群成员不能定时添加")
- continue
-
- c = await gewe_service.get_wxchat_config_from_cache_async(wxid)
- contacts = await gewe_service.get_contacts_brief_from_cache_async(wxid)
-
- contact_wxids = [c.get('userName') for c in contacts]
- chatrooms = c.get('addContactsFromChatroomIdWhiteList', [])
-
- for chatroom_id in chatrooms:
- chatroom = await gewe_service.get_group_info_from_cache_async(wxid, chatroom_id)
- chatroom_member=await gewe_service.get_group_members_from_cache_async(wxid, chatroom_id)
-
- chatroom_nickname = chatroom.get('nickName')
- chatroom_owner_wxid = chatroom_member.get('chatroomOwner', None)
-
- admin_wxids = chatroom_member.get('adminWxid', [])
-
- admin_wxids = chatroom_member.get('adminWxid')
- if admin_wxids is None:
- admin_wxids = []
-
-
- is_add_group_200_times = await gewe_service.is_group_add_contacts_history_one_day_200_async(wxid, chatroom_id)
- if is_add_group_200_times:
- logger.info(f"{wxid}在 {chatroom_nickname} 群成员已经加了200个好友,不再添加,群id:{chatroom_id}")
- continue
-
- logger.info(f'{chatroom_nickname} 的群主是 {chatroom_owner_wxid},管理员是{admin_wxids}')
- contact_wxids_set = set(contact_wxids)
-
-
- contact_wxids_set.update(admin_wxids)
- if chatroom_owner_wxid is not None:
- contact_wxids_set.add(chatroom_owner_wxid)
-
- contact_wxids_set.add(wxid)
-
-
-
-
- chatroot_member_list = chatroom.get('memberList', [])
- remaining_chatroot_members = [x for x in chatroot_member_list if x.get('wxid') not in contact_wxids_set]
-
- nickname = next((member['nickName'] for member in chatroot_member_list if member['wxid'] == wxid), None)
-
- logger.info(f'{nickname}-{wxid} 在 {chatroom_nickname} 群里还可以邀请的好友有:{[x.get("nickName") for x in remaining_chatroot_members]}')
- for m in remaining_chatroot_members:
- contact_wxid= m.get('wxid')
- member_nickname=m.get("nickName")
- group_add_contacts_history = await gewe_service.get_group_add_contacts_history_async(wxid,chatroom_id,contact_wxid)
- sorted_history = sorted(group_add_contacts_history, key=lambda x: x.addTime, reverse=True)
-
-
- if len(sorted_history)==2:
- logger.info(f'{nickname}-{wxid}在{chatroom_nickname}-{chatroom_id} 群的 {member_nickname}-{contact_wxid} 已经邀请过2次,不再邀请')
- continue
-
-
- if len(sorted_history) > 0:
- last_add_time = sorted_history[0].addTime
- def is_add_time_more_than_one_day(addTime: int) -> bool:
- """
- 判断 addTime 是否与当前时间相隔大于 3600 × 24 秒
- :param addTime: Unix 时间戳
- :return: 如果 addTime 与当前时间相隔大于 3600 × 24 秒,返回 True;否则返回 False
- """
-
- current_time = time.time()
-
-
- time_difference = abs(current_time - addTime)
-
-
- return time_difference > 3600 * 24
-
- is_more_than_one_day= is_add_time_more_than_one_day(last_add_time)
-
- if not is_more_than_one_day:
- logger.info(f'{nickname}-{wxid}在{chatroom_nickname}-{chatroom_id} 群的{member_nickname}-{contact_wxid}已经当天邀请,不再邀请')
- continue
-
-
-
- ret, msg, data = await gewe_service.add_group_member_as_friend_async(token_id, app_id, chatroom_id, m.get('wxid'), f'我是群聊"{chatroom_nickname}"群的{nickname}')
- if ret==200:
-
- history=AddGroupContactsHistory.model_validate({
- "chatroomId":chatroom_id,
- "wxid":wxid,
- "contactWixd":contact_wxid,
- "addTime":int(time.time())
- })
- await gewe_service.save_group_add_contacts_history_async(wxid,chatroom_id,contact_wxid,history)
- else:
- logger.info(f'群好友邀请失败原因:{data}')
- logger.info(f'{nickname} 向 {chatroom_nickname}-{chatroom_id} 群的 {m.get("nickName")}-{m.get("wxid")} 发送好友邀请 {msg}')
- await asyncio.sleep(random.uniform(1.5, 3))
- await asyncio.sleep(random.uniform(1.5, 3))
-
-
- except Exception as e:
- logger.error(f"任务执行过程中发生异常: {e}")
-
- loop = asyncio.get_event_loop()
- if loop.is_closed():
- loop = asyncio.new_event_loop()
- asyncio.set_event_loop(loop)
-
- loop.run_until_complete(task())
-
-
- REDIS_KEY_PATTERN = "friend_add_limit:{date}"
- REDIS_LAST_RUN_KEY = "last_run_time:add_friends_task"
-
- @celery_app.task(name='tasks.add_friends_task', bind=True, acks_late=True)
- def add_friends_task(self,redis_config):
- """
- 限制每天最多 15 个,每 2 小时最多 8 个
- """
- async def task():
- redis_service = RedisService()
- await redis_service.init(**redis_config)
- today_str = datetime.datetime.now().strftime("%Y%m%d")
- redis_key = REDIS_KEY_PATTERN.format(date=today_str)
-
-
- total_added = await redis_service.get_hash_field(redis_key, "total") or 0
- last_2h_added =await redis_service.get_hash_field(redis_key, "last_2h") or 0
-
- total_added = int(total_added)
- last_2h_added = int(last_2h_added)
-
- logger.info(f"当前添加好友总数: {total_added}, 过去2小时添加: {last_2h_added}")
-
-
- if total_added >= 15:
- logger.warning("今日好友添加已达上限!")
- return
-
- if last_2h_added >= 8:
- logger.warning("过去2小时添加已达上限!")
- return
-
-
- max_add = min(15 - total_added, 8 - last_2h_added)
- if max_add <= 0:
- return
-
- num_to_add = min(max_add, 1)
- logger.info(f"本次添加 {num_to_add} 位好友")
-
-
-
-
- success = num_to_add
-
-
- if success > 0:
- await redis_service.increment_hash_field(redis_key, "total", success)
- await redis_service.increment_hash_field(redis_key, "last_2h", success)
-
-
- await redis_service.expire(redis_key, 86400)
- await redis_service.expire_field(redis_key, "last_2h", 7200)
-
- logger.info(f"成功添加 {success} 位好友, 今日总数 {total_added + success}")
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- loop = asyncio.get_event_loop()
-
- if loop.is_closed():
- loop = asyncio.new_event_loop()
- asyncio.set_event_loop(loop)
-
- loop.run_until_complete(task())
-
-
- @celery_app.task(name='tasks.random_scheduled_task', bind=True, acks_late=True)
- def random_scheduled_task(self,):
- print(f"Task executed at {datetime.datetime.now()}")
-
- next_run_in = random.randint(10, 60)
- print(f"Next execution will be in {next_run_in} seconds")
-
-
- entry = RedBeatSchedulerEntry(
- name='random-task',
- task='tasks.random_scheduled_task',
- schedule=timedelta(seconds=next_run_in),
- app=celery_app
- )
- entry.save()
- return f"Scheduled next run in {next_run_in} seconds"
-
|