stremio / Backend /__main__.py
Karan Adhikari
๐Ÿš€ Release v3.1.0: Add auto catalog generation
bed5e23 unverified
Raw History Blame
3.64 kB
from asyncio import get_event_loop, sleep as asleep
import asyncio
import logging
from traceback import format_exc
from pyrogram import idle
from Backend import __version__, db
from Backend.helper.pinger import ping
from Backend.logger import LOGGER
from Backend.config import Telegram
from Backend.fastapi import server
from Backend.helper.pyro import restart_notification, setup_bot_commands
from Backend.pyrofork.bot import Helper, StreamBot
from Backend.pyrofork.clients import initialize_clients
from Backend.pyrofork.plugins.channels import _load_channels_from_db
from Backend.helper.subscription_checker import subscription_checker_loop
from Backend.helper.link_checker import DeadLinkChecker
from Backend.fastapi.main import app
from Backend.helper.auto_catalog import (
start_auto_catalog_sync_background, start_auto_catalog_interval_loop,
AUTO_SYNC_DELAY_SECONDS, AUTO_CATALOG_ON_STARTUP,
AUTO_CATALOG_FULL_REBUILD_ON_STARTUP
)
loop = get_event_loop()
async def start_services():
try:
LOGGER.info(f"Initializing Telegram-Stremio v-{__version__}")
await asleep(1.2)
await db.connect()
await asleep(1.2)
await StreamBot.start()
StreamBot.username = StreamBot.me.username
LOGGER.info(f"Bot Client : [@{StreamBot.username}]")
await asleep(1.2)
await Helper.start()
Helper.username = Helper.me.username
LOGGER.info(f"Helper Bot Client : [@{Helper.username}]")
await asleep(1.2)
LOGGER.info("Initializing Multi Clients...")
await initialize_clients()
await asleep(2)
await _load_channels_from_db()
await asleep(2)
await setup_bot_commands(StreamBot)
await asleep(2)
LOGGER.info('Initializing Telegram-Stremio Web Server...')
await restart_notification()
loop.create_task(server.serve())
loop.create_task(ping())
link_checker_task = DeadLinkChecker(db, app, check_interval_hours=24)
loop.create_task(link_checker_task.start())
if AUTO_CATALOG_ON_STARTUP:
loop.create_task(start_auto_catalog_sync_background(
db,
delay_seconds=AUTO_SYNC_DELAY_SECONDS,
full_rebuild=AUTO_CATALOG_FULL_REBUILD_ON_STARTUP,
))
loop.create_task(start_auto_catalog_interval_loop(db))
if Telegram.SUBSCRIPTION:
loop.create_task(subscription_checker_loop(StreamBot))
LOGGER.info("Subscription Checker Task Started.")
LOGGER.info("Telegram-Stremio Started Successfully!")
await idle()
except Exception:
LOGGER.error("Error during startup:\n" + format_exc())
async def stop_services():
try:
LOGGER.info("Stopping services...")
pending_tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
for task in pending_tasks:
task.cancel()
await asyncio.gather(*pending_tasks, return_exceptions=True)
await StreamBot.stop()
await Helper.stop()
await db.disconnect()
LOGGER.info("Services stopped successfully.")
except Exception:
LOGGER.error("Error during shutdown:\n" + format_exc())
if __name__ == '__main__':
try:
loop.run_until_complete(start_services())
except KeyboardInterrupt:
LOGGER.info('Service Stopping...')
except Exception:
LOGGER.error(format_exc())
finally:
loop.run_until_complete(stop_services())
loop.stop()
logging.shutdown()