run_bot.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 == 'haokan_hot':
  53. sheet = Feishu.get_values_batch(log_type, "haokan", "5pWipX")
  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 == 'haokan_channel':
  58. sheet = Feishu.get_values_batch(log_type, "haokan", "7f05d8")
  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 == 'jxxf':
  63. sheet = Feishu.get_values_batch(log_type, "jxxf", "d9e9b1")
  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 == 'zmyx':
  68. sheet = Feishu.get_values_batch(log_type, "zmyx", "19c772")
  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 == 'zhufumao':
  73. sheet = Feishu.get_values_batch(log_type, "zhufumao", "e13bdf")
  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 == 'ssnnyfq':
  78. sheet = Feishu.get_values_batch(log_type, "ssnnyfq", "290bae")
  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 == "kanyikan":
  83. sheet = Feishu.get_values_batch(log_type, "kanyikan", "20ce0c")
  84. # 已下载表,最新一条视频抓取时间
  85. first_download_time = sheet[1][5]
  86. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  87. elif crawler == "music_album":
  88. sheet = Feishu.get_values_batch(log_type, "music_album", "f5a76e")
  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 == "bszf":
  93. sheet = Feishu.get_values_batch(log_type, "bszf", "440018")
  94. # 已下载表,最新一条视频抓取时间
  95. first_download_time = sheet[1][4]
  96. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  97. elif crawler == "kuaishou_recommend":
  98. sheet = Feishu.get_values_batch(log_type, "kuaishou", "3cd128")
  99. # 已下载表,最新一条视频抓取时间
  100. first_download_time = sheet[1][5]
  101. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  102. elif crawler == "kuaishou_follow":
  103. sheet = Feishu.get_values_batch(log_type, "kuaishou", "fYdA8F")
  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 == "gzh":
  108. sheet = Feishu.get_values_batch(log_type, "gzh", "fCs3BT")
  109. # 已下载表,最新一条视频抓取时间
  110. first_download_time = sheet[1][3]
  111. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  112. elif crawler == "weiqun":
  113. sheet = Feishu.get_values_batch(log_type, "weiqun", "3cd128")
  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. elif crawler == "weishi":
  118. sheet = Feishu.get_values_batch(log_type, "weishi", "caa3fa")
  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. elif crawler == "shipinhao":
  123. sheet = Feishu.get_values_batch(log_type, "shipinhao", "c77cf9")
  124. # 已下载表,最新一条视频抓取时间
  125. first_download_time = sheet[1][5]
  126. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  127. elif crawler == "zongjiao":
  128. sheet = Feishu.get_values_batch(log_type, "zongjiao", "xf9wC2")
  129. # 已下载表,最新一条视频抓取时间
  130. first_download_time = sheet[1][5]
  131. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  132. else:
  133. sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "yatRv2")
  134. # 已下载表,最新一条视频抓取时间
  135. first_download_time = sheet[1][5]
  136. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  137. return first_download_time
  138. except Exception as e:
  139. Common.logger(log_type).error(f"get_first_time异常:{e}\n")
  140. # 触发机器人报警:超过24小时没有新入库的视频
  141. @classmethod
  142. def robot_download_sheet(cls, log_type, crawler, duration):
  143. """
  144. 已下载视频表:超过24小时没有新入库的视频
  145. """
  146. try:
  147. # 看一看爬虫报警
  148. if crawler == "kanyikan" 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).warning("看一看已下载表,超过24小时没有新视频入库了😤\n")
  151. # 小年糕爬虫报警
  152. elif crawler == "xiaoniangao_hour" and (
  153. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  154. Feishu.bot(log_type, crawler, "小年糕_小时级_已下载表,超过24小时没有新视频入库了😤")
  155. Common.logger(log_type).warning("小年糕_小时级_已下载表,超过24小时没有新视频入库了😤\n")
  156. elif crawler == "xiaoniangao_person" and (
  157. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  158. Feishu.bot(log_type, crawler, "小年糕_用户主页_已下载表,超过24小时没有新视频入库了😤")
  159. Common.logger(log_type).warning("小年糕_用户主页_已下载表,超过24小时没有新视频入库了😤\n")
  160. elif crawler == "xiaoniangao_play" and (
  161. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  162. Feishu.bot(log_type, crawler, "小年糕_播放量_已下载表,超过24小时没有新视频入库了😤")
  163. Common.logger(log_type).warning("小年糕_播放量_已下载表,超过24小时没有新视频入库了😤\n")
  164. # 西瓜视频
  165. elif crawler == "xigua_video" and (
  166. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  167. Feishu.bot(log_type, crawler, "西瓜视频_用户主页_已下载表,超过24小时没有新视频入库了😤")
  168. Common.logger(log_type).warning("西瓜视频_用户主页_已下载表,超过24小时没有新视频入库了😤\n")
  169. elif crawler == "xigua_little_video" and (
  170. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  171. Feishu.bot(log_type, crawler, "西瓜视频_小视频_已下载表,超过24小时没有新视频入库了😤")
  172. Common.logger(log_type).warning("西瓜视频_小视频_已下载表,超过24小时没有新视频入库了😤\n")
  173. # 知乎
  174. elif crawler == "zhihu_hot" and (
  175. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  176. Feishu.bot(log_type, crawler, "知乎_热门_已下载表,超过24小时没有新视频入库了😤")
  177. Common.logger(log_type).warning("知乎_热门_已下载表,超过24小时没有新视频入库了😤\n")
  178. elif crawler == "zhihu_follow" 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 == "haokan_hot" 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. elif crawler == "haokan_channel" and (
  188. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  189. Feishu.bot(log_type, crawler, "好看_频道_已下载表,超过24小时没有新视频入库了😤")
  190. Common.logger(log_type).warning("好看_频道_已下载表,超过24小时没有新视频入库了😤\n")
  191. # 吉祥幸福
  192. elif crawler == "jxxf" and (
  193. 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 == "zmyx" 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. # 祝福猫视频
  202. elif crawler == "zhufumao" and (
  203. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  204. Feishu.bot(log_type, crawler, "祝福猫_已下载表,超过24小时没有新视频入库了😤")
  205. Common.logger(log_type).warning("祝福猫_已下载表,超过24小时没有新视频入库了😤\n")
  206. # 岁岁年年迎福气
  207. elif crawler == "ssnnyfq" and (
  208. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  209. Feishu.bot(log_type, crawler, "岁岁年年迎福气_已下载表,超过24小时没有新视频入库了😤")
  210. Common.logger(log_type).warning("岁岁年年迎福气_已下载表,超过24小时没有新视频入库了😤\n")
  211. # 音乐相册爬虫报警
  212. elif crawler == "music_album" and (
  213. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  214. Feishu.bot(log_type, crawler, "音乐相册已下载表,超过24小时没有新视频入库了😤")
  215. Common.logger(log_type).warning("音乐相册已下载表,超过24小时没有新视频入库了😤\n")
  216. # 本山祝福爬虫报警
  217. elif crawler == "bszf" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  218. Feishu.bot(log_type, crawler, "本山祝福已下载表,超过24小时没有新视频入库了😤")
  219. Common.logger(log_type).warning("本山祝福已下载表,超过24小时没有新视频入库了😤\n")
  220. # 快手爬虫报警
  221. elif crawler == "kuaishou_recommend" and (
  222. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  223. Feishu.bot(log_type, crawler, "快手_推荐榜_已下载表,超过24小时没有新视频入库了😤")
  224. Common.logger(log_type).warning("快手_推荐榜_已下载表,超过24小时没有新视频入库了😤\n")
  225. elif crawler == "kuaishou_follow" and (
  226. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  227. Feishu.bot(log_type, crawler, "快手_用户主页_已下载表,超过24小时没有新视频入库了😤")
  228. Common.logger(log_type).warning("快手_用户主页_已下载表,超过24小时没有新视频入库了😤\n")
  229. # 公众号爬虫报警
  230. elif crawler == "gzh" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  231. Feishu.bot(log_type, crawler, "公众号已下载表,超过24小时没有新视频入库了😤")
  232. Common.logger(log_type).warning("公众号已下载表,超过24小时没有新视频入库了😤\n")
  233. # 微群视频爬虫报警
  234. elif crawler == "weiqun" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  235. Feishu.bot(log_type, crawler, "微群视频已下载表,超过24小时没有新视频入库了😤")
  236. Common.logger(log_type).warning("微群视频已下载表,超过24小时没有新视频入库了😤\n")
  237. # 微视爬虫报警
  238. elif crawler == "weishi" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  239. Feishu.bot(log_type, crawler, "微视已下载表,超过24小时没有新视频入库了😤")
  240. Common.logger(log_type).warning("微视已下载表,超过24小时没有新视频入库了😤\n")
  241. # 视频号爬虫报警
  242. elif crawler == "shipinhao" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  243. Feishu.bot(log_type, crawler, "视频号已下载表,超过24小时没有新视频入库了😤")
  244. Common.logger(log_type).warning("视频号已下载表,超过24小时没有新视频入库了😤\n")
  245. # 宗教公众号爬虫报警
  246. elif crawler == "zongjiao" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  247. Feishu.bot(log_type, crawler, "宗教公众号已下载表,超过24小时没有新视频入库了😤")
  248. Common.logger(log_type).warning("宗教公众号已下载表,超过24小时没有新视频入库了😤\n")
  249. except Exception as e:
  250. Common.logger(log_type).error(f"robot_alarm异常:{e}\n")
  251. # 监控运行入口
  252. @classmethod
  253. def main(cls):
  254. """
  255. 每隔六小时,检查一次已下载表;
  256. 已下载表的最新一条数据抓取时间,距当前时间超过 24 小时,则触发机器人报警,发送飞书报警消息
  257. """
  258. # 已下载表,超过 24 小时无新视频入库报警
  259. duration = 3600 * 24
  260. while True:
  261. if datetime.datetime.now().hour == 10:
  262. Common.logger("bot").info("监控看一看已下载表")
  263. Bot.robot_download_sheet("bot", "kanyikan", duration)
  264. Common.logger("bot").info("监控小年糕已下载表")
  265. Bot.robot_download_sheet("bot", "xiaoniangao_hour", duration)
  266. Bot.robot_download_sheet("bot", "xiaoniangao_person", duration)
  267. Bot.robot_download_sheet("bot", "xiaoniangao_play", duration)
  268. Common.logger("bot").info("监控西瓜视频已下载表")
  269. Bot.robot_download_sheet("bot", "xigua_video", duration)
  270. # Bot.robot_download_sheet("bot", "xigua_little_video", duration)
  271. Common.logger('bot').info('监控知乎已下载表')
  272. Bot.robot_download_sheet("bot", "zhihu_hot", duration)
  273. Bot.robot_download_sheet("bot", "zhihu_follow", duration)
  274. Common.logger('bot').info('监控好看已下载表')
  275. Bot.robot_download_sheet("bot", "haokan_hot", duration)
  276. Bot.robot_download_sheet("bot", "haokan_channel", duration)
  277. Common.logger("bot").info("监控吉祥幸福已下载表")
  278. Bot.robot_download_sheet("bot", "jxxf", duration)
  279. Common.logger("bot").info("监控众妙音信已下载表")
  280. Bot.robot_download_sheet("bot", "zmyx", duration)
  281. # Common.logger("bot").info("监控祝福猫已下载表")
  282. # Bot.robot_download_sheet("bot", "zhufumao", duration)
  283. Common.logger("bot").info("监控岁岁年年迎福气已下载表")
  284. Bot.robot_download_sheet("bot", "ssnnyfq", duration)
  285. Common.logger("bot").info("监控本山祝福已下载表")
  286. Bot.robot_download_sheet("bot", "bszf", duration)
  287. Common.logger("bot").info("监控快手已下载表")
  288. Bot.robot_download_sheet("bot", "kuaishou_recommend", duration)
  289. Bot.robot_download_sheet("bot", "kuaishou_follow", duration)
  290. Common.logger("bot").info("监控微视已下载表")
  291. Bot.robot_download_sheet("bot", "weishi", duration)
  292. Common.logger("bot").info("监控视频号已下载表")
  293. Bot.robot_download_sheet("bot", "shipinhao", duration)
  294. Common.logger("bot").info("监控宗教公众号已下载表")
  295. Bot.robot_download_sheet("bot", "zongjiao", duration)
  296. # Common.logger("bot").info("监控公众号已下载表")
  297. # Bot.robot_download_sheet("bot", "gzh", duration)
  298. # Common.logger("bot").info("监控音乐相册已下载表")
  299. # Bot.robot_download_sheet("bot", "music_album", duration)
  300. # Common.logger("bot").info("监控微群视频已下载表")
  301. # Bot.robot_download_sheet("bot", "weiqun", duration)
  302. Common.del_logs("bot")
  303. Common.logger("bot").info(f"休眠{24-datetime.datetime.now().hour}小时")
  304. time.sleep(3600 * (24-datetime.datetime.now().hour))
  305. else:
  306. pass
  307. if __name__ == "__main__":
  308. # Bot.robot_download_sheet("bot", "haokan_channel", 1)
  309. Bot.main()
  310. pass