outside_gzh_articles_monitor.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import time
  2. from tqdm import tqdm
  3. from pymysql.cursors import DictCursor
  4. from applications.api import FeishuBotApi
  5. from applications.db import DatabaseConnector
  6. from cold_start.crawler.wechat import get_article_detail
  7. from cold_start.crawler.wechat import get_article_list_from_account
  8. from config import long_articles_config, denet_config
  9. class OutsideGzhArticlesManager:
  10. def __init__(self):
  11. self.long_articles_client = DatabaseConnector(long_articles_config)
  12. self.long_articles_client.connect()
  13. self.denet_client = DatabaseConnector(denet_config)
  14. self.denet_client.connect()
  15. self.feishu_bot_api = FeishuBotApi()
  16. def update_article_illegal_status(self, article_id, illegal_reason):
  17. update_query = f"""
  18. update outside_gzh_account_monitor
  19. set illegal_status = %s, illegal_reason = %s
  20. where id = %s and illegal_reason = %s
  21. """
  22. self.long_articles_client.save(
  23. query=update_query,
  24. params=(1, illegal_reason, article_id, 0)
  25. )
  26. class OutsideGzhArticlesCollector(OutsideGzhArticlesManager):
  27. def fetch_outside_account_list(self):
  28. fetch_query = f"""
  29. select
  30. t2.group_source_name as account_source,
  31. t3.name as account_name,
  32. t3.gh_id as gh_id,
  33. t3.status as status
  34. from wx_statistics_group_source t1
  35. join wx_statistics_group_source_account t2 on t2.group_source_name = t1.account_source_name
  36. join publish_account t3 on t3.id = t2.account_id
  37. where
  38. t1.mode_type = '代运营服务号';
  39. """
  40. return self.denet_client.fetch(query=fetch_query, cursor_type=DictCursor)
  41. def fetch_each_account(self, account: dict):
  42. gh_id = account["gh_id"]
  43. fetch_response = get_article_list_from_account(gh_id)
  44. msg_list = fetch_response.get("data", {}).get("data", [])
  45. if msg_list:
  46. for msg in tqdm(msg_list, desc=f"insert account {account['account_name']}"):
  47. self.save_each_msg_to_db(msg, account)
  48. else:
  49. print(f"crawler failed: {account['account_name']}")
  50. def save_each_msg_to_db(self, msg: dict, account: dict):
  51. base_info = msg["AppMsg"]["BaseInfo"]
  52. detail_info = msg["AppMsg"]["DetailInfo"]
  53. app_msg_id = base_info["AppMsgId"]
  54. create_timestamp = base_info["CreateTime"]
  55. publish_type = base_info["Type"]
  56. # insert each article
  57. for article in detail_info:
  58. link = article["ContentUrl"]
  59. article_detail = get_article_detail(link)
  60. response_code = article_detail["code"]
  61. if response_code == 25012:
  62. illegal_reason = article_detail.get("msg")
  63. # bot and return
  64. self.feishu_bot_api.bot(
  65. title="文章违规告警",
  66. detail={
  67. "account_name": article["account_name"],
  68. "title": article['title'],
  69. "reason": illegal_reason,
  70. "publish_timestamp": create_timestamp,
  71. "account_source": article["account_source"]
  72. },
  73. env="dev"
  74. )
  75. elif response_code == 0:
  76. insert_query = f"""
  77. insert ignore into outside_gzh_account_monitor
  78. (account_name, gh_id, account_source, account_type, app_msg_id, publish_type, position, title, link,
  79. channel_content_id, crawler_timestamp, publish_timestamp)
  80. values
  81. (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
  82. """
  83. self.long_articles_client.save(
  84. query=insert_query,
  85. params=(
  86. account["account_name"],
  87. account["gh_id"],
  88. account["account_source"],
  89. "服务号",
  90. app_msg_id,
  91. publish_type,
  92. article["ItemIndex"],
  93. article["Title"],
  94. link,
  95. article_detail["data"]["data"]["channel_content_id"],
  96. int(time.time()),
  97. int(article_detail["data"]["data"]["publish_timestamp"] / 1000),
  98. ),
  99. )
  100. else:
  101. continue
  102. def deal(self):
  103. account_list = self.fetch_outside_account_list()
  104. for account in tqdm(account_list[:10]):
  105. self.fetch_each_account(account)
  106. class OutsideGzhArticlesMonitor(OutsideGzhArticlesManager):
  107. def fetch_article_list_to_check(self):
  108. publish_timestamp_threshold = int(time.time()) - 7 * 24 * 3600
  109. fetch_query = f"""
  110. select id, account_name, gh_id, account_source, account_type,
  111. title, link, from_unixtime(publish_timestamp) as publish_date
  112. from outside_gzh_account_monitor
  113. where illegal_status = 0 and publish_timestamp > {publish_timestamp_threshold};
  114. """
  115. return self.long_articles_client.fetch(
  116. query=fetch_query, cursor_type=DictCursor
  117. )
  118. def check_each_article(self, article: dict):
  119. """
  120. check each article
  121. """
  122. link = article["link"]
  123. article_detail = get_article_detail(link)
  124. response_code = article_detail["code"]
  125. if response_code == 25012:
  126. illegal_reason = article_detail.get("msg")
  127. self.feishu_bot_api.bot(
  128. title="文章违规告警",
  129. detail={
  130. "account_name": article["account_name"],
  131. "title": article['title'],
  132. "reason": illegal_reason,
  133. "publish_date": article["publish_date"],
  134. "account_source": article["account_source"]
  135. },
  136. env="dev"
  137. )
  138. article_id = article["id"]
  139. self.update_article_illegal_status(article_id, illegal_reason)
  140. else:
  141. return
  142. def deal(self):
  143. article_list = self.fetch_article_list_to_check()
  144. for article in tqdm(article_list):
  145. self.check_each_article(article)