Przeglądaj źródła

优化更新每日发送文章code

luojunhui 4 miesięcy temu
rodzic
commit
cb85927b7a
2 zmienionych plików z 55 dodań i 3 usunięć
  1. 3 0
      applications/const.py
  2. 52 3
      updatePublishedMsgDaily.py

+ 3 - 0
applications/const.py

@@ -21,6 +21,7 @@ class updatePublishedMsgTaskConst:
     ARTICLE_ILLEGAL_CODE = 25012
     ARTICLE_DELETE_CODE = 25005
     ARTICLE_SUCCESS_CODE = 0
+    ARTICLE_UNKNOWN_CODE = 10000
 
     # 请求爬虫详情接口状态码
     # 记录默认状态
@@ -39,6 +40,8 @@ class updatePublishedMsgTaskConst:
     SUBSCRIBE_TYPE_SET = {0, 1}
     # 服务号
     SERVICE_TYPE = 2
+    # 监测周期(秒)
+    MONITOR_PERIOD = 60 * 60 * 24 * 3
 
 
 class updateAccountReadRateTaskConst:

+ 52 - 3
updatePublishedMsgDaily.py

@@ -12,12 +12,12 @@ from tqdm import tqdm
 from datetime import datetime
 from argparse import ArgumentParser
 
-
 from applications import PQMySQL, WeixinSpider, Functions, log, bot, aiditApi
 from applications.const import updatePublishedMsgTaskConst
 
 ARTICLE_TABLE = "official_articles_v2"
 const = updatePublishedMsgTaskConst()
+spider = WeixinSpider()
 
 
 def get_account_using_status():
@@ -586,6 +586,54 @@ def get_article_detail_job():
         )
 
 
+def monitor():
+    """
+    监控任务, 监测周期为3天,监测文章是否被违规,若监测到违规文章,则进行告警
+    :return:
+    """
+    try:
+        db_client = PQMySQL()
+    except Exception as e:
+        error_msg = traceback.format_exc()
+        bot(
+            title="监控任务连接数据库失败",
+            detail={
+                "job": "monitor",
+                "error": e,
+                "msg": error_msg
+            }
+        )
+        return
+
+    now_time = int(time.time())
+    monitor_start_timestamp = now_time - const.MONITOR_PERIOD
+    select_sql = f"""
+        SELECT ghId, accountName, title, ContentUrl, wx_sn
+        FROM {ARTICLE_TABLE}
+        WHERE publish_timestamp >= {monitor_start_timestamp};
+    """
+    article_list = db_client.select(select_sql)
+    for article in tqdm(article_list, desc="monitor article list"):
+        gh_id = article[0]
+        account_name = article[1]
+        title = article[2]
+        url = article[3]
+        wx_sn = article[4]
+        response = spider.get_article_text(url)
+        response_code = response['code']
+        if response_code == const.ARTICLE_ILLEGAL_CODE:
+            bot(
+                title="文章违规告警",
+                detail={
+                    "ghId": gh_id,
+                    "accountName": account_name,
+                    "title": title,
+                    "wx_sn": wx_sn
+                },
+                mention=False
+            )
+
+
 def main():
     """
     main
@@ -599,7 +647,7 @@ def main():
 if __name__ == '__main__':
     parser = ArgumentParser()
     parser.add_argument("--run_task",
-                        help="update: update_job, check: check_job, detail: get_article_detail_job")
+                        help="update: update_job, check: check_job, detail: get_article_detail_job, monitor: monitor")
     args = parser.parse_args()
 
     if args.run_task:
@@ -611,8 +659,9 @@ if __name__ == '__main__':
                 check_job()
             case "detail":
                 get_article_detail_job()
+            case "monitor":
+                monitor()
             case _:
                 print("No such task, input update: update_job, check: check_job, detail: get_article_detail_job")
     else:
         main()
-