Merge commit 'c2bd4d3b2c890787bce353344028c1e270c2c671'

This commit is contained in:
xbh 2025-10-30 10:19:31 +08:00
commit 560581c7ee

View File

@ -117,14 +117,24 @@ class DouyinAutoScheduler:
duration_s=60 duration_s=60
) )
print("📁 开始执行抓取任务...")
logging.info("📁 开始执行抓取任务...") logging.info("📁 开始执行抓取任务...")
scraper.run() scraper.run()
print("✅ 抖音播放量抓取任务执行成功")
logging.info("✅ 抖音播放量抓取任务执行成功") logging.info("✅ 抖音播放量抓取任务执行成功")
# 数据抓取完成后,自动生成当日榜单 # 数据抓取完成后,自动生成当日榜单
self.generate_daily_rankings() self.generate_daily_rankings()
# 任务完成后立即显示下次执行时间
print("🎯 任务完成,准备下次执行...")
self.show_next_run()
print("💡 定时器正在等待中,将在整点自动执行任务...")
logging.info("🎯 任务完成,准备下次执行...")
logging.info("💡 定时器正在等待中,将在整点自动执行任务...")
except Exception as e: except Exception as e:
logging.error(f"💥 执行任务时发生异常: {e}") logging.error(f"💥 执行任务时发生异常: {e}")
import traceback import traceback
@ -149,10 +159,12 @@ class DouyinAutoScheduler:
# 删除当天已有的榜单数据 # 删除当天已有的榜单数据
rankings_collection.delete_many({"date": today_str}) rankings_collection.delete_many({"date": today_str})
print(f"🗑️ 已清理 {today_str} 的旧榜单数据")
logging.info(f"🗑️ 已清理 {today_str} 的旧榜单数据") logging.info(f"🗑️ 已清理 {today_str} 的旧榜单数据")
# 获取今天和昨天的榜单数据进行对比 # 获取今天和昨天的榜单数据进行对比
try: try:
print("🔄 正在生成时间轴对比榜单...")
logging.info("🔄 正在生成时间轴对比榜单...") logging.info("🔄 正在生成时间轴对比榜单...")
# 获取最新批次的数据 # 获取最新批次的数据
@ -320,6 +332,11 @@ class DouyinAutoScheduler:
# 统计信息 # 统计信息
new_count = sum(1 for item in comprehensive_ranking["data"] if item["timeline_data"]["is_new"]) new_count = sum(1 for item in comprehensive_ranking["data"] if item["timeline_data"]["is_new"])
print(f"✅ 时间轴对比榜单生成成功")
print(f"📊 总计 {len(comprehensive_ranking['data'])} 条记录")
print(f"🆕 新上榜 {new_count}")
print(f"🔄 对比基准日期: {yesterday_str}")
logging.info(f"✅ 时间轴对比榜单生成成功") logging.info(f"✅ 时间轴对比榜单生成成功")
logging.info(f"📊 总计 {len(comprehensive_ranking['data'])} 条记录") logging.info(f"📊 总计 {len(comprehensive_ranking['data'])} 条记录")
logging.info(f"🆕 新上榜 {new_count}") logging.info(f"🆕 新上榜 {new_count}")
@ -354,7 +371,17 @@ class DouyinAutoScheduler:
jobs = schedule.get_jobs() jobs = schedule.get_jobs()
if jobs: if jobs:
next_run = jobs[0].next_run next_run = jobs[0].next_run
logging.info(f"⏰ 下次执行时间: {next_run}") current_time = datetime.now()
wait_seconds = (next_run - current_time).total_seconds()
wait_minutes = int(wait_seconds // 60)
wait_hours = int(wait_minutes // 60)
remaining_minutes = wait_minutes % 60
print(f"💡 定时器运行中,下次执行:{next_run.strftime('%Y-%m-%d %H:%M:%S')} (还有{wait_hours}h{remaining_minutes}m)")
print(f"⏳ 距离下次执行:{wait_minutes} 分钟 ({int(wait_seconds)} 秒)")
logging.info(f"💡 定时器运行中,下次执行:{next_run.strftime('%Y-%m-%d %H:%M:%S')} (还有{wait_hours}h{remaining_minutes}m)")
logging.info(f"⏳ 距离下次执行:{wait_minutes} 分钟 ({int(wait_seconds)} 秒)")
def run_once(self): def run_once(self):
"""立即执行一次""" """立即执行一次"""
@ -374,20 +401,32 @@ class DouyinAutoScheduler:
def start_scheduler(self): def start_scheduler(self):
"""启动定时器""" """启动定时器"""
self.is_running = True self.is_running = True
last_status_time = int(time.time()) # 设置为当前时间1分钟后开始显示状态
print("🚀 抖音播放量自动抓取定时器已启动")
print("⏰ 执行时间:每小时整点执行抖音播放量抓取")
print("⏹️ 按 Ctrl+C 停止定时器")
logging.info("🚀 抖音播放量自动抓取定时器已启动") logging.info("🚀 抖音播放量自动抓取定时器已启动")
logging.info(f"⏰ 执行时间:每小时整点执行抖音播放量抓取") logging.info(f"⏰ 执行时间:每小时整点执行抖音播放量抓取")
logging.info("⏹️ 按 Ctrl+C 停止定时器") logging.info("⏹️ 按 Ctrl+C 停止定时器")
# 启动时显示一次下次执行时间
self.show_next_run()
try: try:
while self.is_running: while self.is_running:
schedule.run_pending() schedule.run_pending()
time.sleep(1) time.sleep(1)
# 每分钟显示一次状态 # 每1分钟显示一次状态
if int(time.time()) % 600 == 0: current_time = int(time.time())
if current_time - last_status_time >= 60: # 60秒 = 1分钟
self.show_next_run() self.show_next_run()
last_status_time = current_time
except KeyboardInterrupt: except KeyboardInterrupt:
print("\n⏹️ 定时器已停止")
logging.info("\n⏹️ 定时器已停止") logging.info("\n⏹️ 定时器已停止")
self.is_running = False self.is_running = False