# coding=utf-8 import asyncio import logging import os import sys # 将项目根目录添加到 sys.path project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) if project_root not in sys.path: sys.path.append(project_root) from Apps.XinDianTu.Service import XinDianTuService from Apps.XinDianTu import Opener from Apps.XinDianTu import Crawler # 配置日志 logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) logger = logging.getLogger("FullProcess") async def main(): logger.info("=== 开始全流程任务: 打开小程序 -> 爬取数据 ===") # 步骤 0: 初始化基础服务 (DB等) # 用户要求:程序启动时就完成初始化 logger.info(">>> 步骤 0: 初始化基础服务 (数据库连接)...") service = XinDianTuService() await service.init_db() try: # 步骤 1: 打开新电途小程序 logger.info(">>> 步骤 1: 启动新电途小程序...") try: # 调用 Y2 的 open_mini_program 函数 success = await Opener.open_mini_program() if not success: logger.error("❌ 无法成功打开新电途小程序,任务终止。") return except Exception as e: logger.error(f"❌ 步骤 1 执行异常: {e}") return logger.info("✅ 小程序启动成功,等待 5 秒确保界面稳定...") await asyncio.sleep(5) # 步骤 2: 执行爬取任务 logger.info(">>> 步骤 2: 开始执行场站爬取任务...") try: # 调用 Y3 的 main 函数,传入已初始化的 service # 传入 do_cleanup=False,因为已在步骤 0 完成清理 success = await Crawler.main(service=service, do_cleanup=False) if not success: logger.error("❌ 步骤 2 爬取任务失败,终止后续步骤。") return except Exception as e: logger.error(f"❌ 步骤 2 执行异常: {e}") return finally: # 确保数据库连接关闭 if service: await service.close_db() logger.info("=== 全流程任务结束 ===") if __name__ == "__main__": try: asyncio.run(main()) except KeyboardInterrupt: logger.info("程序被用户中断.") except Exception as e: logger.exception(f"主程序崩溃: {e}")