limit.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import json
  2. import time
  3. import requests
  4. from datetime import datetime, timedelta
  5. from .aliyun_log import AliyunLogger
  6. from common.scheduling_db import MysqlHelper
  7. from common.db import RedisClient
  8. def generate_expire_time():
  9. """
  10. 计算出过期时间
  11. """
  12. now = datetime.now()
  13. # 当天晚上12点的时间
  14. midnight = datetime(now.year, now.month, now.day) + timedelta(days=1)
  15. # 计算当前时间到当天晚上12点的秒数
  16. seconds_until_midnight = int((midnight - now).total_seconds())
  17. return seconds_until_midnight
  18. def bot(name):
  19. """
  20. 报警机器人
  21. """
  22. id_dict = {
  23. "余海涛": "15869772902",
  24. "范军": "15200827642",
  25. "罗情": "15111037095",
  26. "鲁涛": "18573105114",
  27. "王雪珂": "13513479926",
  28. "邓锋": "18175188134",
  29. }
  30. url = "https://open.feishu.cn/open-apis/bot/v2/hook/c273ff48-7b7e-4078-b3f7-d69a3d262df9"
  31. headers = {"Content-Type": "application/json"}
  32. payload = {
  33. "msg_type": "interactive",
  34. "card": {
  35. "elements": [
  36. {
  37. "tag": "div",
  38. "text": {
  39. "content": "抓取数量触发限量通知, <at id={}></at>, <at id={}></at>, <at id={}></at>\n".format(
  40. id_dict[name], id_dict["邓锋"], id_dict["王雪珂"]
  41. ),
  42. "tag": "lark_md",
  43. },
  44. },
  45. {
  46. "tag": "div",
  47. "text": {
  48. "content": "当天已经入库 300 条视频",
  49. "tag": "lark_md",
  50. },
  51. },
  52. ],
  53. "header": {"title": {"content": "【 通知 】", "tag": "plain_text"}},
  54. },
  55. }
  56. requests.post(url, headers=headers, data=json.dumps(payload))
  57. class AuthorLimit(object):
  58. """
  59. 账号爬虫限量
  60. """
  61. def __init__(self, mode, platform):
  62. self.mode = mode
  63. self.platform = platform
  64. self.limit_tag_dict = {"352": "余海涛", "353": "罗情", "53": "范军", "51": "鲁涛"}
  65. def find_tag(self, uid):
  66. """
  67. 通过 uid 去找符合标准的 tag
  68. """
  69. sql = f"""select tag from crawler_user_v3 where uid={uid};"""
  70. result = MysqlHelper.get_values(
  71. log_type=self.mode, crawler=self.platform, env="prod", sql=sql
  72. )
  73. tags = result[0]["tag"]
  74. if tags:
  75. tags = tags.split(",")
  76. for tag in tags:
  77. if self.limit_tag_dict.get(tag):
  78. return tag
  79. return None
  80. def author_limitation(self, user_id):
  81. """
  82. 限制账号, 服务长沙四名同学
  83. """
  84. if self.mode == "author":
  85. tag = self.find_tag(user_id)
  86. if tag:
  87. if RedisClient().connect():
  88. tag_count = RedisClient().select(tag)
  89. if tag_count:
  90. tag_count = int(tag_count.decode("utf-8"))
  91. if tag_count <= 300:
  92. tag_count += 1
  93. expire_seconds = generate_expire_time()
  94. RedisClient().insert(
  95. key=tag, value=tag_count, expire_time=expire_seconds
  96. )
  97. return True
  98. else:
  99. # 报警
  100. name = self.limit_tag_dict[tag]
  101. bot(name)
  102. return False
  103. else:
  104. tag_count = 1
  105. expire_seconds = generate_expire_time()
  106. RedisClient().insert(
  107. key=tag, value=tag_count, expire_time=expire_seconds
  108. )
  109. return True
  110. else:
  111. return True
  112. else:
  113. return True
  114. else:
  115. return True