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. # # 获取各个爬虫的 feeds 表
  14. # @classmethod
  15. # def get_feeds_sheet(cls, log_type, crawler, sheet):
  16. # try:
  17. # if crawler == "kanyikan" and sheet == "recommend":
  18. # feeds_sheet = Feishu.get_values_batch(log_type, "kanyikan", "SdCHOM")
  19. # elif crawler == "kanyikan" and sheet == "moment":
  20. # feeds_sheet = Feishu.get_values_batch(log_type, "kanyikan", "tGqZMX")
  21. # elif crawler == "xiaoniangao" and sheet == "hour":
  22. # feeds_sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "ba0da4")
  23. # elif crawler == "xiaoniangao" and sheet == "person":
  24. # feeds_sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "k6ldje")
  25. # elif crawler == "music_album" and sheet == "recommend":
  26. # feeds_sheet = Feishu.get_values_batch(log_type, "music_album", "69UxPo")
  27. # elif crawler == "bszf" and sheet == "recommend":
  28. # feeds_sheet = Feishu.get_values_batch(log_type, "bszf", "CcHgO7")
  29. # elif crawler == "kuaishou" and sheet == "recommend":
  30. # feeds_sheet = Feishu.get_values_batch(log_type, "kuaishou", "JK6npf")
  31. # elif crawler == "kuaishou" and sheet == "follow":
  32. # feeds_sheet = Feishu.get_values_batch(log_type, "kuaishou", "wW5cyb")
  33. # elif crawler == "gzh" and sheet == "recommend":
  34. # feeds_sheet = Feishu.get_values_batch(log_type, "gzh", "zWKFGb")
  35. # elif crawler == "weishi" and sheet == "recommend":
  36. # feeds_sheet = Feishu.get_values_batch(log_type, "weishi", "O7fCzr")
  37. # else:
  38. # feeds_sheet = "请输入{crawler}和{sheet}"
  39. #
  40. # return feeds_sheet
  41. # except Exception as e:
  42. # Common.logger(log_type).error("get_feeds_sheet异常:{}", e)
  43. #
  44. # # feeds_sheet表报警:连续 2 小时无数据
  45. # @classmethod
  46. # def rebot_feeds_sheet(cls, log_type, crawler, sheet):
  47. # """
  48. # 每隔一分钟获取一次表数据的数量:
  49. # 1.中途有数据时,退出此次监控
  50. # 2.连续2小时无数据时,触发机器人报警
  51. # """
  52. # # kanyikan_recommend_sheet = Feishu.get_values_batch(log_type, "kanyikan", "SdCHOM")
  53. # # kanyikan_moment_sheet = Feishu.get_values_batch(log_type, "kanyikan", "tGqZMX")
  54. # # xiaoniangao_hour_sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "ba0da4")
  55. # # xiaoniangao_person_sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "k6ldje")
  56. # # music_album_recommend_sheet = Feishu.get_values_batch(log_type, "music_album", "69UxPo")
  57. # # bszf_recommend_sheet = Feishu.get_values_batch(log_type, "bszf", "CcHgO7")
  58. # # kuaishou_recommend_sheet = Feishu.get_values_batch(log_type, "kuaishou", "JK6npf")
  59. # # kuaishou_follow_sheet = Feishu.get_values_batch(log_type, "kuaishou", "wW5cyb")
  60. # # gzh_recommend_sheet = Feishu.get_values_batch(log_type, "gzh", "zWKFGb")
  61. #
  62. # for i in range(120):
  63. # if len(cls.get_feeds_sheet(log_type, crawler, sheet)) > 1:
  64. # break
  65. # else:
  66. # time.sleep(60)
  67. # if i == 119 and crawler == "kanyikan" and sheet == "recommend":
  68. # Feishu.bot(log_type, "kanyikan", "看一看推荐榜表,已经 2 小时无数据了😤")
  69. # elif i == 119 and crawler == "kanyikan" and sheet == "moment":
  70. # Feishu.bot(log_type, "kanyikan", "看一看朋友圈表,已经 2 小时无数据了😤")
  71. # elif i == 119 and crawler == "xiaoniangao" and sheet == "person":
  72. # Feishu.bot(log_type, "xiaoniangao", "小年糕用户主页表,已经 2 小时无数据了😤")
  73. # elif i == 119 and crawler == "music_album" \
  74. # and sheet == "recommend" and datetime.datetime.now().hour < 13:
  75. # Feishu.bot(log_type, "music_album", "音乐相册推荐表,已经 2 小时无数据了😤")
  76. # elif i == 119 and crawler == "bszf" and sheet == "recommend" and datetime.datetime.now().hour < 13:
  77. # Feishu.bot(log_type, "bszf", "本山祝福推荐表,已经 2 小时无数据了😤")
  78. # elif i == 119 and crawler == "kuaishou" and sheet == "recommend":
  79. # Feishu.bot(log_type, "kuaishou", "快手推荐表,已经 2 小时无数据了😤")
  80. # elif i == 119 and crawler == "kuaishou" and sheet == "follow":
  81. # Feishu.bot(log_type, "kuaishou", "快手关注表,已经 2 小时无数据了😤")
  82. # elif i == 119 and crawler == "gzh" and sheet == "recommend":
  83. # Feishu.bot(log_type, "gzh", "公众号推荐表,已经 2 小时无数据了😤")
  84. # 获取各个爬虫表最新一条抓取时间
  85. @classmethod
  86. def get_first_time(cls, log_type, crawler):
  87. try:
  88. if crawler == "xiaoniangao_hour":
  89. sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "yatRv2")
  90. # 已下载表,最新一条视频抓取时间
  91. first_download_time = sheet[1][5]
  92. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  93. elif crawler == "xiaoniangao_person":
  94. sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "Wu0CeL")
  95. # 已下载表,最新一条视频抓取时间
  96. first_download_time = sheet[1][5]
  97. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  98. elif crawler == "xiaoniangao_play":
  99. sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "c85k1C")
  100. # 已下载表,最新一条视频抓取时间
  101. first_download_time = sheet[1][5]
  102. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  103. elif crawler == 'xigua_video':
  104. sheet = Feishu.get_values_batch(log_type, "xigua", "e075e9")
  105. # 已下载表,最新一条视频抓取时间
  106. first_download_time = sheet[1][5]
  107. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  108. elif crawler == 'xigua_little_video':
  109. sheet = Feishu.get_values_batch(log_type, "xigua", "hDSDnv")
  110. # 已下载表,最新一条视频抓取时间
  111. first_download_time = sheet[1][5]
  112. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  113. elif crawler == 'zhihu_hot':
  114. sheet = Feishu.get_values_batch(log_type, "zhihu", "8871e3")
  115. # 已下载表,最新一条视频抓取时间
  116. first_download_time = sheet[1][5]
  117. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  118. elif crawler == 'zhihu_follow':
  119. sheet = Feishu.get_values_batch(log_type, "zhihu", "4MGuux")
  120. # 已下载表,最新一条视频抓取时间
  121. first_download_time = sheet[1][5]
  122. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  123. elif crawler == 'jxxf':
  124. sheet = Feishu.get_values_batch(log_type, "jxxf", "d9e9b1")
  125. # 已下载表,最新一条视频抓取时间
  126. first_download_time = sheet[1][5]
  127. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  128. elif crawler == 'zmyx':
  129. sheet = Feishu.get_values_batch(log_type, "zmyx", "19c772")
  130. # 已下载表,最新一条视频抓取时间
  131. first_download_time = sheet[1][5]
  132. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  133. elif crawler == "kanyikan":
  134. sheet = Feishu.get_values_batch(log_type, "kanyikan", "20ce0c")
  135. # 已下载表,最新一条视频抓取时间
  136. first_download_time = sheet[1][5]
  137. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  138. elif crawler == "music_album":
  139. sheet = Feishu.get_values_batch(log_type, "music_album", "f5a76e")
  140. # 已下载表,最新一条视频抓取时间
  141. first_download_time = sheet[1][5]
  142. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  143. elif crawler == "bszf":
  144. sheet = Feishu.get_values_batch(log_type, "bszf", "440018")
  145. # 已下载表,最新一条视频抓取时间
  146. first_download_time = sheet[1][4]
  147. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  148. elif crawler == "kuaishou_recommend":
  149. sheet = Feishu.get_values_batch(log_type, "kuaishou", "3cd128")
  150. # 已下载表,最新一条视频抓取时间
  151. first_download_time = sheet[1][5]
  152. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  153. elif crawler == "kuaishou_follow":
  154. sheet = Feishu.get_values_batch(log_type, "kuaishou", "fYdA8F")
  155. # 已下载表,最新一条视频抓取时间
  156. first_download_time = sheet[1][5]
  157. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  158. elif crawler == "gzh":
  159. sheet = Feishu.get_values_batch(log_type, "gzh", "fCs3BT")
  160. # 已下载表,最新一条视频抓取时间
  161. first_download_time = sheet[1][3]
  162. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  163. elif crawler == "weiqun":
  164. sheet = Feishu.get_values_batch(log_type, "weiqun", "3cd128")
  165. # 已下载表,最新一条视频抓取时间
  166. first_download_time = sheet[1][5]
  167. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  168. elif crawler == "weishi":
  169. sheet = Feishu.get_values_batch(log_type, "weishi", "caa3fa")
  170. # 已下载表,最新一条视频抓取时间
  171. first_download_time = sheet[1][5]
  172. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  173. elif crawler == "shipinhao":
  174. sheet = Feishu.get_values_batch(log_type, "shipinhao", "c77cf9")
  175. # 已下载表,最新一条视频抓取时间
  176. first_download_time = sheet[1][5]
  177. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  178. else:
  179. sheet = Feishu.get_values_batch(log_type, "xiaoniangao", "yatRv2")
  180. # 已下载表,最新一条视频抓取时间
  181. first_download_time = sheet[1][5]
  182. first_download_time = int(time.mktime(time.strptime(first_download_time, "%Y/%m/%d %H:%M:%S")))
  183. return first_download_time
  184. except Exception as e:
  185. Common.logger(log_type).error("get_first_time异常:{}\n", e)
  186. # 触发机器人报警:超过24小时没有新入库的视频
  187. @classmethod
  188. def robot_download_sheet(cls, log_type, crawler, duration):
  189. """
  190. 已下载视频表:超过24小时没有新入库的视频
  191. """
  192. try:
  193. # 看一看爬虫报警
  194. if crawler == "kanyikan" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  195. Feishu.bot(log_type, crawler, "看一看已下载表,超过24小时没有新视频入库了😤")
  196. Common.logger(log_type).warning("看一看已下载表,超过24小时没有新视频入库了😤\n")
  197. # 小年糕爬虫报警
  198. elif crawler == "xiaoniangao_hour" and (
  199. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  200. Feishu.bot(log_type, crawler, "小年糕_小时级_已下载表,超过24小时没有新视频入库了😤")
  201. Common.logger(log_type).warning("小年糕_小时级_已下载表,超过24小时没有新视频入库了😤\n")
  202. elif crawler == "xiaoniangao_person" 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. elif crawler == "xiaoniangao_play" and (
  207. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  208. Feishu.bot(log_type, crawler, "小年糕_播放量_已下载表,超过24小时没有新视频入库了😤")
  209. Common.logger(log_type).warning("小年糕_播放量_已下载表,超过24小时没有新视频入库了😤\n")
  210. # 西瓜视频
  211. elif crawler == "xigua_video" and (
  212. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  213. Feishu.bot(log_type, crawler, "西瓜视频_用户主页_已下载表,超过24小时没有新视频入库了😤")
  214. Common.logger(log_type).warning("西瓜视频_用户主页_已下载表,超过24小时没有新视频入库了😤\n")
  215. elif crawler == "xigua_little_video" and (
  216. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  217. Feishu.bot(log_type, crawler, "西瓜视频_小视频_已下载表,超过24小时没有新视频入库了😤")
  218. Common.logger(log_type).warning("西瓜视频_小视频_已下载表,超过24小时没有新视频入库了😤\n")
  219. # 知乎
  220. elif crawler == "zhihu_hot" and (
  221. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  222. Feishu.bot(log_type, crawler, "知乎_热门_已下载表,超过24小时没有新视频入库了😤")
  223. Common.logger(log_type).warning("知乎_热门_已下载表,超过24小时没有新视频入库了😤\n")
  224. elif crawler == "zhihu_follow" and (
  225. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  226. Feishu.bot(log_type, crawler, "知乎_定向_已下载表,超过24小时没有新视频入库了😤")
  227. Common.logger(log_type).warning("知乎_定向_已下载表,超过24小时没有新视频入库了😤\n")
  228. # 吉祥幸福
  229. elif crawler == "jxxf" and (
  230. 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 == "zmyx" and (
  235. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  236. Feishu.bot(log_type, crawler, "众妙音信_已下载表,超过24小时没有新视频入库了😤")
  237. Common.logger(log_type).warning("众妙音信_已下载表,超过24小时没有新视频入库了😤\n")
  238. # 音乐相册爬虫报警
  239. elif crawler == "music_album" and (
  240. int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  241. Feishu.bot(log_type, crawler, "音乐相册已下载表,超过24小时没有新视频入库了😤")
  242. Common.logger(log_type).warning("音乐相册已下载表,超过24小时没有新视频入库了😤\n")
  243. # 本山祝福爬虫报警
  244. elif crawler == "bszf" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  245. Feishu.bot(log_type, crawler, "本山祝福已下载表,超过24小时没有新视频入库了😤")
  246. Common.logger(log_type).warning("本山祝福已下载表,超过24小时没有新视频入库了😤\n")
  247. # 快手爬虫报警
  248. elif crawler == "kuaishou_recommend" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  249. Feishu.bot(log_type, crawler, "快手_推荐榜_已下载表,超过24小时没有新视频入库了😤")
  250. Common.logger(log_type).warning("快手_推荐榜_已下载表,超过24小时没有新视频入库了😤\n")
  251. elif crawler == "kuaishou_follow" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  252. Feishu.bot(log_type, crawler, "快手_用户主页_已下载表,超过24小时没有新视频入库了😤")
  253. Common.logger(log_type).warning("快手_用户主页_已下载表,超过24小时没有新视频入库了😤\n")
  254. # 公众号爬虫报警
  255. elif crawler == "gzh" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  256. Feishu.bot(log_type, crawler, "公众号已下载表,超过24小时没有新视频入库了😤")
  257. Common.logger(log_type).warning("公众号已下载表,超过24小时没有新视频入库了😤\n")
  258. # 微群视频爬虫报警
  259. elif crawler == "weiqun" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  260. Feishu.bot(log_type, crawler, "微群视频已下载表,超过24小时没有新视频入库了😤")
  261. Common.logger(log_type).warning("微群视频已下载表,超过24小时没有新视频入库了😤\n")
  262. # 微视爬虫报警
  263. elif crawler == "weishi" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  264. Feishu.bot(log_type, crawler, "微视已下载表,超过24小时没有新视频入库了😤")
  265. Common.logger(log_type).warning("微视已下载表,超过24小时没有新视频入库了😤\n")
  266. # 视频号爬虫报警
  267. elif crawler == "shipinhao" and (int(time.time()) - cls.get_first_time(log_type, crawler) > int(duration)):
  268. Feishu.bot(log_type, crawler, "视频号已下载表,超过24小时没有新视频入库了😤")
  269. Common.logger(log_type).warning("视频号已下载表,超过24小时没有新视频入库了😤\n")
  270. except Exception as e:
  271. Common.logger(log_type).error("robot_alarm异常:{}", e)
  272. # 监控运行入口
  273. @classmethod
  274. def main(cls):
  275. """
  276. 每隔六小时,检查一次已下载表;
  277. 已下载表的最新一条数据抓取时间,距当前时间超过 24 小时,则触发机器人报警,发送飞书报警消息
  278. """
  279. # 已下载表,超过 24 小时无新视频入库报警
  280. duration = 3600 * 24
  281. while True:
  282. if datetime.datetime.now().hour == 10:
  283. Common.logger("bot").info("监控看一看已下载表")
  284. Bot.robot_download_sheet("bot", "kanyikan", duration)
  285. Common.logger("bot").info("监控小年糕已下载表")
  286. Bot.robot_download_sheet("bot", "xiaoniangao_hour", duration)
  287. Bot.robot_download_sheet("bot", "xiaoniangao_person", duration)
  288. Bot.robot_download_sheet("bot", "xiaoniangao_play", duration)
  289. Common.logger("bot").info("监控西瓜视频已下载表")
  290. Bot.robot_download_sheet("bot", "xigua_video", duration)
  291. # Bot.robot_download_sheet("bot", "xigua_little_video", duration)
  292. Common.logger('bot').info('监控知乎已下载表')
  293. Bot.robot_download_sheet("bot", "zhihu_hot", duration)
  294. Bot.robot_download_sheet("bot", "zhihu_follow", duration)
  295. Common.logger("bot").info("监控吉祥幸福已下载表")
  296. Bot.robot_download_sheet("bot", "jxxf", duration)
  297. Common.logger("bot").info("监控众妙音信已下载表")
  298. Bot.robot_download_sheet("bot", "zmyx", duration)
  299. Common.logger("bot").info("监控本山祝福已下载表")
  300. Bot.robot_download_sheet("bot", "bszf", duration)
  301. Common.logger("bot").info("监控快手已下载表")
  302. Bot.robot_download_sheet("bot", "kuaishou_recommend", duration)
  303. Bot.robot_download_sheet("bot", "kuaishou_follow", duration)
  304. Common.logger("bot").info("监控微视已下载表")
  305. Bot.robot_download_sheet("bot", "weishi", duration)
  306. Common.logger("bot").info("监控视频号已下载表")
  307. Bot.robot_download_sheet("bot", "shipinhao", duration)
  308. # Common.logger("bot").info("监控公众号已下载表")
  309. # Bot.robot_download_sheet("bot", "gzh", duration)
  310. # Common.logger("bot").info("监控音乐相册已下载表")
  311. # Bot.robot_download_sheet("bot", "music_album", duration)
  312. # Common.logger("bot").info("监控微群视频已下载表")
  313. # Bot.robot_download_sheet("bot", "weiqun", duration)
  314. Common.del_logs("bot")
  315. Common.logger("bot").info("休眠{}小时", 24-datetime.datetime.now().hour)
  316. time.sleep(3600 * (24-datetime.datetime.now().hour))
  317. else:
  318. pass
  319. if __name__ == "__main__":
  320. # Bot.robot_download_sheet("bot", "kuaishou_recommend", 1)
  321. # Bot.robot_download_sheet("bot", "zmyx", 1)
  322. Bot.main()
  323. pass