fsb / FileStream /__main__.py
BinaryONe
Image Enhancement
d9352c1
Raw
History Blame Contribute Delete
3.56 kB
import sys
import asyncio
import logging
import traceback
from aiohttp import web
from pyrogram import idle
import logging.handlers as handlers
# ------------------Local Imports----------------------#
from FileStream.bot import FileStream
from FileStream.Tools import Time_ISTKolNow
from FileStream.config import Telegram, Server
from FileStream.bot.clients import initialize_clients
from FileStream.server import web_server, clear_inactive_clients
# Configure logging
logging.basicConfig(
level=logging.INFO,
datefmt="%d/%m/%Y %H:%M:%S",
format='[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(stream=sys.stdout),
handlers.RotatingFileHandler("streambot.log", mode="a", maxBytes=104857600, backupCount=2, encoding="utf-8")
],
)
logging.getLogger("aiohttp").setLevel(logging.ERROR)
logging.getLogger("pyrogram").setLevel(logging.ERROR)
logging.getLogger("aiohttp.web").setLevel(logging.ERROR)
server = web.AppRunner(web_server()) # Initialize the app runner
loop = asyncio.get_event_loop() # Ensure the event loop is retrieved correctly
async def start_services():
print(
"****\n"
+ (
"------------------ Starting as Secondary Server ------------------"
if Telegram.SECONDARY
else "------------------- Starting as Primary Server -------------------"
)
+ "\n****\n-------------------- Initializing Telegram Bot --------------------"
)
await FileStream.start()
bot_info = await FileStream.get_me()
FileStream.id = bot_info.id
FileStream.username = bot_info.username
FileStream.fname = bot_info.first_name
#[ await FileStream.get_chat(int(chat_id)) for chat_id in Telegram.DATA_SOURCES + [Telegram.FLOG_CHANNEL] ]
#await FileStream.get_users(Telegram.DATA_SOURCES + [Telegram.FLOG_CHANNEL])
print("------------------------------ DONE ------------------------------\n")
print("---------------------- Initializing Clients ----------------------")
await initialize_clients()
print("------------------------------ DONE ------------------------------\n\n")
print("--------------------- Initializing Web Server ---------------------")
await server.setup() # Setup the web server
await web.TCPSite(server, Server.BIND_ADDRESS, Server.PORT).start()
print("------------------------------ DONE ------------------------------\n\n")
# Start the background task to clear inactive clients after web server setup
#asyncio.create_task(clear_inactive_clients()) # Now this works as event loop is running
print(
f"------------------------- Service Started -------------------------\n"
f"Bot =>> {bot_info.first_name}\n" +
(f"DC ID =>> {bot_info.dc_id}\n" if bot_info.dc_id else "") +
f"URL =>> {Server.URL}\n"
"------------------------------------------------------------------"
)
await idle() # Keep the bot alive
async def cleanup():
await server.cleanup()
await FileStream.stop()
if __name__ == "__main__":
try:
loop.run_until_complete(start_services()) # Run the services asynchronously
except KeyboardInterrupt:
print("\n------------------ Stopping Services KeyBoard Interrupt -----------------")
except Exception as err:
logging.error(traceback.format_exc())
finally:
loop.run_until_complete(cleanup())
loop.stop()
print("------------------------ Stopped Services ------------------------")