Files
aiData/T1_TeLaiDian.py
HuangHai ec680d1f92 'commit'
2026-01-17 16:02:29 +08:00

66 lines
2.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding=utf-8
import asyncio
import os
import sys
# 将项目根目录添加到 sys.path
project_root = os.path.dirname(os.path.abspath(__file__))
if project_root not in sys.path:
sys.path.append(project_root)
try:
from Apps.TeLaiDian.Service import TeLaiDianService
from Apps.TeLaiDian import Opener, Crawler, Kit
# 初始化日志文件记录
logger = Kit.setup_logger("T4_TeLaiDian", clear_old_log=True)
except KeyboardInterrupt:
print("\n🛑 用户在初始化阶段手动停止了程序。")
sys.exit(0)
except Exception as e:
print(f"❌ 初始化导入失败: {e}")
sys.exit(1)
async def run_process():
logger.info("=== 开始全流程任务 (特来电): 打开小程序 -> 爬取数据 ===")
# 步骤 0: 初始化基础服务
logger.info(">>> 步骤 0: 初始化基础服务 (数据库连接)...")
service = TeLaiDianService()
await service.init_db()
try:
# 启动前清空临时目录
Kit.clear_temp_dir()
# 步骤 1: 启动小程序
logger.info(">>> 步骤 1: 启动 特来电 小程序...")
success = await Opener.open_mini_program()
if not success:
logger.error("❌ 无法成功打开小程序,任务终止。")
return
logger.info("✅ 小程序启动成功,等待 5 秒确保界面稳定...")
await asyncio.sleep(5)
# 步骤 2: 执行爬取任务
logger.info(">>> 步骤 2: 开始执行场站爬取任务...")
# 调用 Crawler 的 main并传入 service
await Crawler.main(service=service)
logger.info("✅ 爬取任务完成!")
except Exception as e:
logger.error(f"❌ 运行异常: {e}")
finally:
if service:
await service.close_db()
logger.info("=== 全流程任务结束 ===")
if __name__ == "__main__":
try:
asyncio.run(run_process())
except KeyboardInterrupt:
logger.info("\n🛑 用户手动停止了程序 (Ctrl+C)。")
except Exception as e:
logger.exception(f"主程序崩溃: {e}")