snapshot_jd.py 4.0 KB

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