Skip to content

Commit b5fdac8

Browse files
push
1 parent bf196f6 commit b5fdac8

12 files changed

Lines changed: 468 additions & 661 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,6 @@ dmypy.json
112112

113113
# Pyre type checker
114114
.pyre/
115+
116+
#session files
117+
*.session

LICENSE

Lines changed: 0 additions & 661 deletions
This file was deleted.

WebStreamer/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This file is a part of TG-FileStreamBot
2+
# Coding : Jyothis Jayanth [@EverythingSuckz]

WebStreamer/__main__.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# This file is a part of TG-FileStreamBot
2+
# Coding : Jyothis Jayanth [@EverythingSuckz]
3+
4+
import os
5+
import sys
6+
import glob
7+
import asyncio
8+
import logging
9+
import importlib
10+
from pathlib import Path
11+
from pyrogram import idle
12+
from .bot import StreamBot
13+
from .vars import Var
14+
from aiohttp import web
15+
from .server import web_server
16+
17+
ppath = f"WebStreamer/bot/plugins/*.py"
18+
files = glob.glob(ppath)
19+
20+
loop = asyncio.get_event_loop()
21+
22+
23+
async def start_services():
24+
print('\n')
25+
print('------------------- Initalizing Telegram Bot -------------------')
26+
await StreamBot.start()
27+
print('\n')
28+
print('---------------------- DONE ----------------------')
29+
print('\n')
30+
print('------------------- Importing -------------------')
31+
for name in files:
32+
with open(name) as a:
33+
patt = Path(a.name)
34+
plugin_name = patt.stem.replace(".py", "")
35+
plugins_dir = Path(f"WebStreamer/bot/plugins/{plugin_name}.py")
36+
import_path = ".plugins.{}".format(plugin_name)
37+
spec = importlib.util.spec_from_file_location(import_path, plugins_dir)
38+
load = importlib.util.module_from_spec(spec)
39+
spec.loader.exec_module(load)
40+
sys.modules["WebStreamer.bot.plugins." + plugin_name] = load
41+
print("Imported => " + plugin_name)
42+
print('\n')
43+
print('------------------- Initalizing Web Server -------------------')
44+
app = web.AppRunner(await web_server())
45+
await app.setup()
46+
bind_address = "0.0.0.0" if Var.ENV else Var.FQDN
47+
await web.TCPSite(app, bind_address, Var.PORT).start()
48+
print('\n')
49+
print('----------------------- Service Started -----------------------')
50+
print(' bot =>> {}'.format((await StreamBot.get_me()).first_name))
51+
print(' server ip =>> {}:{}'.format(bind_address, Var.PORT))
52+
print('---------------------------------------------------------------')
53+
await idle()
54+
55+
if __name__ == '__main__':
56+
try:
57+
loop.run_until_complete(start_services())
58+
except KeyboardInterrupt:
59+
print('----------------------- Service Stopped -----------------------')

WebStreamer/bot/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This file is a part of TG-FileStreamBot
2+
# Coding : Jyothis Jayanth [@EverythingSuckz]
3+
4+
from pyrogram import Client
5+
from ..vars import Var
6+
7+
StreamBot = Client(
8+
session_name= 'Web Streamer',
9+
api_id=Var.API_ID,
10+
api_hash=Var.API_HASH,
11+
bot_token=Var.BOT_TOKEN,
12+
sleep_threshold=Var.SLEEP_THRESHOLD,
13+
workers=Var.WORKERS
14+
)

WebStreamer/bot/plugins/start.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This file is a part of TG-FileStreamBot
2+
# Coding : Jyothis Jayanth [@EverythingSuckz]
3+
4+
from WebStreamer.bot import StreamBot
5+
from WebStreamer.vars import Var
6+
from pyrogram import filters, emoji
7+
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
8+
9+
@StreamBot.on_message(filters.command(['start', 'help']))
10+
async def start(b, m):
11+
await m.reply('Hi, Send me a file to get an instant stream link.',
12+
reply_markup=InlineKeyboardMarkup(
13+
[
14+
[
15+
InlineKeyboardButton(
16+
f'{emoji.STAR} Source {emoji.STAR}',
17+
url='https://github.com/EverythingSuckz/TG-FileStreamBot'
18+
)
19+
]
20+
]
21+
))

WebStreamer/bot/plugins/stream.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This file is a part of TG-FileStreamBot
2+
# Coding : Jyothis Jayanth [@EverythingSuckz]
3+
4+
from WebStreamer.bot import StreamBot
5+
from WebStreamer.vars import Var
6+
from pyrogram import filters, Client, emoji
7+
from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
8+
9+
10+
@StreamBot.on_message(filters.private & (filters.document | filters.video | filters.audio), group=4)
11+
async def media_receive_handler(c: Client, m: Message):
12+
log_msg = await m.copy(chat_id=Var.BIN_CHANNEL)
13+
stream_link = "https://{}/{}".format(Var.FQDN, log_msg.message_id) if Var.ON_HEROKU else \
14+
"http://{}:{}/{}".format(Var.FQDN,
15+
Var.PORT,
16+
log_msg.message_id)
17+
await m.reply_text(
18+
text="`{}`".format(stream_link)
19+
)

WebStreamer/server/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Taken from megadlbot_oss <https://github.com/eyaadh/megadlbot_oss/blob/master/mega/webserver/__init__.py>
2+
# Thanks to Eyaadh <https://github.com/eyaadh>
3+
# This file is a part of TG-FileStreamBot
4+
# Coding : Jyothis Jayanth [@EverythingSuckz]
5+
6+
from aiohttp import web
7+
from .stream_routes import routes
8+
9+
10+
async def web_server():
11+
web_app = web.Application(client_max_size=30000000)
12+
web_app.add_routes(routes)
13+
return web_app
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Taken from megadlbot_oss <https://github.com/eyaadh/megadlbot_oss/blob/master/mega/webserver/routes.py>
2+
# Thanks to Eyaadh <https://github.com/eyaadh>
3+
4+
import math
5+
import logging
6+
import secrets
7+
import mimetypes
8+
from ..vars import Var
9+
from aiohttp import web
10+
from ..bot import StreamBot
11+
from ..utils.custom_dl import TGCustomYield, chunk_size, offset_fix
12+
13+
routes = web.RouteTableDef()
14+
15+
16+
@routes.get("/")
17+
async def root_route_handler(request):
18+
bot_details = await StreamBot.get_me()
19+
return web.json_response({"status": "running",
20+
"server_permission": "Open",
21+
"bot_associated_w": bot_details.username})
22+
23+
24+
@routes.get("/{message_id}")
25+
async def stream_handler(request):
26+
try:
27+
message_id = int(request.match_info['message_id'])
28+
return await media_streamer(request, message_id)
29+
except ValueError as e:
30+
logging.error(e)
31+
raise web.HTTPNotFound
32+
33+
34+
async def media_streamer(request, message_id: int):
35+
range_header = request.headers.get('Range', 0)
36+
media_msg = await StreamBot.get_messages(Var.BIN_CHANNEL, message_id)
37+
file_properties = await TGCustomYield().generate_file_properties(media_msg)
38+
file_size = file_properties.file_size
39+
40+
if range_header:
41+
from_bytes, until_bytes = range_header.replace('bytes=', '').split('-')
42+
from_bytes = int(from_bytes)
43+
until_bytes = int(until_bytes) if until_bytes else file_size - 1
44+
else:
45+
from_bytes = request.http_range.start or 0
46+
until_bytes = request.http_range.stop or file_size - 1
47+
48+
req_length = until_bytes - from_bytes
49+
50+
new_chunk_size = await chunk_size(req_length)
51+
offset = await offset_fix(from_bytes, new_chunk_size)
52+
first_part_cut = from_bytes - offset
53+
last_part_cut = (until_bytes % new_chunk_size) + 1
54+
part_count = math.ceil(req_length / new_chunk_size)
55+
body = TGCustomYield().yield_file(media_msg, offset, first_part_cut, last_part_cut, part_count,
56+
new_chunk_size)
57+
58+
file_name = file_properties.file_name if file_properties.file_name \
59+
else f"{secrets.token_hex(2)}.jpeg"
60+
mime_type = file_properties.mime_type if file_properties.mime_type \
61+
else f"{mimetypes.guess_type(file_name)}"
62+
63+
return_resp = web.Response(
64+
status=206 if range_header else 200,
65+
body=body,
66+
headers={
67+
"Content-Type": mime_type,
68+
"Content-Range": f"bytes {from_bytes}-{until_bytes}/{file_size}",
69+
"Content-Disposition": f'attachment; filename="{file_name}"',
70+
"Accept-Ranges": "bytes",
71+
}
72+
)
73+
74+
if return_resp.status == 200:
75+
return_resp.headers.add("Content-Length", str(file_size))
76+
77+
return return_resp

WebStreamer/utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This file is a part of TG-FileStreamBot
2+
# Coding : Jyothis Jayanth [@EverythingSuckz]

0 commit comments

Comments
 (0)