railbot/scheduler.py

28 lines
1.0 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.

from apscheduler.schedulers.blocking import BlockingScheduler
from parser import fetch_schedule
from db import init_db, save_schedule
from datetime import date
def daily_job():
# Створюємо таблиці, якщо їх нема
init_db()
# Парсимо обидва напрямки: tab=1 (Київ→Ніжин), tab=2 (Ніжин→Київ)
for tab in (1, 2):
entries = fetch_schedule(tab=tab, use_local=False)
print(f"DEBUG: Напрямок tab={tab}, знайдено {len(entries)} поїздів")
save_schedule(entries)
print(f"DEBUG: Збережено {len(entries)} поїздів для tab={tab}")
print(f"{date.today().isoformat()}: Розклад обох напрямків оновлено.")
if __name__ == '__main__':
sched = BlockingScheduler(timezone='Europe/Kyiv')
sched.add_job(daily_job, 'cron', hour=5, minute=0)
# Одноразове оновлення при старті
daily_job()
# Старт планувальника
sched.start()