Selaa lähdekoodia

新增抓公众号粉丝

luojunhui 2 kuukautta sitten
vanhempi
commit
7e264dcc36

+ 5 - 0
applications/api/async_feishu_api.py

@@ -21,6 +21,9 @@ class Feishu:
     # 长文任务报警群
     long_articles_task_bot = "https://open.feishu.cn/open-apis/bot/v2/hook/223b3d72-f2e8-40e0-9b53-6956e0ae7158"
 
+    # cookie 监测机器人
+    cookie_monitor_bot = "https://open.feishu.cn/open-apis/bot/v2/hook/51b9c83a-f50d-44dd-939f-bcd10ac6c55a"
+
     def __init__(self):
         self.token = None
         self.headers = {"Content-Type": "application/json"}
@@ -210,6 +213,8 @@ class FeishuBotApi(Feishu):
                 url = self.server_account_publish_monitor_bot
             case "long_articles_task":
                 url = self.long_articles_task_bot
+            case "cookie_monitor":
+                url = self.cookie_monitor_bot
             case _:
                 url = self.long_articles_bot_dev
 

+ 22 - 3
applications/tasks/crawler_tasks/crawler_gzh_fans.py

@@ -5,6 +5,7 @@ from applications.crawler.wechat import (
     get_access_token,
     get_union_id_batch,
 )
+from applications.api import feishu_robot
 from applications.utils import run_tasks_with_asyncio_task_group
 
 
@@ -109,6 +110,9 @@ class CrawlerGzhFans(CrawlerGzhFansBase):
     # 抓取单个账号的粉丝
     async def crawl_fans_for_each_account(self, account_info):
         cookie_obj = await self.get_cookie_token_from_database(account_info["gh_id"])
+        if not cookie_obj:
+            return
+
         if not account_info.get("cursor_openid"):
             cursor_openid = ''
         else:
@@ -126,7 +130,6 @@ class CrawlerGzhFans(CrawlerGzhFansBase):
             cursor_timestamp=cursor_timestamp,
         )
         base_resp = response.get("base_resp", {})
-        # print(json.dumps(base_resp, indent=4, ensure_ascii=False))
         code = base_resp.get("ret")
         error_msg = base_resp.get("err_msg")
 
@@ -135,14 +138,30 @@ class CrawlerGzhFans(CrawlerGzhFansBase):
                 user_list = response.get("user_list", {}).get("user_info_list")
                 next_cursor_id = user_list[-1].get("user_openid")
                 next_cursor_timestamp = user_list[-1].get("user_create_time")
-                # print(json.dumps(user_list, ensure_ascii=False, indent=4))
                 await self.insert_gzh_fans_batch(account_info, user_list)
                 await self.update_gzh_cursor_info(
                     account_info["gh_id"], next_cursor_id, next_cursor_timestamp
                 )
 
+            case '00040':
+                print(f"token 非法: {error_msg}")
+                await self.set_cookie_token_as_invalid(account_info["gh_id"])
+                await feishu_robot.bot(
+                    title=f"{account_info['account_name']}的 token && cookie 失效,请及时更新",
+                    detail=account_info,
+                    env="cookie_monitor_bot",
+                    mention=False,
+                )
+
             case _:
-                print(code, error_msg)
+                print("token 异常, 请及时刷新")
+                await self.set_cookie_token_as_invalid(account_info["gh_id"])
+                await feishu_robot.bot(
+                    title=f"{account_info['account_name']}的 token && cookie 失效,请及时更新",
+                    detail=account_info,
+                    env="cookie_monitor_bot",
+                    mention=False,
+                )
 
     # 通过 access_token && open_id 抓取 union_id
     async def get_union_ids_for_each_account(self, account_info: dict):