start_run_taobao.py 4.1 KB

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