start_run_taobao.py 4.3 KB

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