run_bot.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. else:
  54. sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "yatRv2")
  55. # 已下载表,最新一条视频抓取时间
  56. first_download_time = sheet[1][5]
  57. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  58. return first_download_time
  59. except Exception as e:
  60. Common.logger(log_type).error("get_first_time异常:{}", e)
  61. # 获取各个爬虫的 feeds 表
  62. @classmethod
  63. def get_feeds_sheet(cls, log_type, crawler, sheet):
  64. try:
  65. if crawler == "kanyikan" and sheet == "recommend":
  66. feeds_sheet = Feishu.get_values_batch(log_type, "kanyikan", "SdCHOM")
  67. elif crawler == "kanyikan" and sheet == "moment":
  68. feeds_sheet = Feishu.get_values_batch(log_type, "kanyikan", "tGqZMX")
  69. elif crawler == "xiaoniangao" and sheet == "hour":
  70. feeds_sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "ba0da4")
  71. elif crawler == "xiaoniangao" and sheet == "person":
  72. feeds_sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "k6ldje")
  73. elif crawler == "music_album" and sheet == "recommend":
  74. feeds_sheet = Feishu.get_values_batch(log_type, "music_album", "69UxPo")
  75. elif crawler == "bszf" and sheet == "recommend":
  76. feeds_sheet = Feishu.get_values_batch(log_type, "bszf", "CcHgO7")
  77. elif crawler == "kuaishou" and sheet == "recommend":
  78. feeds_sheet = Feishu.get_values_batch(log_type, "kuaishou", "JK6npf")
  79. elif crawler == "kuaishou" and sheet == "follow":
  80. feeds_sheet = Feishu.get_values_batch(log_type, "kuaishou", "wW5cyb")
  81. elif crawler == "gzh" and sheet == "recommend":
  82. feeds_sheet = Feishu.get_values_batch(log_type, "gzh", "zWKFGb")
  83. else:
  84. feeds_sheet = "请输入{crawler}和{sheet}"
  85. return feeds_sheet
  86. except Exception as e:
  87. Common.logger(log_type).error("get_feeds_sheet异常:{}", e)
  88. # feeds_sheet表报警:连续 2 小时无数据
  89. @classmethod
  90. def rebot_feeds_sheet(cls, log_type, crawler, sheet):
  91. """
  92. 每隔一分钟获取一次表数据的数量:
  93. 1.中途有数据时,退出此次监控
  94. 2.连续2小时无数据时,触发机器人报警
  95. """
  96. # kanyikan_recommend_sheet = Feishu.get_values_batch(log_type, "kanyikan", "SdCHOM")
  97. # kanyikan_moment_sheet = Feishu.get_values_batch(log_type, "kanyikan", "tGqZMX")
  98. # xiaoniangao_hour_sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "ba0da4")
  99. # xiaoniangao_person_sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "k6ldje")
  100. # music_album_recommend_sheet = Feishu.get_values_batch(log_type, "music_album", "69UxPo")
  101. # bszf_recommend_sheet = Feishu.get_values_batch(log_type, "bszf", "CcHgO7")
  102. # kuaishou_recommend_sheet = Feishu.get_values_batch(log_type, "kuaishou", "JK6npf")
  103. # kuaishou_follow_sheet = Feishu.get_values_batch(log_type, "kuaishou", "wW5cyb")
  104. # gzh_recommend_sheet = Feishu.get_values_batch(log_type, "gzh", "zWKFGb")
  105. for i in range(120):
  106. if len(cls.get_feeds_sheet(log_type, crawler, sheet)) > 1:
  107. break
  108. else:
  109. time.sleep(60)
  110. if i == 119 and crawler == "kanyikan" and sheet == "recommend":
  111. Feishu.bot(log_type, "kanyikan", "看一看推荐榜表,已经 2 小时无数据了😤")
  112. elif i == 119 and crawler == "kanyikan" and sheet == "moment":
  113. Feishu.bot(log_type, "kanyikan", "看一看朋友圈表,已经 2 小时无数据了😤")
  114. elif i == 119 and crawler == "xiaoniangao" and sheet == "person":
  115. Feishu.bot(log_type, "xiaoniangao", "小年糕用户主页表,已经 2 小时无数据了😤")
  116. elif i == 119 and crawler == "music_album" \
  117. and sheet == "recommend" and datetime.datetime.now().hour < 13:
  118. Feishu.bot(log_type, "music_album", "音乐相册推荐表,已经 2 小时无数据了😤")
  119. elif i == 119 and crawler == "bszf" and sheet == "recommend" and datetime.datetime.now().hour < 13:
  120. Feishu.bot(log_type, "bszf", "本山祝福推荐表,已经 2 小时无数据了😤")
  121. elif i == 119 and crawler == "kuaishou" and sheet == "recommend":
  122. Feishu.bot(log_type, "kuaishou", "快手推荐表,已经 2 小时无数据了😤")
  123. elif i == 119 and crawler == "kuaishou" and sheet == "follow":
  124. Feishu.bot(log_type, "kuaishou", "快手关注表,已经 2 小时无数据了😤")
  125. elif i == 119 and crawler == "gzh" and sheet == "recommend":
  126. Feishu.bot(log_type, "gzh", "公众号推荐表,已经 2 小时无数据了😤")
  127. # 触发机器人报警:超过24小时没有新入库的视频
  128. @classmethod
  129. def robot_download_sheet(cls, log_type, crawler, duration):
  130. """
  131. 已下载视频表:超过24小时没有新入库的视频
  132. """
  133. try:
  134. if crawler == "kanyikan" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  135. Feishu.bot(log_type, crawler, "看一看已下载表,超过24小时没有新视频入库了😤")
  136. Common.logger(log_type).info("看一看已下载表,超过24小时没有新视频入库了😤\n")
  137. elif crawler == "xiaoniangao" and (
  138. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  139. Feishu.bot(log_type, crawler, "小年糕已下载表,超过24小时没有新视频入库了😤")
  140. Common.logger(log_type).info("小年糕已下载表,超过24小时没有新视频入库了😤\n")
  141. elif crawler == "music_album" and (
  142. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  143. Feishu.bot(log_type, crawler, "音乐相册已下载表,超过24小时没有新视频入库了😤")
  144. Common.logger(log_type).info("音乐相册已下载表,超过24小时没有新视频入库了😤\n")
  145. elif crawler == "bszf" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  146. Feishu.bot(log_type, crawler, "本山祝福已下载表,超过24小时没有新视频入库了😤")
  147. Common.logger(log_type).info("本山祝福已下载表,超过24小时没有新视频入库了😤\n")
  148. elif crawler == "kuaishou" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  149. Feishu.bot(log_type, crawler, "快手已下载表,超过24小时没有新视频入库了😤")
  150. Common.logger(log_type).info("快手已下载表,超过24小时没有新视频入库了😤\n")
  151. elif crawler == "gzh" and (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. except Exception as e:
  155. Common.logger(log_type).error("robot_alarm异常:{}", e)
  156. # 监控运行入口
  157. @classmethod
  158. def main(cls):
  159. """
  160. 每隔一小时,检查一次已下载表;
  161. 已下载表的最新一条数据抓取时间,距当前时间超过 24 小时,则触发机器人报警,发送飞书报警消息
  162. """
  163. # 已下载表,超过 24 小时无新视频入库报警
  164. duration = 3600 * 24
  165. while True:
  166. if 21 > datetime.datetime.now().hour >= 10:
  167. Common.logger("bot").info("监控看一看已下载表")
  168. Bot.robot_download_sheet("bot", "kanyikan", duration)
  169. Common.logger("bot").info("监控小年糕已下载表")
  170. Bot.robot_download_sheet("bot", "xiaoniangao", duration)
  171. Common.logger("bot").info("监控音乐相册已下载表")
  172. Bot.robot_download_sheet("bot", "music_album", duration)
  173. Common.logger("bot").info("监控本山祝福已下载表")
  174. Bot.robot_download_sheet("bot", "bszf", duration)
  175. Common.logger("bot").info("监控快手已下载表")
  176. Bot.robot_download_sheet("bot", "kuaishou", duration)
  177. # Common.logger("bot").info("监控公众号已下载表")
  178. # Bot.robot_alarm("bot", "gzh", duration)
  179. Common.del_logs("bot")
  180. Common.logger("bot").info("休眠 1 小时")
  181. time.sleep(3600)
  182. else:
  183. Common.logger("bot").info("今日监控完毕\n")
  184. if __name__ == "__main__":
  185. # Bot.get_feeds_sheet("bot", "gzh", "recommend")
  186. # Bot.rebot_feeds_sheet("bot", "gzh", "recommend")
  187. Bot.main()