run_bot.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2022/8/9
  4. # import datetime
  5. import datetime
  6. import os
  7. import sys
  8. import time
  9. sys.path.append(os.getcwd())
  10. from main.common import Common
  11. from main.feishu_lib import Feishu
  12. class Bot:
  13. # 获取各个爬虫表最新一条抓取时间
  14. @classmethod
  15. def get_first_time(cls, log_type, crawler):
  16. try:
  17. if crawler == "xiaoniangao":
  18. sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "yatRv2")
  19. # print(sheet[1])
  20. # 已下载表,最新一条视频抓取时间
  21. first_download_time = sheet[1][5]
  22. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  23. elif crawler == "kanyikan":
  24. sheet = Feishu.get_values_batch(log_type, "kanyikan", "20ce0c")
  25. # print(sheet[1])
  26. # 已下载表,最新一条视频抓取时间
  27. first_download_time = sheet[1][5]
  28. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  29. elif crawler == "music_album":
  30. sheet = Feishu.get_values_batch(log_type, "music_album", "f5a76e")
  31. # print(sheet[1])
  32. # 已下载表,最新一条视频抓取时间
  33. first_download_time = sheet[1][5]
  34. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  35. elif crawler == "bszf":
  36. sheet = Feishu.get_values_batch(log_type, "bszf", "440018")
  37. # print(sheet[1])
  38. # 已下载表,最新一条视频抓取时间
  39. first_download_time = sheet[1][4]
  40. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  41. elif crawler == "kuaishou":
  42. sheet = Feishu.get_values_batch(log_type, "kuaishou", "3cd128")
  43. # print(sheet[1])
  44. # 已下载表,最新一条视频抓取时间
  45. first_download_time = sheet[1][5]
  46. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  47. elif crawler == "gzh":
  48. sheet = Feishu.get_values_batch(log_type, "gzh", "fCs3BT")
  49. # print(sheet[1])
  50. # 已下载表,最新一条视频抓取时间
  51. first_download_time = sheet[1][3]
  52. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  53. elif crawler == "weiqun":
  54. sheet = Feishu.get_values_batch(log_type, "weiqun", "3cd128")
  55. # print(sheet[1])
  56. # 已下载表,最新一条视频抓取时间
  57. first_download_time = sheet[1][5]
  58. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  59. else:
  60. sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "yatRv2")
  61. # 已下载表,最新一条视频抓取时间
  62. first_download_time = sheet[1][5]
  63. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  64. return first_download_time
  65. except Exception as e:
  66. Common.logger(log_type).error("get_first_time异常:{}", e)
  67. # 获取各个爬虫的 feeds 表
  68. @classmethod
  69. def get_feeds_sheet(cls, log_type, crawler, sheet):
  70. try:
  71. if crawler == "kanyikan" and sheet == "recommend":
  72. feeds_sheet = Feishu.get_values_batch(log_type, "kanyikan", "SdCHOM")
  73. elif crawler == "kanyikan" and sheet == "moment":
  74. feeds_sheet = Feishu.get_values_batch(log_type, "kanyikan", "tGqZMX")
  75. elif crawler == "xiaoniangao" and sheet == "hour":
  76. feeds_sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "ba0da4")
  77. elif crawler == "xiaoniangao" and sheet == "person":
  78. feeds_sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "k6ldje")
  79. elif crawler == "music_album" and sheet == "recommend":
  80. feeds_sheet = Feishu.get_values_batch(log_type, "music_album", "69UxPo")
  81. elif crawler == "bszf" and sheet == "recommend":
  82. feeds_sheet = Feishu.get_values_batch(log_type, "bszf", "CcHgO7")
  83. elif crawler == "kuaishou" and sheet == "recommend":
  84. feeds_sheet = Feishu.get_values_batch(log_type, "kuaishou", "JK6npf")
  85. elif crawler == "kuaishou" and sheet == "follow":
  86. feeds_sheet = Feishu.get_values_batch(log_type, "kuaishou", "wW5cyb")
  87. elif crawler == "gzh" and sheet == "recommend":
  88. feeds_sheet = Feishu.get_values_batch(log_type, "gzh", "zWKFGb")
  89. else:
  90. feeds_sheet = "请输入{crawler}和{sheet}"
  91. return feeds_sheet
  92. except Exception as e:
  93. Common.logger(log_type).error("get_feeds_sheet异常:{}", e)
  94. # feeds_sheet表报警:连续 2 小时无数据
  95. @classmethod
  96. def rebot_feeds_sheet(cls, log_type, crawler, sheet):
  97. """
  98. 每隔一分钟获取一次表数据的数量:
  99. 1.中途有数据时,退出此次监控
  100. 2.连续2小时无数据时,触发机器人报警
  101. """
  102. # kanyikan_recommend_sheet = Feishu.get_values_batch(log_type, "kanyikan", "SdCHOM")
  103. # kanyikan_moment_sheet = Feishu.get_values_batch(log_type, "kanyikan", "tGqZMX")
  104. # xiaoniangao_hour_sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "ba0da4")
  105. # xiaoniangao_person_sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "k6ldje")
  106. # music_album_recommend_sheet = Feishu.get_values_batch(log_type, "music_album", "69UxPo")
  107. # bszf_recommend_sheet = Feishu.get_values_batch(log_type, "bszf", "CcHgO7")
  108. # kuaishou_recommend_sheet = Feishu.get_values_batch(log_type, "kuaishou", "JK6npf")
  109. # kuaishou_follow_sheet = Feishu.get_values_batch(log_type, "kuaishou", "wW5cyb")
  110. # gzh_recommend_sheet = Feishu.get_values_batch(log_type, "gzh", "zWKFGb")
  111. for i in range(120):
  112. if len(cls.get_feeds_sheet(log_type, crawler, sheet)) > 1:
  113. break
  114. else:
  115. time.sleep(60)
  116. if i == 119 and crawler == "kanyikan" and sheet == "recommend":
  117. Feishu.bot(log_type, "kanyikan", "看一看推荐榜表,已经 2 小时无数据了😤")
  118. elif i == 119 and crawler == "kanyikan" and sheet == "moment":
  119. Feishu.bot(log_type, "kanyikan", "看一看朋友圈表,已经 2 小时无数据了😤")
  120. elif i == 119 and crawler == "xiaoniangao" and sheet == "person":
  121. Feishu.bot(log_type, "xiaoniangao", "小年糕用户主页表,已经 2 小时无数据了😤")
  122. elif i == 119 and crawler == "music_album" \
  123. and sheet == "recommend" and datetime.datetime.now().hour < 13:
  124. Feishu.bot(log_type, "music_album", "音乐相册推荐表,已经 2 小时无数据了😤")
  125. elif i == 119 and crawler == "bszf" and sheet == "recommend" and datetime.datetime.now().hour < 13:
  126. Feishu.bot(log_type, "bszf", "本山祝福推荐表,已经 2 小时无数据了😤")
  127. elif i == 119 and crawler == "kuaishou" and sheet == "recommend":
  128. Feishu.bot(log_type, "kuaishou", "快手推荐表,已经 2 小时无数据了😤")
  129. elif i == 119 and crawler == "kuaishou" and sheet == "follow":
  130. Feishu.bot(log_type, "kuaishou", "快手关注表,已经 2 小时无数据了😤")
  131. elif i == 119 and crawler == "gzh" and sheet == "recommend":
  132. Feishu.bot(log_type, "gzh", "公众号推荐表,已经 2 小时无数据了😤")
  133. # 触发机器人报警:超过24小时没有新入库的视频
  134. @classmethod
  135. def robot_download_sheet(cls, log_type, crawler, duration):
  136. """
  137. 已下载视频表:超过24小时没有新入库的视频
  138. """
  139. try:
  140. # 看一看爬虫报警
  141. if crawler == "kanyikan" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  142. Feishu.bot(log_type, crawler, "看一看已下载表,超过24小时没有新视频入库了😤")
  143. Common.logger(log_type).info("看一看已下载表,超过24小时没有新视频入库了😤\n")
  144. # 小年糕爬虫报警
  145. elif crawler == "xiaoniangao" and (
  146. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  147. Feishu.bot(log_type, crawler, "小年糕已下载表,超过24小时没有新视频入库了😤")
  148. Common.logger(log_type).info("小年糕已下载表,超过24小时没有新视频入库了😤\n")
  149. # 音乐相册爬虫报警
  150. elif crawler == "music_album" and (
  151. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  152. Feishu.bot(log_type, crawler, "音乐相册已下载表,超过24小时没有新视频入库了😤")
  153. Common.logger(log_type).info("音乐相册已下载表,超过24小时没有新视频入库了😤\n")
  154. # 本山祝福爬虫报警
  155. elif crawler == "bszf" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  156. Feishu.bot(log_type, crawler, "本山祝福已下载表,超过24小时没有新视频入库了😤")
  157. Common.logger(log_type).info("本山祝福已下载表,超过24小时没有新视频入库了😤\n")
  158. # 快手爬虫报警
  159. elif crawler == "kuaishou" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  160. Feishu.bot(log_type, crawler, "快手已下载表,超过24小时没有新视频入库了😤")
  161. Common.logger(log_type).info("快手已下载表,超过24小时没有新视频入库了😤\n")
  162. # 公众号爬虫报警
  163. elif crawler == "gzh" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  164. Feishu.bot(log_type, crawler, "公众号已下载表,超过24小时没有新视频入库了😤")
  165. Common.logger(log_type).info("公众号已下载表,超过24小时没有新视频入库了😤\n")
  166. # 微群视频爬虫报警
  167. elif crawler == "weiqun" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  168. Feishu.bot(log_type, crawler, "微群视频已下载表,超过24小时没有新视频入库了😤")
  169. Common.logger(log_type).info("微群视频已下载表,超过24小时没有新视频入库了😤\n")
  170. except Exception as e:
  171. Common.logger(log_type).error("robot_alarm异常:{}", e)
  172. # 监控运行入口
  173. @classmethod
  174. def main(cls):
  175. """
  176. 每隔一小时,检查一次已下载表;
  177. 已下载表的最新一条数据抓取时间,距当前时间超过 24 小时,则触发机器人报警,发送飞书报警消息
  178. """
  179. # 已下载表,超过 24 小时无新视频入库报警
  180. duration = 3600 * 24
  181. while True:
  182. if 21 > datetime.datetime.now().hour >= 10:
  183. Common.logger("bot").info("监控看一看已下载表")
  184. Bot.robot_download_sheet("bot", "kanyikan", duration)
  185. Common.logger("bot").info("监控小年糕已下载表")
  186. Bot.robot_download_sheet("bot", "xiaoniangao", duration)
  187. Common.logger("bot").info("监控音乐相册已下载表")
  188. Bot.robot_download_sheet("bot", "music_album", duration)
  189. Common.logger("bot").info("监控本山祝福已下载表")
  190. Bot.robot_download_sheet("bot", "bszf", duration)
  191. Common.logger("bot").info("监控快手已下载表")
  192. Bot.robot_download_sheet("bot", "kuaishou", duration)
  193. Common.logger("bot").info("监控微群视频已下载表")
  194. Bot.robot_download_sheet("bot", "weiqun", duration)
  195. # Common.logger("bot").info("监控公众号已下载表")
  196. # Bot.robot_alarm("bot", "gzh", duration)
  197. Common.del_logs("bot")
  198. Common.logger("bot").info("休眠 1 小时")
  199. time.sleep(3600)
  200. else:
  201. Common.logger("bot").info("今日监控完毕\n")
  202. if __name__ == "__main__":
  203. # Bot.get_feeds_sheet("bot", "gzh", "recommend")
  204. # Bot.rebot_feeds_sheet("bot", "gzh", "recommend")
  205. # Bot.robot_download_sheet("bot", "weiqun", 10)
  206. Bot.main()