limit.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import os
  2. import sys
  3. from datetime import datetime, timedelta
  4. sys.path.append(os.getcwd())
  5. from common.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. class AuthorLimit(object):
  19. """
  20. 账号爬虫限量
  21. """
  22. def __init__(self, mode, platform):
  23. self.mode = mode
  24. self.platform = platform
  25. self.limit_tag_dict = {"352": "余海涛", "353": "罗情", "53": "范军", "51": "鲁涛", "131": False}
  26. def find_tag(self, uid):
  27. """
  28. 通过 uid 去找符合标准的 tag
  29. """
  30. sql = f"""select tag from crawler_user_v3 where uid={uid};"""
  31. result = MysqlHelper.get_values(
  32. log_type=self.mode, crawler=self.platform, env="prod", sql=sql
  33. )
  34. tags = result[0]["tag"]
  35. if tags:
  36. tags = tags.split(",")
  37. if "131" in tags:
  38. return None
  39. else:
  40. for tag in tags:
  41. if self.limit_tag_dict.get(tag):
  42. return tag
  43. return None
  44. def author_limitation(self, user_id):
  45. """
  46. 限制账号, 服务长沙四名同学
  47. """
  48. if self.mode == "author":
  49. tag = self.find_tag(user_id)
  50. if tag:
  51. AliyunLogger.logging(
  52. code="8807",
  53. platform=self.platform,
  54. mode=self.mode,
  55. env="prod",
  56. message="找到个人账号,{}".format(tag)
  57. )
  58. R = RedisClient()
  59. if R.connect():
  60. tag_count = R.select(tag)
  61. if tag_count:
  62. tag_count = int(tag_count.decode("utf-8"))
  63. if tag_count <= 300:
  64. tag_count += 1
  65. expire_seconds = generate_expire_time()
  66. R.insert(
  67. key=tag, value=tag_count, expire_time=expire_seconds
  68. )
  69. return True
  70. else:
  71. # 报警
  72. return False
  73. else:
  74. tag_count = 1
  75. expire_seconds = generate_expire_time()
  76. R.insert(
  77. key=tag, value=tag_count, expire_time=expire_seconds
  78. )
  79. return True
  80. else:
  81. return True
  82. else:
  83. return True
  84. else:
  85. return True