snapshot_taobao.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import json
  2. import random
  3. import time
  4. import requests
  5. from commons.Logger import logger
  6. from commons.conn_mysql import MySQLPoolOnline, MySQLPool39
  7. from spiders.taobao.snapshot_taobao_crawl2 import TaobaoCrawl
  8. from commons.scheduler import CrawlerScheduler
  9. from commons.feishu_webhook import send_text
  10. from commons.config import TB_DEVICE_ID
  11. platform_name = "淘宝"
  12. class TaobaoMain:
  13. def __init__(self):
  14. # self.db_online = MySQLPool39()
  15. self.db_online = MySQLPoolOnline()
  16. self.crawl_count = ""
  17. self.task_id = ""
  18. self.task_dict = None
  19. self.driver = None
  20. self.cumulative_pages = 0
  21. self.cumulative_items = 0
  22. self.cumulative_stored = 0
  23. self.cumulative_skipped = 0
  24. def get_status(self, status):
  25. if status not in (2, 3, 4):
  26. logger.warning(f"未知状态值: {status}, 跳过状态上报")
  27. return
  28. if status == 2:
  29. parmas = {
  30. "collect_task_allocate_id": self.task_id, "status": status, "finish_status": 0,
  31. "start_time": int(time.time())
  32. }
  33. if status == 3:
  34. parmas = {
  35. "collect_task_allocate_id": self.task_id, "status": status, "finish_status": 1,
  36. "real_count": self.crawl_count, "end_time": int(time.time()),
  37. }
  38. if status == 4:
  39. parmas = {
  40. "collect_task_allocate_id": self.task_id, "status": status, "finish_status": 0,
  41. "end_time": int(time.time())}
  42. # url = "http://scheduletest.dfwy.tech/api/collect_equipment_execute/result_report"
  43. url = "http://scheduleapi.findit.ltd/api/collect_equipment_execute/result_report"
  44. try:
  45. res = requests.get(url, params=parmas, timeout=20)
  46. res.raise_for_status()
  47. logger.info("状态上报: %s", res.text[:500])
  48. except Exception as e:
  49. logger.warning(f"状态上报失败: {e}")
  50. def heartbeat_task(self):
  51. url = "https://scheduleapi.findit.ltd/api/collect_equipment_execute/heartbeat"
  52. params = {
  53. "collect_task_allocate_id": self.task_id,
  54. }
  55. try:
  56. res = requests.get(url, params=params, timeout=20)
  57. logger.info("心跳任务上报成功")
  58. except Exception as e:
  59. logger.info(f"心跳任务上报失败{str(e)}")
  60. def get_task(self):
  61. """获取当前设备绑定的淘宝待执行任务(快照版)。"""
  62. sql = """
  63. SELECT t.*
  64. FROM `retrieve_collect_task_allocate` t
  65. INNER JOIN `retrieve_collect_equipment_account` a
  66. ON t.`collect_equipment_account_id` = a.`id`
  67. WHERE a.`device_id` = %s
  68. AND t.`platform` = 1
  69. AND t.`status` = 1
  70. AND t.`snapshot_collect_status` = 0
  71. LIMIT 1
  72. """
  73. task_list = self.db_online.select_data(sql, (TB_DEVICE_ID,))
  74. if not task_list:
  75. return {}
  76. task_dict = task_list[0]
  77. self.task_id = task_dict["id"]
  78. return task_dict
  79. def run(self):
  80. spider_schedule = CrawlerScheduler(TB_DEVICE_ID, 1)
  81. spider_schedule.start()
  82. time.sleep(3)
  83. while 1:
  84. print(f'not spider_schedule.end:{not spider_schedule.end}')
  85. if not spider_schedule.end:
  86. print('进入工作')
  87. self.task_dict = spider_schedule.get_task()
  88. if not self.task_dict:
  89. logger.info(f"{platform_name}暂无任务")
  90. time.sleep(35)
  91. continue
  92. print(self.task_dict)
  93. self.task_dict.update({'collect_equipment_account_id': TB_DEVICE_ID})
  94. self.crawl_count, is_success, self.driver, self.cumulative_pages, self.cumulative_items, self.cumulative_stored, self.cumulative_skipped = TaobaoCrawl(self.task_dict, spider_schedule, self.driver, self.cumulative_pages, self.cumulative_items, self.cumulative_stored, self.cumulative_skipped).run()
  95. spider_schedule.stop()
  96. else:
  97. spider_schedule.start()
  98. print('休息')
  99. time.sleep(35)
  100. time.sleep(30)
  101. if __name__ == '__main__':
  102. # 每10分钟执行一次
  103. while True:
  104. TaobaoMain().run()
  105. interval_time = random.randint(1200, 1800)
  106. logger.info(f"程序睡眠{interval_time}秒后继续执行")
  107. time.sleep(interval_time)