Skip to content

Commit

Permalink
refactor: init files
Browse files Browse the repository at this point in the history
  • Loading branch information
EverythingSuckz committed Feb 8, 2022
1 parent 8cbc9cb commit fe6ba2a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
5 changes: 0 additions & 5 deletions WebStreamer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,5 @@
from .vars import Var
from WebStreamer.bot.clients import StreamBot

print("\n")
print("------------------- Initializing Telegram Bot -------------------")

StreamBot.start()
bot_info = StreamBot.get_me()
__version__ = 2.2
StartTime = time.time()
43 changes: 29 additions & 14 deletions WebStreamer/__main__.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,74 @@
# This file is a part of TG-FileStreamBot
# Coding : Jyothis Jayanth [@EverythingSuckz]

import sys
import asyncio
import logging
from .vars import Var
from aiohttp import web
from pyrogram import idle
from WebStreamer import utils
from WebStreamer import bot_info
from WebStreamer import StreamBot
from WebStreamer.server import web_server
from WebStreamer.bot.clients import initialize_clients


logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logging.getLogger("pyrogram").setLevel(logging.ERROR)
logging.getLogger("aiohttp").setLevel(logging.ERROR)
logging.getLogger("pyrogram").setLevel(logging.ERROR)
logging.getLogger("aiohttp.web").setLevel(logging.ERROR)

loop = asyncio.get_event_loop()
server = web.AppRunner(web_server())

loop = asyncio.get_event_loop()

async def start_services():
print("----------------------------- DONE -----------------------------")
print()
print("-------------------- Initializing Telegram Bot --------------------")
await StreamBot.start()
bot_info = await StreamBot.get_me()
StreamBot.username = bot_info.username
print("------------------------------ DONE ------------------------------")
print()
print(
"----------------------------- Initializing Clients -----------------------------"
"---------------------- Initializing Clients ----------------------"
)
await initialize_clients()
print("----------------------------- DONE -----------------------------")
print("------------------------------ DONE ------------------------------")
if Var.ON_HEROKU:
print("------------------ Starting Keep Alive Service ------------------")
print()
asyncio.create_task(utils.ping_server())
print("-------------------- Initalizing Web Server --------------------")
app = web.AppRunner(await web_server())
await app.setup()
print("--------------------- Initalizing Web Server ---------------------")
await server.setup()
bind_address = "0.0.0.0" if Var.ON_HEROKU else Var.BIND_ADDRESS
await web.TCPSite(app, bind_address, Var.PORT).start()
print("----------------------------- DONE -----------------------------")
await web.TCPSite(server, bind_address, Var.PORT).start()
print("------------------------------ DONE ------------------------------")
print()
print("----------------------- Service Started -----------------------")
print("------------------------- Service Started -------------------------")
print(" bot =>> {}".format(bot_info.first_name))
if bot_info.dc_id:
print(" DC ID =>> {}".format(str(bot_info.dc_id)))
print(" server ip =>> {}".format(bind_address, Var.PORT))
if Var.ON_HEROKU:
print(" app running on =>> {}".format(Var.FQDN))
print("---------------------------------------------------------------")
print("------------------------------------------------------------------")
await idle()

async def cleanup():
await server.cleanup()
await StreamBot.stop()

if __name__ == "__main__":
try:
loop.run_until_complete(start_services())
except KeyboardInterrupt:
logging.info("----------------------- Service Stopped -----------------------")
pass
except Exception as err:
logging.error(err.with_traceback(None))
finally:
loop.run_until_complete(cleanup())
loop.stop()
print("------------------------ Stopped Services ------------------------")
2 changes: 1 addition & 1 deletion WebStreamer/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .stream_routes import routes


async def web_server():
def web_server():
web_app = web.Application(client_max_size=30000000)
web_app.add_routes(routes)
return web_app
4 changes: 2 additions & 2 deletions WebStreamer/server/stream_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from aiohttp.http_exceptions import BadStatusLine
from WebStreamer.bot import multi_clients, work_loads
from WebStreamer.server.exceptions import FIleNotFound, InvalidHash
from WebStreamer import Var, utils, StartTime, __version__, bot_info
from WebStreamer import Var, utils, StartTime, __version__, StreamBot


routes = web.RouteTableDef()
Expand All @@ -22,7 +22,7 @@ async def root_route_handler(_):
{
"server_status": "running",
"uptime": utils.get_readable_time(time.time() - StartTime),
"telegram_bot": "@" + bot_info.username,
"telegram_bot": "@" + StreamBot.username,
"connected_bots": len(multi_clients),
"loads": dict(
("bot" + str(c + 1), l)
Expand Down

0 comments on commit fe6ba2a

Please sign in to comment.