start_run_jd.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import json
  2. import time
  3. import requests
  4. from commons.Logger import logger
  5. from commons.conn_mysql import MySQLPoolOnline, MySQLPool39
  6. from spiders.jd.jd_auto_crawl import JdCrawlerV2
  7. import schedule
  8. from commons.feishu_webhook import send_text
  9. import random
  10. from commons.config import JD_DEVICE_ID
  11. platform_name = "京东"
  12. class JdMain:
  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 = self.get_task()
  19. def get_status(self, status):
  20. if status not in (2, 3, 4):
  21. logger.warning(f"未知状态值: {status}, 跳过状态上报")
  22. return
  23. if status == 2:
  24. parmas = {
  25. "collect_task_allocate_id": self.task_id, "status": status, "finish_status": 0,
  26. "start_time": int(time.time())
  27. }
  28. if status == 3:
  29. parmas = {
  30. "collect_task_allocate_id": self.task_id, "status": status, "finish_status": 1,
  31. "real_count": self.crawl_count, "end_time": int(time.time()),
  32. }
  33. if status == 4:
  34. parmas = {
  35. "collect_task_allocate_id": self.task_id, "status": status, "finish_status": 0,
  36. "end_time": int(time.time())}
  37. # url = "http://scheduletest.dfwy.tech/api/collect_equipment_execute/result_report"
  38. url = "http://scheduleapi.findit.ltd/api/collect_equipment_execute/result_report"
  39. try:
  40. res = requests.get(url, params=parmas, timeout=20)
  41. res.raise_for_status()
  42. logger.info("状态上报: %s", res.text)
  43. except Exception as e:
  44. logger.warning(f"状态上报失败: {e}")
  45. def get_task(self):
  46. """获取当前设备绑定的京东待执行任务。"""
  47. sql = """
  48. SELECT t.*
  49. FROM `retrieve_collect_task_allocate` t
  50. INNER JOIN `retrieve_collect_equipment_account` a
  51. ON t.`collect_equipment_account_id` = a.`id`
  52. WHERE a.`device_id` = %s
  53. AND t.`platform` = 2
  54. AND t.`status` = 1
  55. LIMIT 1
  56. """
  57. task_list = self.db_online.select_data(sql, (JD_DEVICE_ID,))
  58. print(task_list)
  59. if not task_list:
  60. return {}
  61. task_dict = task_list[0]
  62. self.task_id = task_dict["id"]
  63. print(task_dict)
  64. return task_dict
  65. def heartbeat_task(self):
  66. url = "https://scheduleapi.findit.ltd/api/collect_equipment_execute/heartbeat"
  67. params = {
  68. "collect_task_allocate_id": self.task_id,
  69. }
  70. try:
  71. res = requests.get(url, params=params, timeout=20)
  72. logger.info("心跳任务上报成功")
  73. except Exception as e:
  74. logger.info("心跳任务上报失败")
  75. def run(self):
  76. self.task_dict = self.get_task()
  77. if not self.task_dict:
  78. logger.info(f"{platform_name}暂无任务")
  79. return
  80. self.get_status(2)
  81. self.crawl_count, is_success = JdCrawlerV2(self.task_dict).run()
  82. self.heartbeat_task()
  83. # send_text(
  84. # f"""{str(time.strftime("%Y-%m-%d %H:%M:%S"))} 通知:\n平台: {platform_name}, 药品: {self.task_dict.get("product_brand") + " " + self.task_dict.get("product_name") + " " + self.task_dict.get("product_specs")}, 爬取数据: {self.crawl_count}条""")
  85. if is_success:
  86. self.get_status(3)
  87. else:
  88. self.get_status(4)
  89. if __name__ == '__main__':
  90. # task_dict= {"id": 1622, 'collect_task_id': 4596, 'company_id': 8, 'product_name': '小儿氨酚烷胺颗粒',
  91. # 'product_specs': '', 'product_keyword': '', 'product_brand': '可复美', 'sampling_cycle': 1,
  92. # 'sampling_start_time': 1778083200, 'sampling_end_time': 1778342399, 'collect_equipment_account_id': 15,
  93. # 'collect_region_id': 0, 'collect_equipment_id': 25, 'collect_round': 2,"start_page":4,"end_page":10}
  94. # JdCrawlerV2(task_dict).run()
  95. # 每10分钟执行一次
  96. while True:
  97. JdMain().run()
  98. interval_time = random.randint(1200, 1800)
  99. logger.info(f"程序睡眠{interval_time}秒后继续执行")
  100. time.sleep(interval_time)