railbot/scheduler.py

34 lines
1.4 KiB
Python
Raw 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()
# Отримуємо повний розклад з сайту (Kyiv→Nizhyn)
entries = fetch_schedule(use_local=False)
# Діагностика: виведемо кількість поїздів та список зупинок
print(f"DEBUG: знайдено поїздів: {len(entries)}")
for e in entries:
print(f"DEBUG: Поїзд {e['train_number']} днів {e['days']}, станцій: {len(e['times'])}")
# Зберігаємо розклад у БД (додає поїзди, станції та часи)
save_schedule('kyiv_nizhyn', entries)
print(f"{date.today().isoformat()}: збережено розклад для {len(entries)} поїздів.")
if __name__ == '__main__':
# Налаштування планувальника: щодня о 05:00 (Europe/Kyiv)
sched = BlockingScheduler(timezone='Europe/Kyiv')
sched.add_job(daily_job, 'cron', hour=5, minute=0)
# Одноразове оновлення та діагностика при старті
daily_job()
# Запуск планувальника
sched.start()