start_run_taobao.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.taobao.taobao_crawl import TaobaoCrawl
  7. import schedule
  8. from commons.feishu_webhook import send_text
  9. platform_name = "淘宝"
  10. class TaobaoMain:
  11. def __init__(self):
  12. # self.db_online = MySQLPool39()
  13. self.db_online = MySQLPoolOnline()
  14. self.crawl_count = ""
  15. self.task_id = ""
  16. self.task_dict = None
  17. def get_status(self, status):
  18. if status not in (2, 3, 4):
  19. logger.warning(f"未知状态值: {status}, 跳过状态上报")
  20. return
  21. if status == 2:
  22. parmas = {
  23. "collect_task_allocate_id": self.task_id, "status": status, "finish_status": 0,
  24. "start_time": int(time.time())
  25. }
  26. if status == 3:
  27. parmas = {
  28. "collect_task_allocate_id": self.task_id, "status": status, "finish_status": 1,
  29. "real_count": self.crawl_count, "end_time": int(time.time()),
  30. }
  31. if status == 4:
  32. parmas = {
  33. "collect_task_allocate_id": self.task_id, "status": status, "finish_status": 0,
  34. "end_time": int(time.time())}
  35. # url = "http://scheduletest.dfwy.tech/api/collect_equipment_execute/result_report"
  36. url = "http://scheduleapi.findit.ltd/api/collect_equipment_execute/result_report"
  37. try:
  38. res = requests.get(url, params=parmas, timeout=20)
  39. res.raise_for_status()
  40. logger.info("状态上报: %s", res.text[:500])
  41. except Exception as e:
  42. logger.warning(f"状态上报失败: {e}")
  43. def get_task(self):
  44. """ 获取任务 """
  45. sql_task = f""" select `id`,`collect_task_id`,`company_id`,`product_name`,`product_specs`,`product_keyword`,`product_brand`,`sampling_cycle`,`sampling_start_time`,`sampling_end_time`,`collect_equipment_account_id`,`collect_region_id`,`collect_equipment_id`,`collect_round` from `retrieve_collect_task_allocate` where `platform`=1 and `status`=1 limit 1 """
  46. task_list = self.db_online.select_data(sql_task)
  47. if not task_list:
  48. return {}
  49. task_dict = task_list[0]
  50. self.task_id = task_dict["id"]
  51. print(task_dict)
  52. return task_dict
  53. def run(self):
  54. self.task_dict = self.get_task()
  55. if not self.task_dict:
  56. logger.info(f"{platform_name}暂无任务")
  57. return
  58. logger.info(self.task_dict)
  59. self.get_status(2)
  60. # self.crawl_count, is_success = TaobaoCrawl(self.task_dict).run()
  61. # send_text(
  62. # f"""{str(time.strftime("%Y-%m-%d %H:%M:%S"))} 通知:\n平台: {platform_name}, 药品: {self.task_dict.get("product_name")}, 爬取数据: {self.crawl_count}条""")
  63. # if is_success:
  64. # self.get_status(3)
  65. # else:
  66. # self.get_status(4)
  67. def run_schedule_task():
  68. """执行淘宝爬虫任务"""
  69. H = time.strftime("%H")
  70. if int(H) < 7:
  71. return
  72. try:
  73. logger.info(f"开始执行{platform_name}爬虫任务")
  74. TaobaoMain().run()
  75. logger.info(f"{platform_name}爬虫任务执行完成")
  76. except Exception as e:
  77. logger.error(f"{platform_name}爬虫任务执行失败: {e}", exc_info=True)
  78. if __name__ == '__main__':
  79. TaobaoMain().run()
  80. # 每10分钟执行一次
  81. schedule.every(30).minutes.do(run_schedule_task)
  82. logger.info(f"定时任务已启动,每30分钟执行一次{platform_name}爬虫")
  83. while True:
  84. schedule.run_pending()
  85. time.sleep(3)