run_bot.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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_hour":
  18. sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "yatRv2")
  19. # 已下载表,最新一条视频抓取时间
  20. first_download_time = sheet[1][5]
  21. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  22. elif crawler == "xiaoniangao_person":
  23. sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "Wu0CeL")
  24. # 已下载表,最新一条视频抓取时间
  25. first_download_time = sheet[1][5]
  26. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  27. elif crawler == "xiaoniangao_play":
  28. sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "c85k1C")
  29. # 已下载表,最新一条视频抓取时间
  30. first_download_time = sheet[1][5]
  31. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  32. elif crawler == 'xigua_video':
  33. sheet = Feishu.get_values_batch(log_type, "xigua", "e075e9")
  34. # 已下载表,最新一条视频抓取时间
  35. first_download_time = sheet[1][5]
  36. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  37. elif crawler == 'xigua_little_video':
  38. sheet = Feishu.get_values_batch(log_type, "xigua", "hDSDnv")
  39. # 已下载表,最新一条视频抓取时间
  40. first_download_time = sheet[1][5]
  41. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  42. elif crawler == 'zhihu_hot':
  43. sheet = Feishu.get_values_batch(log_type, "zhihu", "8871e3")
  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 == 'zhihu_follow':
  48. sheet = Feishu.get_values_batch(log_type, "zhihu", "4MGuux")
  49. # 已下载表,最新一条视频抓取时间
  50. first_download_time = sheet[1][5]
  51. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  52. elif crawler == 'jxxf':
  53. sheet = Feishu.get_values_batch(log_type, "jxxf", "d9e9b1")
  54. # 已下载表,最新一条视频抓取时间
  55. first_download_time = sheet[1][5]
  56. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  57. elif crawler == 'zmyx':
  58. sheet = Feishu.get_values_batch(log_type, "zmyx", "19c772")
  59. # 已下载表,最新一条视频抓取时间
  60. first_download_time = sheet[1][5]
  61. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  62. elif crawler == 'zhufumao':
  63. sheet = Feishu.get_values_batch(log_type, "zhufumao", "e13bdf")
  64. # 已下载表,最新一条视频抓取时间
  65. first_download_time = sheet[1][5]
  66. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  67. elif crawler == 'ssnnyfq':
  68. sheet = Feishu.get_values_batch(log_type, "ssnnyfq", "290bae")
  69. # 已下载表,最新一条视频抓取时间
  70. first_download_time = sheet[1][5]
  71. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  72. elif crawler == "kanyikan":
  73. sheet = Feishu.get_values_batch(log_type, "kanyikan", "20ce0c")
  74. # 已下载表,最新一条视频抓取时间
  75. first_download_time = sheet[1][5]
  76. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  77. elif crawler == "music_album":
  78. sheet = Feishu.get_values_batch(log_type, "music_album", "f5a76e")
  79. # 已下载表,最新一条视频抓取时间
  80. first_download_time = sheet[1][5]
  81. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  82. elif crawler == "bszf":
  83. sheet = Feishu.get_values_batch(log_type, "bszf", "440018")
  84. # 已下载表,最新一条视频抓取时间
  85. first_download_time = sheet[1][4]
  86. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  87. elif crawler == "kuaishou_recommend":
  88. sheet = Feishu.get_values_batch(log_type, "kuaishou", "3cd128")
  89. # 已下载表,最新一条视频抓取时间
  90. first_download_time = sheet[1][5]
  91. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  92. elif crawler == "kuaishou_follow":
  93. sheet = Feishu.get_values_batch(log_type, "kuaishou", "fYdA8F")
  94. # 已下载表,最新一条视频抓取时间
  95. first_download_time = sheet[1][5]
  96. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  97. elif crawler == "gzh":
  98. sheet = Feishu.get_values_batch(log_type, "gzh", "fCs3BT")
  99. # 已下载表,最新一条视频抓取时间
  100. first_download_time = sheet[1][3]
  101. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  102. elif crawler == "weiqun":
  103. sheet = Feishu.get_values_batch(log_type, "weiqun", "3cd128")
  104. # 已下载表,最新一条视频抓取时间
  105. first_download_time = sheet[1][5]
  106. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  107. elif crawler == "weishi":
  108. sheet = Feishu.get_values_batch(log_type, "weishi", "caa3fa")
  109. # 已下载表,最新一条视频抓取时间
  110. first_download_time = sheet[1][5]
  111. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  112. elif crawler == "shipinhao":
  113. sheet = Feishu.get_values_batch(log_type, "shipinhao", "c77cf9")
  114. # 已下载表,最新一条视频抓取时间
  115. first_download_time = sheet[1][5]
  116. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  117. else:
  118. sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "yatRv2")
  119. # 已下载表,最新一条视频抓取时间
  120. first_download_time = sheet[1][5]
  121. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  122. return first_download_time
  123. except Exception as e:
  124. Common.logger(log_type).error(f"get_first_time异常:{e}\n")
  125. # 触发机器人报警:超过24小时没有新入库的视频
  126. @classmethod
  127. def robot_download_sheet(cls, log_type, crawler, duration):
  128. """
  129. 已下载视频表:超过24小时没有新入库的视频
  130. """
  131. try:
  132. # 看一看爬虫报警
  133. if crawler == "kanyikan" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  134. Feishu.bot(log_type, crawler, "看一看已下载表,超过24小时没有新视频入库了😤")
  135. Common.logger(log_type).warning("看一看已下载表,超过24小时没有新视频入库了😤\n")
  136. # 小年糕爬虫报警
  137. elif crawler == "xiaoniangao_hour" 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).warning("小年糕_小时级_已下载表,超过24小时没有新视频入库了😤\n")
  141. elif crawler == "xiaoniangao_person" 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).warning("小年糕_用户主页_已下载表,超过24小时没有新视频入库了😤\n")
  145. elif crawler == "xiaoniangao_play" 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).warning("小年糕_播放量_已下载表,超过24小时没有新视频入库了😤\n")
  149. # 西瓜视频
  150. elif crawler == "xigua_video" 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).warning("西瓜视频_用户主页_已下载表,超过24小时没有新视频入库了😤\n")
  154. elif crawler == "xigua_little_video" and (
  155. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  156. Feishu.bot(log_type, crawler, "西瓜视频_小视频_已下载表,超过24小时没有新视频入库了😤")
  157. Common.logger(log_type).warning("西瓜视频_小视频_已下载表,超过24小时没有新视频入库了😤\n")
  158. # 知乎
  159. elif crawler == "zhihu_hot" and (
  160. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  161. Feishu.bot(log_type, crawler, "知乎_热门_已下载表,超过24小时没有新视频入库了😤")
  162. Common.logger(log_type).warning("知乎_热门_已下载表,超过24小时没有新视频入库了😤\n")
  163. elif crawler == "zhihu_follow" and (
  164. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  165. Feishu.bot(log_type, crawler, "知乎_定向_已下载表,超过24小时没有新视频入库了😤")
  166. Common.logger(log_type).warning("知乎_定向_已下载表,超过24小时没有新视频入库了😤\n")
  167. # 吉祥幸福
  168. elif crawler == "jxxf" and (
  169. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  170. Feishu.bot(log_type, crawler, "吉祥幸福_已下载表,超过24小时没有新视频入库了😤")
  171. Common.logger(log_type).warning("吉祥幸福_已下载表,超过24小时没有新视频入库了😤\n")
  172. # 众妙音信
  173. elif crawler == "zmyx" and (
  174. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  175. Feishu.bot(log_type, crawler, "众妙音信_已下载表,超过24小时没有新视频入库了😤")
  176. Common.logger(log_type).warning("众妙音信_已下载表,超过24小时没有新视频入库了😤\n")
  177. # 祝福猫视频
  178. elif crawler == "zhufumao" and (
  179. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  180. Feishu.bot(log_type, crawler, "祝福猫_已下载表,超过24小时没有新视频入库了😤")
  181. Common.logger(log_type).warning("祝福猫_已下载表,超过24小时没有新视频入库了😤\n")
  182. # 岁岁年年迎福气
  183. elif crawler == "ssnnyfq" and (
  184. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  185. Feishu.bot(log_type, crawler, "岁岁年年迎福气_已下载表,超过24小时没有新视频入库了😤")
  186. Common.logger(log_type).warning("岁岁年年迎福气_已下载表,超过24小时没有新视频入库了😤\n")
  187. # 音乐相册爬虫报警
  188. elif crawler == "music_album" and (
  189. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  190. Feishu.bot(log_type, crawler, "音乐相册已下载表,超过24小时没有新视频入库了😤")
  191. Common.logger(log_type).warning("音乐相册已下载表,超过24小时没有新视频入库了😤\n")
  192. # 本山祝福爬虫报警
  193. elif crawler == "bszf" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  194. Feishu.bot(log_type, crawler, "本山祝福已下载表,超过24小时没有新视频入库了😤")
  195. Common.logger(log_type).warning("本山祝福已下载表,超过24小时没有新视频入库了😤\n")
  196. # 快手爬虫报警
  197. elif crawler == "kuaishou_recommend" and (
  198. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  199. Feishu.bot(log_type, crawler, "快手_推荐榜_已下载表,超过24小时没有新视频入库了😤")
  200. Common.logger(log_type).warning("快手_推荐榜_已下载表,超过24小时没有新视频入库了😤\n")
  201. elif crawler == "kuaishou_follow" and (
  202. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  203. Feishu.bot(log_type, crawler, "快手_用户主页_已下载表,超过24小时没有新视频入库了😤")
  204. Common.logger(log_type).warning("快手_用户主页_已下载表,超过24小时没有新视频入库了😤\n")
  205. # 公众号爬虫报警
  206. elif crawler == "gzh" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  207. Feishu.bot(log_type, crawler, "公众号已下载表,超过24小时没有新视频入库了😤")
  208. Common.logger(log_type).warning("公众号已下载表,超过24小时没有新视频入库了😤\n")
  209. # 微群视频爬虫报警
  210. elif crawler == "weiqun" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  211. Feishu.bot(log_type, crawler, "微群视频已下载表,超过24小时没有新视频入库了😤")
  212. Common.logger(log_type).warning("微群视频已下载表,超过24小时没有新视频入库了😤\n")
  213. # 微视爬虫报警
  214. elif crawler == "weishi" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  215. Feishu.bot(log_type, crawler, "微视已下载表,超过24小时没有新视频入库了😤")
  216. Common.logger(log_type).warning("微视已下载表,超过24小时没有新视频入库了😤\n")
  217. # 视频号爬虫报警
  218. elif crawler == "shipinhao" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  219. Feishu.bot(log_type, crawler, "视频号已下载表,超过24小时没有新视频入库了😤")
  220. Common.logger(log_type).warning("视频号已下载表,超过24小时没有新视频入库了😤\n")
  221. except Exception as e:
  222. Common.logger(log_type).error(f"robot_alarm异常:{e}\n")
  223. # 监控运行入口
  224. @classmethod
  225. def main(cls):
  226. """
  227. 每隔六小时,检查一次已下载表;
  228. 已下载表的最新一条数据抓取时间,距当前时间超过 24 小时,则触发机器人报警,发送飞书报警消息
  229. """
  230. # 已下载表,超过 24 小时无新视频入库报警
  231. duration = 3600 * 24
  232. while True:
  233. if datetime.datetime.now().hour == 10:
  234. Common.logger("bot").info("监控看一看已下载表")
  235. Bot.robot_download_sheet("bot", "kanyikan", duration)
  236. Common.logger("bot").info("监控小年糕已下载表")
  237. Bot.robot_download_sheet("bot", "xiaoniangao_hour", duration)
  238. Bot.robot_download_sheet("bot", "xiaoniangao_person", duration)
  239. Bot.robot_download_sheet("bot", "xiaoniangao_play", duration)
  240. # Common.logger("bot").info("监控西瓜视频已下载表")
  241. # Bot.robot_download_sheet("bot", "xigua_video", duration)
  242. # Bot.robot_download_sheet("bot", "xigua_little_video", duration)
  243. Common.logger('bot').info('监控知乎已下载表')
  244. Bot.robot_download_sheet("bot", "zhihu_hot", duration)
  245. Bot.robot_download_sheet("bot", "zhihu_follow", duration)
  246. Common.logger("bot").info("监控吉祥幸福已下载表")
  247. Bot.robot_download_sheet("bot", "jxxf", duration)
  248. Common.logger("bot").info("监控众妙音信已下载表")
  249. Bot.robot_download_sheet("bot", "zmyx", duration)
  250. Common.logger("bot").info("监控祝福猫已下载表")
  251. Bot.robot_download_sheet("bot", "zhufumao", duration)
  252. Common.logger("bot").info("监控岁岁年年迎福气已下载表")
  253. Bot.robot_download_sheet("bot", "ssnnyfq", duration)
  254. Common.logger("bot").info("监控本山祝福已下载表")
  255. Bot.robot_download_sheet("bot", "bszf", duration)
  256. Common.logger("bot").info("监控快手已下载表")
  257. Bot.robot_download_sheet("bot", "kuaishou_recommend", duration)
  258. Bot.robot_download_sheet("bot", "kuaishou_follow", duration)
  259. Common.logger("bot").info("监控微视已下载表")
  260. Bot.robot_download_sheet("bot", "weishi", duration)
  261. Common.logger("bot").info("监控视频号已下载表")
  262. Bot.robot_download_sheet("bot", "shipinhao", duration)
  263. # Common.logger("bot").info("监控公众号已下载表")
  264. # Bot.robot_download_sheet("bot", "gzh", duration)
  265. # Common.logger("bot").info("监控音乐相册已下载表")
  266. # Bot.robot_download_sheet("bot", "music_album", duration)
  267. # Common.logger("bot").info("监控微群视频已下载表")
  268. # Bot.robot_download_sheet("bot", "weiqun", duration)
  269. Common.del_logs("bot")
  270. Common.logger("bot").info(f"休眠{24-datetime.datetime.now().hour}小时")
  271. time.sleep(3600 * (24-datetime.datetime.now().hour))
  272. else:
  273. pass
  274. if __name__ == "__main__":
  275. # Bot.robot_download_sheet("bot", "zhufumao", 1)
  276. Bot.main()
  277. pass