Skip to content

Commit

Permalink
multi client [dev]
Browse files Browse the repository at this point in the history
  • Loading branch information
eyMarv committed Jan 3, 2022
1 parent 788332f commit 3571cf9
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 40 deletions.
7 changes: 4 additions & 3 deletions WebStreamer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

import time
from .vars import Var
from WebStreamer.bot import StreamBot
from WebStreamer.bot.clients import *

print('\n')
print('------------------- Initalizing Telegram Bot -------------------')

StreamBot.start()
for x in [MultiCli1, MultiCli2, MultiCli3]:
if x:
x.start()
bot_info = StreamBot.get_me()
__version__ = 1.06
StartTime = time.time()


12 changes: 1 addition & 11 deletions WebStreamer/bot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
# This file is a part of TG-FileStreamBot
# Coding : Jyothis Jayanth [@EverythingSuckz]

from pyrogram import Client
from ..vars import Var

StreamBot = Client(
session_name= 'Web Streamer',
api_id=Var.API_ID,
api_hash=Var.API_HASH,
bot_token=Var.BOT_TOKEN,
sleep_threshold=Var.SLEEP_THRESHOLD,
workers=Var.WORKERS
)
from .clients import StreamBot, StreamQu, MultiQu1, MultiQu2, MultiQu3, MultiCli1, MultiCli2, MultiCli3
61 changes: 61 additions & 0 deletions WebStreamer/bot/clients.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# This file is a part of TG-FileStreamBot
# Coding : Jyothis Jayanth [@EverythingSuckz]

from pyrogram import Client
from asyncio import Queue
from ..vars import Var

StreamBot = Client(
session_name= 'WebStreamer',
api_id=Var.API_ID,
api_hash=Var.API_HASH,
bot_token=Var.BOT_TOKEN,
sleep_threshold=Var.SLEEP_THRESHOLD,
workers=Var.WORKERS
)
StreamQu = Queue()
for x in range(1):
StreamQu.put_nowait(x)
MultiQu1 = None
MultiQu2 = None
MultiQu3 = None
MultiCli1 = None
MultiCli2 = None
MultiCli3 = None
if Var.MULTI_CLIENT:
if Var.MULTI_TOK1:
MultiCli1 = Client(
session_name= ':memory:',
api_id=Var.API_ID,
api_hash=Var.API_HASH,
bot_token=Var.MULTI_TOK1,
sleep_threshold=Var.SLEEP_THRESHOLD,
no_updates=True
)
MultiQu1 = Queue()
for x in range(1):
MultiQu1.put_nowait(x)
if MultiCli1 and Var.MULTI_TOK2:
MultiCli2 = Client(
session_name= ':memory:',
api_id=Var.API_ID,
api_hash=Var.API_HASH,
bot_token=Var.MULTI_TOK2,
sleep_threshold=Var.SLEEP_THRESHOLD,
no_updates=True
)
MultiQu2 = Queue()
for x in range(1):
MultiQu2.put_nowait(x)
if MultiCli2 and Var.MULTI_TOK3:
MultiCli3 = Client(
session_name= ':memory:',
api_id=Var.API_ID,
api_hash=Var.API_HASH,
bot_token=Var.MULTI_TOK3,
sleep_threshold=Var.SLEEP_THRESHOLD,
no_updates=True
)
MultiQu3 = Queue()
for x in range(1):
MultiQu3.put_nowait(x)
28 changes: 24 additions & 4 deletions WebStreamer/server/stream_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import mimetypes
from aiohttp import web
from WebStreamer.vars import Var
from WebStreamer.bot import StreamBot
from WebStreamer.bot.clients import *
from WebStreamer import StartTime, __version__, bot_info
from WebStreamer.utils.time_format import get_readable_time
from WebStreamer.utils.custom_dl import TGCustomYield, chunk_size, offset_fix
from asyncio import QueueEmpty
from random import choice as rchoice

routes = web.RouteTableDef()

Expand All @@ -38,8 +40,26 @@ async def stream_handler(request):

async def media_streamer(request, message_id: int):
range_header = request.headers.get('Range', 0)
media_msg = await StreamBot.get_messages(Var.BIN_CHANNEL, message_id)
file_properties = await TGCustomYield().generate_file_properties(media_msg)
try:
StreamQu.get_nowait()
clien = StreamBot
except QueueEmpty:
try:
MultiQu1.get_nowait()
clien = MultiCli1
except QueueEmpty:
try:
MultiQu2.get_nowait()
clien = MultiCli2
except QueueEmpty:
try:
MultiQu3.get_nowait()
clien = MultiCli3
except QueueEmpty:
clien = rchoice([StreamBot, MultiCli1, MultiCli2, MultiCli3])
tg_connect = TGCustomYield(clien)
media_msg = await clien.get_messages(Var.BIN_CHANNEL, message_id)
file_properties = await tg_connect.generate_file_properties(media_msg)
file_size = file_properties.file_size

if range_header:
Expand All @@ -57,7 +77,7 @@ async def media_streamer(request, message_id: int):
first_part_cut = from_bytes - offset
last_part_cut = (until_bytes % new_chunk_size) + 1
part_count = math.ceil(req_length / new_chunk_size)
body = TGCustomYield().yield_file(media_msg, offset, first_part_cut, last_part_cut, part_count,
body = tg_connect.yield_file(media_msg, offset, first_part_cut, last_part_cut, part_count,
new_chunk_size)

mime_type = file_properties.mime_type
Expand Down
27 changes: 23 additions & 4 deletions WebStreamer/utils/custom_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import math
from typing import Union
from pyrogram.types import Message
from WebStreamer.bot import StreamBot
from WebStreamer.bot import *
from pyrogram import Client, utils, raw
from pyrogram.session import Session, Auth
from pyrogram.errors import AuthBytesInvalid
from pyrogram.file_id import FileId, FileType, ThumbnailSource
from asyncio import QueueEmpty
from random import choice as rchoice


async def chunk_size(length):
Expand All @@ -21,14 +23,15 @@ async def offset_fix(offset, chunksize):


class TGCustomYield:
def __init__(self):
def __init__(self, client: Client):
""" A custom method to stream files from telegram.
functions:
generate_file_properties: returns the properties for a media on a specific message contained in FileId class.
generate_media_session: returns the media session for the DC that contains the media file on the message.
yield_file: yield a file from telegram servers for streaming.
"""
self.main_bot = StreamBot
self.main_bot = client


@staticmethod
async def generate_file_properties(msg: Message):
Expand Down Expand Up @@ -151,7 +154,23 @@ async def get_location(file_id: FileId):

async def yield_file(self, media_msg: Message, offset: int, first_part_cut: int,
last_part_cut: int, part_count: int, chunk_size: int) -> Union[str, None]: #pylint: disable=unsubscriptable-object
client = self.main_bot
try:
StreamQu.get_nowait()
self.main_bot = StreamBot
except QueueEmpty:
try:
MultiQu1.get_nowait()
self.main_bot = MultiCli1
except QueueEmpty:
try:
MultiQu2.get_nowait()
self.main_bot = MultiCli2
except QueueEmpty:
try:
MultiQu3.get_nowait()
self.main_bot = MultiCli3
except QueueEmpty:
self.main_bot = rchoice([StreamBot, MultiCli1, MultiCli2, MultiCli3])
data = await self.generate_file_properties(media_msg)
media_session = await self.generate_media_session(client, media_msg)

Expand Down
37 changes: 19 additions & 18 deletions WebStreamer/vars.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
# This file is a part of TG-FileStreamBot
# Coding : Jyothis Jayanth [@EverythingSuckz]

from os import getenv, environ
from dotenv import load_dotenv

load_dotenv()
from os import environ

class Var(object):
API_ID = int(getenv('API_ID'))
API_HASH = str(getenv('API_HASH'))
BOT_TOKEN = str(getenv('BOT_TOKEN'))
SLEEP_THRESHOLD = int(getenv('SLEEP_THRESHOLD', '60'))
WORKERS = int(getenv('WORKERS', '3'))
BIN_CHANNEL = int(getenv('BIN_CHANNEL', None))
PORT = int(getenv('PORT', 8080))
BIND_ADDRESS = str(getenv('WEB_SERVER_BIND_ADDRESS', '0.0.0.0'))
PING_INTERVAL = int(getenv('PING_INTERVAL', '1200')) # 20 minutes
HAS_SSL = getenv('HAS_SSL', False)
MULTI_CLIENT = bool(environ.get('MULTI_CLIENT', False))
MULTI_TOK1 = str(environ.get('MULTI_TOK1', None))
MULTI_TOK2 = str(environ.get('MULTI_TOK2', None))
MULTI_TOK3 = str(environ.get('MULTI_TOK3', None))
API_ID = int(environ.get('API_ID'))
API_HASH = str(environ.get('API_HASH'))
BOT_TOKEN = str(environ.get('BOT_TOKEN'))
SLEEP_THRESHOLD = int(environ.get('SLEEP_THRESHOLD', '60')) # 1 minte
WORKERS = int(environ.get('WORKERS', '')) # let pyrogram handle it
BIN_CHANNEL = int(environ.get('BIN_CHANNEL', None))
PORT = int(environ.get('PORT', 8080))
BIND_ADDRESS = str(environ.get('WEB_SERVER_BIND_ADDRESS', '0.0.0.0'))
PING_INTERVAL = int(environ.get('PING_INTERVAL', '1200')) # 20 minutes
HAS_SSL = environ.get('HAS_SSL', False)
HAS_SSL = True if str(HAS_SSL).lower() == 'true' else False
NO_PORT = getenv('NO_PORT', False)
NO_PORT = environ.get('NO_PORT', False)
NO_PORT = True if str(NO_PORT).lower() == 'true' else False
if 'DYNO' in environ:
ON_HEROKU = True
APP_NAME = str(getenv('APP_NAME'))
APP_NAME = str(environ.get('APP_NAME'))
else:
ON_HEROKU = False
FQDN = str(getenv('FQDN', BIND_ADDRESS)) if not ON_HEROKU or getenv('FQDN') else APP_NAME + '.herokuapp.com'
FQDN = str(environ.get('FQDN', BIND_ADDRESS)) if not ON_HEROKU or environ.get('FQDN') else APP_NAME + '.herokuapp.com'
if ON_HEROKU:
URL = f"https://{FQDN}/"
else:
URL = "http{}://{}{}/".format('s' if HAS_SSL else '', FQDN, '' if NO_PORT else ':'+ str(PORT))
URL = "http{}://{}{}/".format('s' if HAS_SSL else '', FQDN, '' if NO_PORT else ':' + str(PORT))

0 comments on commit 3571cf9

Please sign in to comment.