privateone commited on
Commit
0e49d61
·
1 Parent(s): d523f3f

Code Updates

Browse files
FileStream/__main__.py CHANGED
@@ -12,18 +12,14 @@ from FileStream.Tools import Time_ISTKolNow
12
  from FileStream.server import web_server
13
  from FileStream.bot.clients import initialize_clients
14
 
 
15
  logging.basicConfig(
16
  level=logging.INFO,
17
  datefmt="%d/%m/%Y %H:%M:%S",
18
- format=
19
- '[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
20
  handlers=[
21
  logging.StreamHandler(stream=sys.stdout),
22
- handlers.RotatingFileHandler("streambot.log",
23
- mode="a",
24
- maxBytes=104857600,
25
- backupCount=2,
26
- encoding="utf-8")
27
  ],
28
  )
29
 
@@ -33,62 +29,60 @@ logging.getLogger("aiohttp.web").setLevel(logging.ERROR)
33
 
34
  server = web.AppRunner(web_server())
35
 
36
- loop = asyncio.get_event_loop()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
 
 
 
38
 
39
- async def start_services():
40
- print()
41
- if Telegram.SECONDARY:
42
- print("------------------ Starting as Secondary Server ------------------")
43
- else:
44
- print("------------------- Starting as Primary Server -------------------")
45
- print()
46
- print("-------------------- Initializing Telegram Bot --------------------")
47
 
48
- await FileStream.start()
49
- bot_info = await FileStream.get_me()
50
- FileStream.id = bot_info.id
51
- FileStream.username = bot_info.username
52
- FileStream.fname = bot_info.first_name
53
- print("------------------------------ DONE ------------------------------")
54
- print()
55
- print("---------------------- Initializing Clients ----------------------")
56
- await initialize_clients()
57
- print("------------------------------ DONE ------------------------------")
58
- print()
59
- print("--------------------- Initializing Web Server ---------------------")
60
- await server.setup()
61
- await web.TCPSite(server, Server.BIND_ADDRESS, Server.PORT).start()
62
- print("------------------------------ DONE ------------------------------")
63
- print()
64
- print("------------------------- Service Started -------------------------")
65
- print("Bot =>> {}".format(bot_info.first_name))
66
- if bot_info.dc_id:
67
- print("DC ID =>> {}".format(str(bot_info.dc_id)))
68
- print(" URL =>> {}".format(Server.URL))
69
- print("------------------------------------------------------------------")
70
- """
71
- all_sources = [
72
- Telegram.ULOG_GROUP, Telegram.FLOG_CHANNEL, Telegram.PFLOG_CHANNEL
73
- ]
74
- for source in all_sources:
75
- await FileStream.send_message(chat_id=source,
76
- text=f"Hi, I am Online @{ISTKolNow()}",
77
- disable_web_page_preview=True)
78
- """
79
- await idle()
80
 
 
 
 
 
 
 
 
81
 
82
  async def cleanup():
83
- await server.cleanup()
84
- await FileStream.stop()
85
 
 
 
 
 
 
 
 
 
 
86
 
87
  if __name__ == "__main__":
88
- try:
89
- loop.run_until_complete(start_services())
90
- except KeyboardInterrupt:
91
- loop.stop()
92
- print("------------------------ Stopped Services ------------------------")
93
- except Exception as err:
94
- logging.error(traceback.format_exc())
 
12
  from FileStream.server import web_server
13
  from FileStream.bot.clients import initialize_clients
14
 
15
+ # Configure logging
16
  logging.basicConfig(
17
  level=logging.INFO,
18
  datefmt="%d/%m/%Y %H:%M:%S",
19
+ format='[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
 
20
  handlers=[
21
  logging.StreamHandler(stream=sys.stdout),
22
+ handlers.RotatingFileHandler("streambot.log", mode="a", maxBytes=104857600, backupCount=2, encoding="utf-8")
 
 
 
 
23
  ],
24
  )
25
 
 
29
 
30
  server = web.AppRunner(web_server())
31
 
32
+ async def start_services():
33
+ print(
34
+ "****\n"
35
+ + (
36
+ "------------------ Starting as Secondary Server ------------------"
37
+ if Telegram.SECONDARY
38
+ else "------------------- Starting as Primary Server -------------------"
39
+ )
40
+ + "\n****\n-------------------- Initializing Telegram Bot --------------------"
41
+ )
42
+ await FileStream.start()
43
+ bot_info = await FileStream.get_me()
44
+ FileStream.id = bot_info.id
45
+ FileStream.username = bot_info.username
46
+ FileStream.fname = bot_info.first_name
47
+ print("------------------------------ DONE ------------------------------\n\n")
48
 
49
+ print("---------------------- Initializing Clients ----------------------")
50
+ await initialize_clients()
51
+ print("------------------------------ DONE ------------------------------\n\n")
52
 
53
+ print("--------------------- Initializing Web Server ---------------------")
54
+ await server.setup()
55
+ await web.TCPSite(server, Server.BIND_ADDRESS, Server.PORT).start()
56
+ print("------------------------------ DONE ------------------------------\n\n")
 
 
 
 
57
 
58
+ print(
59
+ f"------------------------- Service Started -------------------------\n"
60
+ f"Bot =>> {bot_info.first_name}\n"
61
+ + (f"DC ID =>> {bot_info.dc_id}\n" if bot_info.dc_id else "")
62
+ + f"URL =>> {Server.URL}\n"
63
+ "------------------------------------------------------------------"
64
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
+ # Uncomment the following section to send messages to specific sources
67
+ """
68
+ all_sources = [Telegram.ULOG_GROUP, Telegram.FLOG_CHANNEL, Telegram.PFLOG_CHANNEL]
69
+ for source in all_sources:
70
+ await FileStream.send_message(chat_id=source, text=f"Hi, I am Online @{Time_ISTKolNow()}", disable_web_page_preview=True)
71
+ """
72
+ await idle()
73
 
74
  async def cleanup():
75
+ await server.cleanup()
 
76
 
77
+ async def main():
78
+ try:
79
+ await start_services()
80
+ except KeyboardInterrupt:
81
+ print("\n------------------------ Stopped Services ------------------------")
82
+ except Exception as err:
83
+ logging.error("An error occurred:\n%s", traceback.format_exc())
84
+ finally:
85
+ await cleanup()
86
 
87
  if __name__ == "__main__":
88
+ asyncio.run(main())
 
 
 
 
 
 
FileStream/bot/__init__.py CHANGED
@@ -18,11 +18,13 @@ FileStream = Client(name="FileStream",
18
  workers=Telegram.WORKERS,
19
  no_updates=no_updates)
20
 
21
- multi_clients = {}
22
- work_loads = {}
 
 
23
 
24
  async def req_client():
25
  index = min(work_loads, key=work_loads.get)
26
- faster_client = multi_clients[index]
27
  response = dict(index=index, client=faster_client)
28
  return response
 
18
  workers=Telegram.WORKERS,
19
  no_updates=no_updates)
20
 
21
+
22
+ WORK_LOADS = {}
23
+ MULTI_CLIENTS = {}
24
+ ACTIVE_CLIENTS = {}
25
 
26
  async def req_client():
27
  index = min(work_loads, key=work_loads.get)
28
+ faster_client = MULTI_CLIENTS[index]
29
  response = dict(index=index, client=faster_client)
30
  return response
FileStream/bot/clients.py CHANGED
@@ -1,9 +1,11 @@
1
  import asyncio
2
  import logging
3
  from os import environ
4
- from ..config import Telegram
5
  from pyrogram import Client
6
- from . import multi_clients, work_loads, FileStream
 
 
 
7
 
8
 
9
  async def initialize_clients():
@@ -16,8 +18,8 @@ async def initialize_clients():
16
  )
17
  )
18
  if not all_tokens:
19
- multi_clients[0] = FileStream
20
- work_loads[0] = 0
21
  print("No additional clients found, using default client")
22
  return
23
 
@@ -45,14 +47,14 @@ async def initialize_clients():
45
  in_memory=True,
46
  ).start()
47
  client.id = (await client.get_me()).id
48
- work_loads[client_id] = 0
49
  return client_id, client
50
  except Exception:
51
  logging.error(f"Failed starting Client - {client_id} Error:", exc_info=True)
52
 
53
  clients = await asyncio.gather(*[start_client(i, token) for i, token in all_tokens.items()])
54
- multi_clients.update(dict(clients))
55
- if len(multi_clients) != 1:
56
  Telegram.MULTI_CLIENT = True
57
  print("Multi-Client Mode Enabled")
58
  else:
 
1
  import asyncio
2
  import logging
3
  from os import environ
 
4
  from pyrogram import Client
5
+
6
+ from . import FileStream
7
+ from ..config import Telegram
8
+ from . import MULTI_CLIENTS, WORK_LOADS
9
 
10
 
11
  async def initialize_clients():
 
18
  )
19
  )
20
  if not all_tokens:
21
+ MULTI_CLIENTS[0] = FileStream
22
+ WORK_LOADS[0] = 0
23
  print("No additional clients found, using default client")
24
  return
25
 
 
47
  in_memory=True,
48
  ).start()
49
  client.id = (await client.get_me()).id
50
+ WORK_LOADS[client_id] = 0
51
  return client_id, client
52
  except Exception:
53
  logging.error(f"Failed starting Client - {client_id} Error:", exc_info=True)
54
 
55
  clients = await asyncio.gather(*[start_client(i, token) for i, token in all_tokens.items()])
56
+ MULTI_CLIENTS.update(dict(clients))
57
+ if len(MULTI_CLIENTS) != 1:
58
  Telegram.MULTI_CLIENT = True
59
  print("Multi-Client Mode Enabled")
60
  else:
FileStream/bot/plugins/FileHandlers/callback.py CHANGED
@@ -12,7 +12,8 @@ from pyrogram.raw.types import KeyboardButtonSimpleWebView
12
  from FileStream import __version__
13
  from FileStream.Database import Database
14
  from FileStream.config import Telegram, Server
15
- from FileStream.bot import FileStream, multi_clients
 
16
  from FileStream.server.exceptions import FIleNotFound
17
  from FileStream.utils.FileProcessors.translation import LANG, BUTTON
18
  from FileStream.utils.FileProcessors.human_readable import humanbytes
 
12
  from FileStream import __version__
13
  from FileStream.Database import Database
14
  from FileStream.config import Telegram, Server
15
+ from FileStream.bot import FileStream
16
+ from FileStream.bot import MULTI_CLIENTS
17
  from FileStream.server.exceptions import FIleNotFound
18
  from FileStream.utils.FileProcessors.translation import LANG, BUTTON
19
  from FileStream.utils.FileProcessors.human_readable import humanbytes
FileStream/bot/plugins/FileHandlers/stream.py CHANGED
@@ -32,7 +32,8 @@ from FileStream.Database import Database
32
  from FileStream.config import Telegram, Server
33
  from FileStream.Tools.tool import TimeFormatter
34
  from FileStream.Tools.progress import progress
35
- from FileStream.bot import FileStream, multi_clients, work_loads, req_client
 
36
  from FileStream.utils.FileProcessors.bot_utils import is_user_banned, is_user_exist, is_user_joined, gen_link, is_channel_banned, is_channel_exist, is_user_authorized, upload_type_func
37
  from FileStream.utils.FileProcessors.custom_dl import ByteStreamer
38
  from FileStream.utils.FileProcessors.custom_ul import TeleUploader
@@ -87,7 +88,7 @@ async def channel_receive_handler(bot: Client, message: Message):
87
 
88
  try:
89
  inserted_id = await db.add_file(get_file_info(message))
90
- await get_file_ids(False, inserted_id, multi_clients, message)
91
  reply_markup, stream_link = await gen_link(_id=inserted_id)
92
  await bot.edit_message_reply_markup(
93
  chat_id=message.chat.id,
@@ -155,15 +156,15 @@ async def reply_handler(
155
  ext = mimetypes.guess_extension(mime_type)
156
  file_info['file']['file_name'] = f"{message.text}{ext}"
157
 
158
- #index = min(work_loads, key=work_loads.get)
159
- #faster_client = multi_clients[index]
160
  client=await req_client()
161
  print(f"Using {client['index']} for {file_info['file']['file_name']}")
162
  logging.info(f"Client {client['index']} is now serving {request.headers.get('X-FORWARDED-FOR',request.remote)}")
163
 
164
 
165
  tg_connect = TGFileController(client['client'])
166
- req_file_id = await tg_connect.get_file_properties(file_info['_id'], multi_clients)
167
 
168
  start_time = time.time()
169
  file = await tg_connect.upload_file(
 
32
  from FileStream.config import Telegram, Server
33
  from FileStream.Tools.tool import TimeFormatter
34
  from FileStream.Tools.progress import progress
35
+ from FileStream.bot import FileStream, req_client
36
+ from FileStream.bot import MULTI_CLIENTS, WORK_LOADS
37
  from FileStream.utils.FileProcessors.bot_utils import is_user_banned, is_user_exist, is_user_joined, gen_link, is_channel_banned, is_channel_exist, is_user_authorized, upload_type_func
38
  from FileStream.utils.FileProcessors.custom_dl import ByteStreamer
39
  from FileStream.utils.FileProcessors.custom_ul import TeleUploader
 
88
 
89
  try:
90
  inserted_id = await db.add_file(get_file_info(message))
91
+ await get_file_ids(False, inserted_id, MULTI_CLIENTS, message)
92
  reply_markup, stream_link = await gen_link(_id=inserted_id)
93
  await bot.edit_message_reply_markup(
94
  chat_id=message.chat.id,
 
156
  ext = mimetypes.guess_extension(mime_type)
157
  file_info['file']['file_name'] = f"{message.text}{ext}"
158
 
159
+ #index = min(WORK_LWORK_LOADS, key=WORK_LWORK_LOADS.get)
160
+ #faster_client = MULTI_CLIENTS[index]
161
  client=await req_client()
162
  print(f"Using {client['index']} for {file_info['file']['file_name']}")
163
  logging.info(f"Client {client['index']} is now serving {request.headers.get('X-FORWARDED-FOR',request.remote)}")
164
 
165
 
166
  tg_connect = TGFileController(client['client'])
167
+ req_file_id = await tg_connect.get_file_properties(file_info['_id'], MULTI_CLIENTS)
168
 
169
  start_time = time.time()
170
  file = await tg_connect.upload_file(
FileStream/server/Functions/downloader.py CHANGED
@@ -14,33 +14,35 @@ from aiohttp.http_exceptions import BadStatusLine
14
  from FileStream.config import Telegram
15
  from FileStream import utils, StartTime, __version__
16
  from FileStream.Tools import mime_identifier, Time_ISTKolNow
17
- from FileStream.bot import multi_clients, work_loads, req_client, FileStream
 
18
  from FileStream.server.exceptions import FIleNotFound, InvalidHash
19
  from FileStream.server.render_template import render_page, render_upload
20
  from FileStream.utils.FileProcessors.custom_ul import TeleUploader
21
 
22
- class_cache={}
23
-
24
 
25
  async def media_streamer(request: web.Request, db_id: str):
 
26
  range_header = request.headers.get("Range", 0)
 
 
27
 
28
- #index = min(work_loads, key=work_loads.get)
29
- #faster_client = multi_clients[index]
30
  client = await req_client()
31
 
32
  if Telegram.MULTI_CLIENT:
33
  logging.info(f"Client {client['index']} is now serving {request.headers.get('X-FORWARDED-FOR',request.remote)}")
34
 
35
- if client['client'] in class_cache:
36
- tg_connect = class_cache[client['client']]
37
  logging.debug(f"Using cached ByteStreamer object for client {client['index']}")
 
38
  else:
39
  logging.debug(f"Creating new ByteStreamer object for client {client['index']}")
40
  tg_connect = utils.ByteStreamer(client['client'])
41
- class_cache[client['client']] = tg_connect
 
42
  logging.debug("before calling get_file_properties")
43
- file_id = await tg_connect.get_file_properties(db_id, multi_clients)
44
  logging.debug("after calling get_file_properties")
45
 
46
  file_size = file_id.file_size
@@ -53,12 +55,12 @@ async def media_streamer(request: web.Request, db_id: str):
53
  from_bytes = request.http_range.start or 0
54
  until_bytes = (request.http_range.stop or file_size) - 1
55
 
56
- if (until_bytes > file_size) or (from_bytes < 0) or (until_bytes
57
- < from_bytes):
58
  return web.Response(
59
- status=416,
60
- body="416: Range not satisfiable",
61
- headers={"Content-Range": f"bytes */{file_size}"},
62
  )
63
 
64
  chunk_size = 512 * 1024
@@ -69,30 +71,28 @@ async def media_streamer(request: web.Request, db_id: str):
69
  last_part_cut = until_bytes % chunk_size + 1
70
 
71
  req_length = until_bytes - from_bytes + 1
72
- part_count = math.ceil(until_bytes / chunk_size) - math.floor(
73
- offset / chunk_size)
74
- body = tg_connect.yield_file(file_id, client['index'], offset, first_part_cut,
75
- last_part_cut, part_count, chunk_size)
76
 
77
  mime_type = file_id.mime_type
78
  file_name = utils.get_name(file_id)
79
  disposition = "attachment"
80
 
81
  if not mime_type:
82
- mime_type = mimetypes.guess_type(
83
- file_name)[0] or "application/octet-stream"
84
 
85
  # if "video/" in mime_type or "audio/" in mime_type:
86
  # disposition = "inline"
87
 
88
  return web.Response(
89
- status=206 if range_header else 200,
90
- body=body,
91
- headers={
92
- "Content-Type": f"{mime_type}",
93
- "Content-Range": f"bytes {from_bytes}-{until_bytes}/{file_size}",
94
- "Content-Length": str(req_length),
95
- "Content-Disposition": f'{disposition}; filename="{file_name}"',
96
- "Accept-Ranges": "bytes",
97
- },
98
  )
 
14
  from FileStream.config import Telegram
15
  from FileStream import utils, StartTime, __version__
16
  from FileStream.Tools import mime_identifier, Time_ISTKolNow
17
+ from FileStream.bot import req_client, FileStream
18
+ from FileStream.bot import MULTI_CLIENTS, WORK_LOADS, ACTIVE_CLIENTS
19
  from FileStream.server.exceptions import FIleNotFound, InvalidHash
20
  from FileStream.server.render_template import render_page, render_upload
21
  from FileStream.utils.FileProcessors.custom_ul import TeleUploader
22
 
 
 
23
 
24
  async def media_streamer(request: web.Request, db_id: str):
25
+
26
  range_header = request.headers.get("Range", 0)
27
+ #index = minWORK_LOADS, keyWORK_LOADS.get)
28
+ #faster_client = MULTI_CLIENTS[index]
29
 
 
 
30
  client = await req_client()
31
 
32
  if Telegram.MULTI_CLIENT:
33
  logging.info(f"Client {client['index']} is now serving {request.headers.get('X-FORWARDED-FOR',request.remote)}")
34
 
35
+ if client['client'] in ACTIVE_CLIENTS:
36
+ tg_connect = ACTIVE_CLIENTS[client['client']]
37
  logging.debug(f"Using cached ByteStreamer object for client {client['index']}")
38
+
39
  else:
40
  logging.debug(f"Creating new ByteStreamer object for client {client['index']}")
41
  tg_connect = utils.ByteStreamer(client['client'])
42
+ ACTIVE_CLIENTS[client['client']] = tg_connect
43
+
44
  logging.debug("before calling get_file_properties")
45
+ file_id = await tg_connect.get_file_properties(db_id, MULTI_CLIENTS)
46
  logging.debug("after calling get_file_properties")
47
 
48
  file_size = file_id.file_size
 
55
  from_bytes = request.http_range.start or 0
56
  until_bytes = (request.http_range.stop or file_size) - 1
57
 
58
+ if (until_bytes > file_size) or (from_bytes < 0) or (until_bytes < from_bytes):
59
+
60
  return web.Response(
61
+ status=416,
62
+ body="416: Range not satisfiable",
63
+ headers={"Content-Range": f"bytes */{file_size}"},
64
  )
65
 
66
  chunk_size = 512 * 1024
 
71
  last_part_cut = until_bytes % chunk_size + 1
72
 
73
  req_length = until_bytes - from_bytes + 1
74
+ part_count = math.ceil(until_bytes / chunk_size) - math.floor(offset / chunk_size)
75
+
76
+ body = tg_connect.yield_file(file_id, client['index'], offset, first_part_cut,last_part_cut, part_count, chunk_size)
 
77
 
78
  mime_type = file_id.mime_type
79
  file_name = utils.get_name(file_id)
80
  disposition = "attachment"
81
 
82
  if not mime_type:
83
+ mime_type = mimetypes.guess_type(file_name)[0] or "application/octet-stream"
 
84
 
85
  # if "video/" in mime_type or "audio/" in mime_type:
86
  # disposition = "inline"
87
 
88
  return web.Response(
89
+ status=206 if range_header else 200,
90
+ body=body,
91
+ headers={
92
+ "Content-Type": f"{mime_type}",
93
+ "Content-Range": f"bytes {from_bytes}-{until_bytes}/{file_size}",
94
+ "Content-Length": str(req_length),
95
+ "Content-Disposition": f'{disposition}; filename="{file_name}"',
96
+ "Accept-Ranges": "bytes",
97
+ },
98
  )
FileStream/server/render_template.py CHANGED
@@ -21,20 +21,15 @@ async def render_page(db_id):
21
  async with aiohttp.ClientSession() as s:
22
  async with s.get(src) as u:
23
  file_size = humanbytes(int(u.headers.get('Content-Length')))
24
-
25
  with open(template_file) as f:
26
  template = jinja2.Template(f.read())
27
-
28
  return template.render(file_name=file_name,
29
  file_url=src,
30
  file_size=file_size)
31
 
32
 
33
  async def render_upload():
34
-
35
  template_file = "FileStream/server/template/upload.html"
36
-
37
  with open(template_file) as f:
38
  template = jinja2.Template(f.read())
39
-
40
  return template.render()
 
21
  async with aiohttp.ClientSession() as s:
22
  async with s.get(src) as u:
23
  file_size = humanbytes(int(u.headers.get('Content-Length')))
 
24
  with open(template_file) as f:
25
  template = jinja2.Template(f.read())
 
26
  return template.render(file_name=file_name,
27
  file_url=src,
28
  file_size=file_size)
29
 
30
 
31
  async def render_upload():
 
32
  template_file = "FileStream/server/template/upload.html"
 
33
  with open(template_file) as f:
34
  template = jinja2.Template(f.read())
 
35
  return template.render()
FileStream/server/routes_main.py CHANGED
@@ -10,7 +10,8 @@ from aiohttp.http_exceptions import BadStatusLine
10
 
11
  #---------------------Local Upload---------------------#
12
  from FileStream.config import Telegram
13
- from FileStream.bot import multi_clients, work_loads, FileStream
 
14
  from FileStream.server.exceptions import FIleNotFound, InvalidHash
15
  from FileStream import utils, StartTime, __version__
16
  from FileStream.utils.FileProcessors.custom_ul import TeleUploader
@@ -26,8 +27,8 @@ async def root_status_handler(_):
26
  "server_status":"running",
27
  "uptime": utils.get_readable_time(time.time() - StartTime),
28
  "telegram_bot": "@" + FileStream.username,
29
- "connected_bots": len(multi_clients),
30
- "loads":dict(("bot" + str(c + 1), l) for c, (_, l) in enumerate(sorted(work_loads.items(), key=lambda x: x[1], reverse=True))),
31
  "version": __version__,
32
  })
33
 
 
10
 
11
  #---------------------Local Upload---------------------#
12
  from FileStream.config import Telegram
13
+ from FileStream.bot import FileStream
14
+ from FileStream.bot import MULTI_CLIENTS, WORK_LOADS
15
  from FileStream.server.exceptions import FIleNotFound, InvalidHash
16
  from FileStream import utils, StartTime, __version__
17
  from FileStream.utils.FileProcessors.custom_ul import TeleUploader
 
27
  "server_status":"running",
28
  "uptime": utils.get_readable_time(time.time() - StartTime),
29
  "telegram_bot": "@" + FileStream.username,
30
+ "connected_bots": len(MULTI_CLIENTS),
31
+ "loads":dict(("bot" + str(c + 1), l) for c, (_, l) in enumerate(sorted(WORK_LOADS.items(), key=lambda x: x[1], reverse=True))),
32
  "version": __version__,
33
  })
34
 
FileStream/utils/FileProcessors/custom_dl.py CHANGED
@@ -1,7 +1,7 @@
1
  import asyncio
2
  import logging
3
  from typing import Dict, Union
4
- from FileStream.bot import work_loads
5
  from pyrogram import Client, utils, raw
6
  from .file_properties import get_file_ids
7
  from pyrogram.session import Session, Auth
@@ -155,7 +155,7 @@ class ByteStreamer:
155
  ) -> Union[str, None]:
156
 
157
  client = self.client
158
- work_loads[index] += 1
159
  logging.debug(f"Starting to yielding file with client {index}.")
160
  media_session = await self.generate_media_session(client, file_id)
161
 
@@ -196,7 +196,7 @@ class ByteStreamer:
196
  pass
197
  finally:
198
  logging.debug(f"Finished yielding file with {current_part} parts.")
199
- work_loads[index] -= 1
200
 
201
  async def clean_cache(self) -> None:
202
  """
 
1
  import asyncio
2
  import logging
3
  from typing import Dict, Union
4
+ from FileStream.bot import WORK_LOADS
5
  from pyrogram import Client, utils, raw
6
  from .file_properties import get_file_ids
7
  from pyrogram.session import Session, Auth
 
155
  ) -> Union[str, None]:
156
 
157
  client = self.client
158
+ WORK_LOADS[index] += 1
159
  logging.debug(f"Starting to yielding file with client {index}.")
160
  media_session = await self.generate_media_session(client, file_id)
161
 
 
196
  pass
197
  finally:
198
  logging.debug(f"Finished yielding file with {current_part} parts.")
199
+ WORK_LOADS[index] -= 1
200
 
201
  async def clean_cache(self) -> None:
202
  """
FileStream/utils/FileProcessors/custom_mix.py CHANGED
@@ -14,9 +14,9 @@ from pyrogram.file_id import FileId, FileType, ThumbnailSource
14
  from pyrogram.types import Message
15
  from pyrogram import StopTransmission
16
 
17
- from FileStream.bot import work_loads
18
  from typing import Dict, Union
19
- from FileStream.bot import work_loads
20
  from pyrogram import Client, utils, raw
21
  from .file_properties import get_file_ids
22
  from pyrogram.session import Session, Auth
@@ -49,7 +49,7 @@ from pyrogram.enums.parse_mode import ParseMode
49
 
50
  #-----------------------Local Imports----------------------------------#
51
  from FileStream import utils, StartTime, __version__
52
- from FileStream.bot import FileStream, multi_clients, work_loads
53
  from FileStream.utils.FileProcessors.bot_utils import is_user_banned, is_user_exist, is_user_joined, gen_link, is_channel_banned, is_channel_exist, is_user_authorized
54
  from FileStream.Database import Database
55
  from FileStream.utils.FileProcessors.file_properties import get_file_ids, get_file_info
@@ -78,7 +78,7 @@ class TGFileController:
78
  async def get_me(self):
79
  return await self.client.get_me().username
80
 
81
- async def get_file_properties(self, db_id: str, multi_clients) -> FileId:
82
  """
83
  Returns the properties of a media of a specific message in a FIleId class.
84
  if the properties are cached, then it'll return the cached results.
@@ -86,12 +86,12 @@ class TGFileController:
86
  """
87
  if not db_id in self.cached_file_ids:
88
  logging.debug("Before Calling generate_file_properties")
89
- await self.generate_file_properties(db_id, multi_clients)
90
  logging.debug(f"Cached file properties for file with ID {db_id}")
91
  return self.cached_file_ids[db_id]
92
 
93
  async def generate_file_properties(self, db_id: str,
94
- multi_clients) -> FileId:
95
  """
96
  Generates the properties of a media file on a specific message.
97
  returns ths properties in a FIleId class.
@@ -306,7 +306,7 @@ class TGFileController:
306
  client.loop.create_task(worker(session)) for _ in range(workers_count)
307
  ]
308
 
309
- work_loads[index] += 1
310
  logging.debug(f"Starting to yielding file with client {index}.")
311
  media_session = await self.generate_media_session(client, file_id)
312
 
@@ -370,7 +370,7 @@ class TGFileController:
370
 
371
  await session.stop()
372
  logging.debug(f"Finished yielding file with {file_part} parts.")
373
- work_loads[index] -= 1
374
 
375
  if is_big:
376
  return raw.types.InputFileBig(
 
14
  from pyrogram.types import Message
15
  from pyrogram import StopTransmission
16
 
17
+
18
  from typing import Dict, Union
19
+ from FileStream.bot import WORK_LOADS
20
  from pyrogram import Client, utils, raw
21
  from .file_properties import get_file_ids
22
  from pyrogram.session import Session, Auth
 
49
 
50
  #-----------------------Local Imports----------------------------------#
51
  from FileStream import utils, StartTime, __version__
52
+ from FileStream.bot import FileStream, MULTI_CLIENTS, WORK_LOADS
53
  from FileStream.utils.FileProcessors.bot_utils import is_user_banned, is_user_exist, is_user_joined, gen_link, is_channel_banned, is_channel_exist, is_user_authorized
54
  from FileStream.Database import Database
55
  from FileStream.utils.FileProcessors.file_properties import get_file_ids, get_file_info
 
78
  async def get_me(self):
79
  return await self.client.get_me().username
80
 
81
+ async def get_file_properties(self, db_id: str, MULTI_CLIENTS) -> FileId:
82
  """
83
  Returns the properties of a media of a specific message in a FIleId class.
84
  if the properties are cached, then it'll return the cached results.
 
86
  """
87
  if not db_id in self.cached_file_ids:
88
  logging.debug("Before Calling generate_file_properties")
89
+ await self.generate_file_properties(db_id, MULTI_CLIENTS)
90
  logging.debug(f"Cached file properties for file with ID {db_id}")
91
  return self.cached_file_ids[db_id]
92
 
93
  async def generate_file_properties(self, db_id: str,
94
+ MULTI_CLIENTS) -> FileId:
95
  """
96
  Generates the properties of a media file on a specific message.
97
  returns ths properties in a FIleId class.
 
306
  client.loop.create_task(worker(session)) for _ in range(workers_count)
307
  ]
308
 
309
+ WORK_LOADS[index] += 1
310
  logging.debug(f"Starting to yielding file with client {index}.")
311
  media_session = await self.generate_media_session(client, file_id)
312
 
 
370
 
371
  await session.stop()
372
  logging.debug(f"Finished yielding file with {file_part} parts.")
373
+ WORK_LOADS[index] -= 1
374
 
375
  if is_big:
376
  return raw.types.InputFileBig(
FileStream/utils/FileProcessors/custom_ul.py CHANGED
@@ -11,7 +11,7 @@ from pyrogram import filters, types, Client, raw
11
  from pyrogram.file_id import FileId, FileType, PHOTO_TYPES, ThumbnailSource
12
  from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery
13
  #----------------------------------------------
14
- from FileStream.bot import FileStream, multi_clients, work_loads
15
  from FileStream.config import Telegram, Server
16
  from FileStream.Database import Database
17
  import asyncio
@@ -48,7 +48,7 @@ class TeleUploader:
48
  async def get_me(self):
49
  return await self.client.get_me().username
50
 
51
- async def get_file_properties(self, db_id: str, multi_clients) -> FileId:
52
  """
53
  Returns the properties of a media of a specific message in a FIleId class.
54
  if the properties are cached, then it'll return the cached results.
@@ -56,7 +56,7 @@ class TeleUploader:
56
  """
57
  if not db_id in self.cached_file_ids:
58
  logging.debug("Before Calling generate_file_properties")
59
- await self.generate_file_properties(db_id, multi_clients)
60
  logging.debug(f"Cached file properties for file with ID {db_id}")
61
  return self.cached_file_ids[db_id]
62
 
@@ -72,12 +72,11 @@ class TeleUploader:
72
  if media_session is None:
73
  if file_id.dc_id != await client.storage.dc_id():
74
  media_session = Session(
75
- client,
76
- file_id.dc_id,
77
- await Auth(client, file_id.dc_id, await
78
- client.storage.test_mode()).create(),
79
- await client.storage.test_mode(),
80
- is_media=True,
81
  )
82
  await media_session.start()
83
 
@@ -87,23 +86,21 @@ class TeleUploader:
87
 
88
  try:
89
  await media_session.invoke(
90
- raw.functions.auth.ImportAuthorization(
91
- id=exported_auth.id, bytes=exported_auth.bytes))
92
  break
93
  except AuthBytesInvalid:
94
- logging.debug(
95
- f"Invalid authorization bytes for DC {file_id.dc_id}")
96
  continue
97
  else:
98
  await media_session.stop()
99
  raise AuthBytesInvalid
100
  else:
101
  media_session = Session(
102
- client,
103
- file_id.dc_id,
104
- await client.storage.auth_key(),
105
- await client.storage.test_mode(),
106
- is_media=True,
107
  )
108
  await media_session.start()
109
  logging.debug(f"Created media session for DC {file_id.dc_id}")
@@ -149,8 +146,7 @@ class TeleUploader:
149
  file_size_limit_mib = 4000 if client.me.is_premium else 2000
150
  #file_size_limit_mib = 4000
151
  if file_details["file"]["file_size"] > file_size_limit_mib * 1024 * 1024:
152
- raise ValueError(
153
- f"Can't upload files bigger than {file_size_limit_mib} MiB")
154
 
155
  #file_total_parts = file_details["total_parts"]
156
  #is_big = file_size > 10 * 1024 * 1024
@@ -175,8 +171,7 @@ class TeleUploader:
175
  file_total_parts=file_details["file"]["total_parts"],
176
  bytes=chunk)
177
  response['status'] = "success"
178
- response[
179
- 'message'] = f"Uploading as Bigfile {file_details['file']['file_part']}/{file_details['file']['total_parts']}"
180
  print("Response", response)
181
 
182
  await session.invoke(rpc)
@@ -197,8 +192,7 @@ class TeleUploader:
197
  file=final,
198
  mime_type=file_details['file']["mime_type"],
199
  attributes=[
200
- raw.types.DocumentAttributeFilename(
201
- file_name=file_details['file']["file_name"])
202
  ])
203
 
204
  try:
@@ -243,7 +237,7 @@ class TeleUploader:
243
  )
244
 
245
  #log_msg = await send_file(FileStream, db_id, file_info['file_id'], message)
246
- #await update_file_id(log_msg.id, multi_clients))
247
  except Exception as e:
248
  await client.send_message(chat_id=Telegram.ULOG_GROUP,
249
  text=f"**#EʀʀᴏʀTʀᴀᴄᴋᴇʙᴀᴄᴋ:** `{e}`",
 
11
  from pyrogram.file_id import FileId, FileType, PHOTO_TYPES, ThumbnailSource
12
  from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery
13
  #----------------------------------------------
14
+ from FileStream.bot import FileStream, MULTI_CLIENTS, WORK_LOADS
15
  from FileStream.config import Telegram, Server
16
  from FileStream.Database import Database
17
  import asyncio
 
48
  async def get_me(self):
49
  return await self.client.get_me().username
50
 
51
+ async def get_file_properties(self, db_id: str, MULTI_CLIENTS) -> FileId:
52
  """
53
  Returns the properties of a media of a specific message in a FIleId class.
54
  if the properties are cached, then it'll return the cached results.
 
56
  """
57
  if not db_id in self.cached_file_ids:
58
  logging.debug("Before Calling generate_file_properties")
59
+ await self.generate_file_properties(db_id, MULTI_CLIENTS)
60
  logging.debug(f"Cached file properties for file with ID {db_id}")
61
  return self.cached_file_ids[db_id]
62
 
 
72
  if media_session is None:
73
  if file_id.dc_id != await client.storage.dc_id():
74
  media_session = Session(
75
+ client,
76
+ file_id.dc_id,
77
+ await Auth(client, file_id.dc_id, await client.storage.test_mode()).create(),
78
+ await client.storage.test_mode(),
79
+ is_media=True,
 
80
  )
81
  await media_session.start()
82
 
 
86
 
87
  try:
88
  await media_session.invoke(
89
+ raw.functions.auth.ImportAuthorization(id=exported_auth.id, bytes=exported_auth.bytes))
 
90
  break
91
  except AuthBytesInvalid:
92
+ logging.debug(f"Invalid authorization bytes for DC {file_id.dc_id}")
 
93
  continue
94
  else:
95
  await media_session.stop()
96
  raise AuthBytesInvalid
97
  else:
98
  media_session = Session(
99
+ client,
100
+ file_id.dc_id,
101
+ await client.storage.auth_key(),
102
+ await client.storage.test_mode(),
103
+ is_media=True,
104
  )
105
  await media_session.start()
106
  logging.debug(f"Created media session for DC {file_id.dc_id}")
 
146
  file_size_limit_mib = 4000 if client.me.is_premium else 2000
147
  #file_size_limit_mib = 4000
148
  if file_details["file"]["file_size"] > file_size_limit_mib * 1024 * 1024:
149
+ raise ValueError(f"Can't upload files bigger than {file_size_limit_mib} MiB")
 
150
 
151
  #file_total_parts = file_details["total_parts"]
152
  #is_big = file_size > 10 * 1024 * 1024
 
171
  file_total_parts=file_details["file"]["total_parts"],
172
  bytes=chunk)
173
  response['status'] = "success"
174
+ response['message'] = f"Uploading as Bigfile {file_details['file']['file_part']}/{file_details['file']['total_parts']}"
 
175
  print("Response", response)
176
 
177
  await session.invoke(rpc)
 
192
  file=final,
193
  mime_type=file_details['file']["mime_type"],
194
  attributes=[
195
+ raw.types.DocumentAttributeFilename(file_name=file_details['file']["file_name"])
 
196
  ])
197
 
198
  try:
 
237
  )
238
 
239
  #log_msg = await send_file(FileStream, db_id, file_info['file_id'], message)
240
+ #await update_file_id(log_msg.id, MULTI_CLIENTS))
241
  except Exception as e:
242
  await client.send_message(chat_id=Telegram.ULOG_GROUP,
243
  text=f"**#EʀʀᴏʀTʀᴀᴄᴋᴇʙᴀᴄᴋ:** `{e}`",
FileStream/utils/FileProcessors/file_properties.py CHANGED
@@ -8,7 +8,8 @@ from pyrogram.enums import ParseMode, ChatType
8
  from pyrogram.types import Message
9
  from pyrogram.file_id import FileId
10
  #-----------------------------------------------------
11
- from FileStream.bot import FileStream, multi_clients
 
12
  from FileStream.Database import Database
13
  from FileStream.config import Telegram, Server
14
  from FileStream.Tools import Time_ISTKolNow
@@ -39,9 +40,9 @@ async def send_file(client: Client, db_id, file_id: str, message, send_to):
39
  return {"message": log_msg, "sent_to": send_to}
40
 
41
 
42
- async def update_file_id(message, multi_clients):
43
  file_ids = {}
44
- for client_id, client in multi_clients.items():
45
  #log_msg = await client.get_messages(message['location'],message['message_id'])
46
  media = get_media_from_message(await client.get_messages(message['location'],message['message_id']))
47
  file_ids[str(client.id)] = getattr(media, "file_id", "")
@@ -81,7 +82,7 @@ async def get_file_ids(client: Client | bool, db_id: str, message) -> Optional[F
81
  log_msg = await send_file(FileStream, db_id,file_info['file']['file_id'], message,Telegram.FLOG_CHANNEL)
82
  #updated_info = update_file_info(log_msg)
83
  await db.update_file_info(db_id, update_file_info(log_msg))
84
- await db.update_file_ids(db_id, await update_file_id(await db.get_file(db_id),multi_clients))
85
 
86
  logging.debug("Stored file_id of all clients in DB")
87
  if not client:
@@ -89,7 +90,7 @@ async def get_file_ids(client: Client | bool, db_id: str, message) -> Optional[F
89
  file_info = await db.get_file(db_id)
90
 
91
  if file_info['user_type'] == "WEB":
92
- await db.update_file_ids(db_id, await update_file_id(await db.get_file(db_id), multi_clients))
93
  logging.debug("Stored file_id of all clients in DB")
94
  if not client:
95
  return
 
8
  from pyrogram.types import Message
9
  from pyrogram.file_id import FileId
10
  #-----------------------------------------------------
11
+ from FileStream.bot import FileStream
12
+ from FileStream.bot import MULTI_CLIENTS
13
  from FileStream.Database import Database
14
  from FileStream.config import Telegram, Server
15
  from FileStream.Tools import Time_ISTKolNow
 
40
  return {"message": log_msg, "sent_to": send_to}
41
 
42
 
43
+ async def update_file_id(message, MULTI_CLIENTS):
44
  file_ids = {}
45
+ for client_id, client in MULTI_CLIENTS.items():
46
  #log_msg = await client.get_messages(message['location'],message['message_id'])
47
  media = get_media_from_message(await client.get_messages(message['location'],message['message_id']))
48
  file_ids[str(client.id)] = getattr(media, "file_id", "")
 
82
  log_msg = await send_file(FileStream, db_id,file_info['file']['file_id'], message,Telegram.FLOG_CHANNEL)
83
  #updated_info = update_file_info(log_msg)
84
  await db.update_file_info(db_id, update_file_info(log_msg))
85
+ await db.update_file_ids(db_id, await update_file_id(await db.get_file(db_id),MULTI_CLIENTS))
86
 
87
  logging.debug("Stored file_id of all clients in DB")
88
  if not client:
 
90
  file_info = await db.get_file(db_id)
91
 
92
  if file_info['user_type'] == "WEB":
93
+ await db.update_file_ids(db_id, await update_file_id(await db.get_file(db_id), MULTI_CLIENTS))
94
  logging.debug("Stored file_id of all clients in DB")
95
  if not client:
96
  return
Unused Codes/__main__old.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ import asyncio
3
+ import logging
4
+ import traceback
5
+ import logging.handlers as handlers
6
+ from FileStream.config import Telegram, Server
7
+ from aiohttp import web
8
+ from pyrogram import idle
9
+
10
+ from FileStream.bot import FileStream
11
+ from FileStream.Tools import Time_ISTKolNow
12
+ from FileStream.server import web_server
13
+ from FileStream.bot.clients import initialize_clients
14
+
15
+ logging.basicConfig(
16
+ level=logging.INFO,
17
+ datefmt="%d/%m/%Y %H:%M:%S",
18
+ format=
19
+ '[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
20
+ handlers=[
21
+ logging.StreamHandler(stream=sys.stdout),
22
+ handlers.RotatingFileHandler("streambot.log",
23
+ mode="a",
24
+ maxBytes=104857600,
25
+ backupCount=2,
26
+ encoding="utf-8")
27
+ ],
28
+ )
29
+
30
+ logging.getLogger("aiohttp").setLevel(logging.ERROR)
31
+ logging.getLogger("pyrogram").setLevel(logging.ERROR)
32
+ logging.getLogger("aiohttp.web").setLevel(logging.ERROR)
33
+
34
+ server = web.AppRunner(web_server())
35
+
36
+ loop = asyncio.get_event_loop()
37
+
38
+
39
+ async def start_services():
40
+ print("****")
41
+ if Telegram.SECONDARY:
42
+ print("------------------ Starting as Secondary Server ------------------")
43
+ else:
44
+ print("------------------- Starting as Primary Server -------------------")
45
+ print( "****")
46
+ print("-------------------- Initializing Telegram Bot --------------------")
47
+
48
+ await FileStream.start()
49
+ bot_info = await FileStream.get_me()
50
+ FileStream.id = bot_info.id
51
+ FileStream.username = bot_info.username
52
+ FileStream.fname = bot_info.first_name
53
+ print("------------------------------ DONE ------------------------------")
54
+ print("\n")
55
+ print("---------------------- Initializing Clients ----------------------")
56
+ await initialize_clients()
57
+ print("------------------------------ DONE ------------------------------")
58
+ print("\n")
59
+ print("--------------------- Initializing Web Server ---------------------")
60
+ await server.setup()
61
+ await web.TCPSite(server, Server.BIND_ADDRESS, Server.PORT).start()
62
+ print("------------------------------ DONE ------------------------------")
63
+ print("\n")
64
+ print("------------------------- Service Started -------------------------")
65
+ print("Bot =>> {}".format(bot_info.first_name))
66
+ if bot_info.dc_id:
67
+ print("DC ID =>> {}".format(str(bot_info.dc_id)))
68
+ print(" URL =>> {}".format(Server.URL))
69
+ print("------------------------------------------------------------------")
70
+ """
71
+ all_sources = [
72
+ Telegram.ULOG_GROUP, Telegram.FLOG_CHANNEL, Telegram.PFLOG_CHANNEL
73
+ ]
74
+ for source in all_sources:
75
+ await FileStream.send_message(chat_id=source,
76
+ text=f"Hi, I am Online @{ISTKolNow()}",
77
+ disable_web_page_preview=True)
78
+ """
79
+ await idle()
80
+
81
+
82
+ async def cleanup():
83
+ await server.cleanup()
84
+ #await FileStream.stop()
85
+
86
+
87
+ if __name__ == "__main__":
88
+ try:
89
+ loop.run_until_complete(start_services())
90
+ except KeyboardInterrupt:
91
+ loop.stop()
92
+ print("------------------------ Stopped Services ------------------------")
93
+ except Exception as err:
94
+ logging.error(traceback.format_exc())