File size: 4,435 Bytes
1404692 36dde81 1404692 36dde81 1404692 36dde81 1404692 36dde81 07f63a8 f697c3b 36dde81 6f85dc9 36dde81 1404692 36dde81 1404692 36dde81 1404692 36dde81 07f63a8 36dde81 07f63a8 1404692 36dde81 c12bb0b 36dde81 1404692 36dde81 1404692 36dde81 1404692 b2192d0 36dde81 f697c3b bed5e23 07f63a8 36dde81 1404692 36dde81 1404692 36dde81 1404692 36dde81 1404692 36dde81 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | import asyncio
import logging
import secrets
from traceback import format_exc
from pyrogram import idle
from starlette.middleware.sessions import SessionMiddleware
from Backend import __version__, db
from Backend.fastapi import server
from Backend.fastapi.main import app
from Backend.helper import subscription_task_manager
from Backend.helper.link_checker import DeadLinkChecker
from Backend.helper.own_bots import reload_helpers, sweep_copies, warm_pools
from Backend.helper.pinger import ping
from Backend.helper.pyro import restart_notification, setup_bot_commands
from Backend.helper.scan_manager import dbcheck_manager, duplicate_manager, scan_manager
from Backend.helper.session_auth import get_active_session_string
from Backend.helper.settings_manager import SettingsManager
from Backend.logger import LOGGER
import Backend.pyrofork.bot as botmod
from Backend.pyrofork.bot import StreamBot
from Backend.pyrofork.clients import initialize_clients
loop = asyncio.get_event_loop()
#----- Boot every subsystem then idle the bot
async def start_services():
try:
LOGGER.info(f"Initializing Telegram-Stremio v-{__version__}")
await asyncio.sleep(1.2)
await db.connect()
await asyncio.sleep(1.2)
await SettingsManager.initialize(db)
app.add_middleware(SessionMiddleware, secret_key=SettingsManager.current().session_secret or secrets.token_hex(32))
await asyncio.sleep(0.5)
await scan_manager.load(db)
dbcheck_manager.bind_db(db)
duplicate_manager.bind_db(db)
await asyncio.sleep(0.3)
await db.reload_extra_databases(SettingsManager.current().extra_databases)
await asyncio.sleep(0.5)
await StreamBot.start()
StreamBot.username = StreamBot.me.username
LOGGER.info(f"Bot Client : [@{StreamBot.username}]")
await asyncio.sleep(1.2)
if botmod.Userbot is None:
stored_session = await get_active_session_string()
if stored_session:
botmod.build_userbot(stored_session)
LOGGER.info("Loaded Userbot session from encrypted storage.")
if botmod.Userbot is not None:
await botmod.Userbot.start()
botmod.Userbot.username = botmod.Userbot.me.username
LOGGER.info(f"Userbot Client : [@{botmod.Userbot.username}]")
else:
LOGGER.info("Userbot not configured — running with StreamBot only.")
await asyncio.sleep(1.2)
LOGGER.info("Initializing Multi Clients...")
await initialize_clients()
from Backend.fastapi.routes.stream_routes import warm_streamers
warm_streamers()
LOGGER.info(f"Helper bots: {await reload_helpers()} active")
loop.create_task(sweep_copies())
loop.create_task(warm_pools())
await asyncio.sleep(2)
await setup_bot_commands(StreamBot)
await asyncio.sleep(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())
await subscription_task_manager.sync(StreamBot)
LOGGER.info("Telegram-Stremio Started Successfully!")
await idle()
except Exception:
LOGGER.error("Error during startup:\n" + format_exc())
#----- Cancel pending tasks and shut clients down
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()
if botmod.Userbot is not None:
await botmod.Userbot.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()
|