changsha_bot.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """
  2. 独立执行,每个人执行一次,单日总量到达 300 即执行一次
  3. """
  4. import json
  5. import datetime
  6. import time
  7. import requests
  8. import multiprocessing
  9. from common.db import RedisClient
  10. def protect_spider_timeout(function):
  11. """
  12. 守护进程,在程序启动后的某一个时段内守护爬虫进程
  13. :param function: 被守护的函数
  14. :param hour: 守护时长 / hour
  15. """
  16. process = multiprocessing.Process(target=function)
  17. process.start()
  18. while True:
  19. if not process.is_alive():
  20. process.terminate()
  21. time.sleep(60)
  22. process = multiprocessing.Process(target=function)
  23. process.start()
  24. time.sleep(60)
  25. def bot(name):
  26. """
  27. 报警机器人
  28. """
  29. id_dict = {
  30. "余海涛": "ou_b87d153e200a04de3d82b1b9276e8f90",
  31. "范军": "ou_fce9cfef186e260e70554b47fee70a34",
  32. "罗情": "ou_88139cd84c2d105c2e1d699c14ec3375",
  33. "鲁涛": "ou_7986cccb78e6c981db8d0eef93443d05",
  34. "王雪珂": "ou_2233fb8e1302314bae166fcfa144151f",
  35. "邓锋": "ou_379d37645f929e1e6553a75aecda42a2"
  36. }
  37. url = "https://open.feishu.cn/open-apis/bot/v2/hook/df47bb77-ecaa-4628-b076-aae776415ae8"
  38. headers = {"Content-Type": "application/json"}
  39. payload = {
  40. "msg_type": "interactive",
  41. "card": {
  42. "elements": [
  43. {
  44. "tag": "div",
  45. "text": {
  46. "content": "抓取数量触发限量通知, <at id={}></at>, <at id={}></at>, <at id={}></at>\n".format(
  47. id_dict[name], id_dict["邓锋"], id_dict["王雪珂"]
  48. ),
  49. "tag": "lark_md",
  50. },
  51. },
  52. {
  53. "tag": "div",
  54. "text": {
  55. "content": "当天已经入库 300 条视频",
  56. "tag": "lark_md",
  57. },
  58. },
  59. ],
  60. "header": {"title": {"content": "【 通知 】", "tag": "plain_text"}},
  61. },
  62. }
  63. requests.post(url, headers=headers, data=json.dumps(payload))
  64. def monitor():
  65. """
  66. 监测 redis 中数据
  67. """
  68. keys = {"352": "余海涛", "353": "罗情", "53": "范军", "51": "鲁涛"}
  69. now = datetime.datetime.now().time()
  70. start_alert_time = datetime.time(10)
  71. end_alert_time = datetime.time(20, 30)
  72. while True:
  73. if start_alert_time <= now <= end_alert_time:
  74. try:
  75. R = RedisClient()
  76. if R.connect():
  77. for key in keys:
  78. count = R.select(key)
  79. if count:
  80. OO = int(count.decode("utf-8"))
  81. name = keys[key]
  82. redis_date_key = key + "-" + datetime.date.today().strftime("%Y%m%d")
  83. if R.select(redis_date_key):
  84. # 说明已经存储进去了, 不需要再报警了
  85. continue
  86. else:
  87. if OO > 300:
  88. R.insert(redis_date_key, "already bot", 86400)
  89. # print("超过了, 报警", name)
  90. bot(name)
  91. else:
  92. continue
  93. except Exception as e:
  94. print(e)
  95. pass
  96. # 查询一次之后等待 60 s
  97. time.sleep(60)
  98. if __name__ == '__main__':
  99. protect_spider_timeout(monitor)
  100. # monitor()
  101. # R = RedisClient()
  102. # R.connect()
  103. # # w = R.select("53-20240327")
  104. # # print(w)
  105. # R.delete("51-20240327")
  106. # R.delete("53-20240327")
  107. # R.delete("353-20240327")
  108. # R.delete("352-20240327")