Spaces:
Paused
Paused
Upload 2 files
Browse files- utils/clients.py +144 -144
- utils/extra.py +2 -13
utils/clients.py
CHANGED
|
@@ -1,144 +1,144 @@
|
|
| 1 |
-
import asyncio, config
|
| 2 |
-
from pathlib import Path
|
| 3 |
-
from pyrogram import Client
|
| 4 |
-
from utils.directoryHandler import backup_drive_data, loadDriveData
|
| 5 |
-
from utils.logger import Logger
|
| 6 |
-
import os
|
| 7 |
-
import signal
|
| 8 |
-
|
| 9 |
-
logger = Logger(__name__)
|
| 10 |
-
|
| 11 |
-
multi_clients = {}
|
| 12 |
-
premium_clients = {}
|
| 13 |
-
work_loads = {}
|
| 14 |
-
premium_work_loads = {}
|
| 15 |
-
main_bot = None
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
async def initialize_clients():
|
| 19 |
-
global multi_clients, work_loads, premium_clients, premium_work_loads
|
| 20 |
-
logger.info("Initializing Clients")
|
| 21 |
-
|
| 22 |
-
session_cache_path = Path(f"./cache")
|
| 23 |
-
session_cache_path.parent.mkdir(parents=True, exist_ok=True)
|
| 24 |
-
|
| 25 |
-
all_tokens = dict((i, t) for i, t in enumerate(config.BOT_TOKENS, start=1))
|
| 26 |
-
all_sessions = dict(
|
| 27 |
-
(i, s) for i, s in enumerate(config.STRING_SESSIONS, start=len(all_tokens) + 1)
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
# Scan for existing session files to allow starting without secrets
|
| 31 |
-
if session_cache_path.exists():
|
| 32 |
-
for file_path in session_cache_path.glob("*.session"):
|
| 33 |
-
if file_path.stem.isdigit():
|
| 34 |
-
client_id = int(file_path.stem)
|
| 35 |
-
# Only add if not already in config lists
|
| 36 |
-
if client_id not in all_tokens and client_id not in all_sessions:
|
| 37 |
-
logger.info(f"Found existing session file for Client {client_id}")
|
| 38 |
-
all_tokens[client_id] = None
|
| 39 |
-
|
| 40 |
-
async def start_client(client_id, token, type):
|
| 41 |
-
name = str(client_id)
|
| 42 |
-
for attempt in range(2):
|
| 43 |
-
try:
|
| 44 |
-
if attempt == 0:
|
| 45 |
-
logger.info(f"Starting - {type.title()} Client {client_id}")
|
| 46 |
-
else:
|
| 47 |
-
logger.info(f"Retrying - {type.title()} Client {client_id}")
|
| 48 |
-
|
| 49 |
-
if type == "bot":
|
| 50 |
-
client = Client(
|
| 51 |
-
name=name,
|
| 52 |
-
api_id=config.API_ID,
|
| 53 |
-
api_hash=config.API_HASH,
|
| 54 |
-
bot_token=token,
|
| 55 |
-
workdir=session_cache_path,
|
| 56 |
-
)
|
| 57 |
-
client.loop = asyncio.get_running_loop()
|
| 58 |
-
await client.start()
|
| 59 |
-
await client.send_message(
|
| 60 |
-
config.STORAGE_CHANNEL,
|
| 61 |
-
f"Started - {type.title()} Client {client_id}",
|
| 62 |
-
)
|
| 63 |
-
multi_clients[client_id] = client
|
| 64 |
-
work_loads[client_id] = 0
|
| 65 |
-
elif type == "user":
|
| 66 |
-
client = await Client(
|
| 67 |
-
name=name,
|
| 68 |
-
api_id=config.API_ID,
|
| 69 |
-
api_hash=config.API_HASH,
|
| 70 |
-
session_string=token,
|
| 71 |
-
sleep_threshold=config.SLEEP_THRESHOLD,
|
| 72 |
-
workdir=session_cache_path,
|
| 73 |
-
no_updates=True,
|
| 74 |
-
).start()
|
| 75 |
-
await client.send_message(
|
| 76 |
-
config.STORAGE_CHANNEL,
|
| 77 |
-
f"Started - {type.title()} Client {client_id}",
|
| 78 |
-
)
|
| 79 |
-
premium_clients[client_id] = client
|
| 80 |
-
premium_work_loads[client_id] = 0
|
| 81 |
-
|
| 82 |
-
logger.info(f"Started - {type.title()} Client {client_id}")
|
| 83 |
-
return
|
| 84 |
-
except Exception as e:
|
| 85 |
-
logger.error(
|
| 86 |
-
f"Failed To Start {type.title()} Client - {client_id} Error: {e}"
|
| 87 |
-
)
|
| 88 |
-
if attempt == 0:
|
| 89 |
-
# Try to close client to release file locks
|
| 90 |
-
if 'client' in locals():
|
| 91 |
-
try:
|
| 92 |
-
await client.stop()
|
| 93 |
-
except:
|
| 94 |
-
pass
|
| 95 |
-
|
| 96 |
-
session_path = session_cache_path / f"{name}.session"
|
| 97 |
-
if session_path.exists():
|
| 98 |
-
logger.info(f"Deleting invalid session file: {session_path}")
|
| 99 |
-
try:
|
| 100 |
-
session_path.unlink()
|
| 101 |
-
except Exception as delete_error:
|
| 102 |
-
logger.error(f"Could not delete session file: {delete_error}")
|
| 103 |
-
else:
|
| 104 |
-
logger.error(f"Client {client_id} failed to start after retry.")
|
| 105 |
-
|
| 106 |
-
await asyncio.gather(
|
| 107 |
-
*(
|
| 108 |
-
[
|
| 109 |
-
start_client(client_id, client, "bot")
|
| 110 |
-
for client_id, client in all_tokens.items()
|
| 111 |
-
]
|
| 112 |
-
+ [
|
| 113 |
-
start_client(client_id, client, "user")
|
| 114 |
-
for client_id, client in all_sessions.items()
|
| 115 |
-
]
|
| 116 |
-
)
|
| 117 |
-
)
|
| 118 |
-
if len(multi_clients) == 0:
|
| 119 |
-
logger.error("No Clients Were Initialized")
|
| 120 |
-
raise RuntimeError("No Clients Were Initialized - Please add BOT_TOKENS to config or ensure valid .session files are in the cache folder.")
|
| 121 |
-
|
| 122 |
-
if len(premium_clients) == 0:
|
| 123 |
-
logger.info("No Premium Clients Were Initialized")
|
| 124 |
-
|
| 125 |
-
logger.info("Clients Initialized")
|
| 126 |
-
|
| 127 |
-
# Load the drive data
|
| 128 |
-
await loadDriveData()
|
| 129 |
-
|
| 130 |
-
# Start the backup drive data task
|
| 131 |
-
asyncio.create_task(backup_drive_data())
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
def get_client(premium_required=False) -> Client:
|
| 135 |
-
global multi_clients, work_loads, premium_clients, premium_work_loads
|
| 136 |
-
|
| 137 |
-
if premium_required:
|
| 138 |
-
index = min(premium_work_loads, key=premium_work_loads.get)
|
| 139 |
-
premium_work_loads[index] += 1
|
| 140 |
-
return premium_clients[index]
|
| 141 |
-
|
| 142 |
-
index = min(work_loads, key=work_loads.get)
|
| 143 |
-
work_loads[index] += 1
|
| 144 |
-
return multi_clients[index]
|
|
|
|
| 1 |
+
import asyncio, config
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from pyrogram import Client
|
| 4 |
+
from utils.directoryHandler import backup_drive_data, loadDriveData
|
| 5 |
+
from utils.logger import Logger
|
| 6 |
+
import os
|
| 7 |
+
import signal
|
| 8 |
+
|
| 9 |
+
logger = Logger(__name__)
|
| 10 |
+
|
| 11 |
+
multi_clients = {}
|
| 12 |
+
premium_clients = {}
|
| 13 |
+
work_loads = {}
|
| 14 |
+
premium_work_loads = {}
|
| 15 |
+
main_bot = None
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
async def initialize_clients():
|
| 19 |
+
global multi_clients, work_loads, premium_clients, premium_work_loads
|
| 20 |
+
logger.info("Initializing Clients")
|
| 21 |
+
|
| 22 |
+
session_cache_path = Path(f"./cache")
|
| 23 |
+
session_cache_path.parent.mkdir(parents=True, exist_ok=True)
|
| 24 |
+
|
| 25 |
+
all_tokens = dict((i, t) for i, t in enumerate(config.BOT_TOKENS, start=1))
|
| 26 |
+
all_sessions = dict(
|
| 27 |
+
(i, s) for i, s in enumerate(config.STRING_SESSIONS, start=len(all_tokens) + 1)
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Scan for existing session files to allow starting without secrets
|
| 31 |
+
if session_cache_path.exists():
|
| 32 |
+
for file_path in session_cache_path.glob("*.session"):
|
| 33 |
+
if file_path.stem.isdigit():
|
| 34 |
+
client_id = int(file_path.stem)
|
| 35 |
+
# Only add if not already in config lists
|
| 36 |
+
if client_id not in all_tokens and client_id not in all_sessions:
|
| 37 |
+
logger.info(f"Found existing session file for Client {client_id}")
|
| 38 |
+
all_tokens[client_id] = None
|
| 39 |
+
|
| 40 |
+
async def start_client(client_id, token, type):
|
| 41 |
+
name = str(client_id)
|
| 42 |
+
for attempt in range(2):
|
| 43 |
+
try:
|
| 44 |
+
if attempt == 0:
|
| 45 |
+
logger.info(f"Starting - {type.title()} Client {client_id}")
|
| 46 |
+
else:
|
| 47 |
+
logger.info(f"Retrying - {type.title()} Client {client_id}")
|
| 48 |
+
|
| 49 |
+
if type == "bot":
|
| 50 |
+
client = Client(
|
| 51 |
+
name=name,
|
| 52 |
+
api_id=config.API_ID,
|
| 53 |
+
api_hash=config.API_HASH,
|
| 54 |
+
bot_token=token,
|
| 55 |
+
workdir=session_cache_path,
|
| 56 |
+
)
|
| 57 |
+
client.loop = asyncio.get_running_loop()
|
| 58 |
+
await client.start()
|
| 59 |
+
await client.send_message(
|
| 60 |
+
config.STORAGE_CHANNEL,
|
| 61 |
+
f"Started - {type.title()} Client {client_id}",
|
| 62 |
+
)
|
| 63 |
+
multi_clients[client_id] = client
|
| 64 |
+
work_loads[client_id] = 0
|
| 65 |
+
elif type == "user":
|
| 66 |
+
client = await Client(
|
| 67 |
+
name=name,
|
| 68 |
+
api_id=config.API_ID,
|
| 69 |
+
api_hash=config.API_HASH,
|
| 70 |
+
session_string=token,
|
| 71 |
+
sleep_threshold=config.SLEEP_THRESHOLD,
|
| 72 |
+
workdir=session_cache_path,
|
| 73 |
+
no_updates=True,
|
| 74 |
+
).start()
|
| 75 |
+
await client.send_message(
|
| 76 |
+
config.STORAGE_CHANNEL,
|
| 77 |
+
f"Started - {type.title()} Client {client_id}",
|
| 78 |
+
)
|
| 79 |
+
premium_clients[client_id] = client
|
| 80 |
+
premium_work_loads[client_id] = 0
|
| 81 |
+
|
| 82 |
+
logger.info(f"Started - {type.title()} Client {client_id}")
|
| 83 |
+
return
|
| 84 |
+
except Exception as e:
|
| 85 |
+
logger.error(
|
| 86 |
+
f"Failed To Start {type.title()} Client - {client_id} Error: {e}"
|
| 87 |
+
)
|
| 88 |
+
if attempt == 0:
|
| 89 |
+
# Try to close client to release file locks
|
| 90 |
+
if 'client' in locals():
|
| 91 |
+
try:
|
| 92 |
+
await client.stop()
|
| 93 |
+
except:
|
| 94 |
+
pass
|
| 95 |
+
|
| 96 |
+
session_path = session_cache_path / f"{name}.session"
|
| 97 |
+
if session_path.exists():
|
| 98 |
+
logger.info(f"Deleting invalid session file: {session_path}")
|
| 99 |
+
try:
|
| 100 |
+
session_path.unlink()
|
| 101 |
+
except Exception as delete_error:
|
| 102 |
+
logger.error(f"Could not delete session file: {delete_error}")
|
| 103 |
+
else:
|
| 104 |
+
logger.error(f"Client {client_id} failed to start after retry.")
|
| 105 |
+
|
| 106 |
+
await asyncio.gather(
|
| 107 |
+
*(
|
| 108 |
+
[
|
| 109 |
+
start_client(client_id, client, "bot")
|
| 110 |
+
for client_id, client in all_tokens.items()
|
| 111 |
+
]
|
| 112 |
+
+ [
|
| 113 |
+
start_client(client_id, client, "user")
|
| 114 |
+
for client_id, client in all_sessions.items()
|
| 115 |
+
]
|
| 116 |
+
)
|
| 117 |
+
)
|
| 118 |
+
if len(multi_clients) == 0:
|
| 119 |
+
logger.error("No Clients Were Initialized")
|
| 120 |
+
raise RuntimeError("No Clients Were Initialized - Please add BOT_TOKENS to config or ensure valid .session files are in the cache folder.")
|
| 121 |
+
|
| 122 |
+
if len(premium_clients) == 0:
|
| 123 |
+
logger.info("No Premium Clients Were Initialized")
|
| 124 |
+
|
| 125 |
+
logger.info("Clients Initialized")
|
| 126 |
+
|
| 127 |
+
# Load the drive data
|
| 128 |
+
await loadDriveData()
|
| 129 |
+
|
| 130 |
+
# Start the backup drive data task
|
| 131 |
+
asyncio.create_task(backup_drive_data())
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def get_client(premium_required=False) -> Client:
|
| 135 |
+
global multi_clients, work_loads, premium_clients, premium_work_loads
|
| 136 |
+
|
| 137 |
+
if premium_required:
|
| 138 |
+
index = min(premium_work_loads, key=premium_work_loads.get)
|
| 139 |
+
premium_work_loads[index] += 1
|
| 140 |
+
return premium_clients[index]
|
| 141 |
+
|
| 142 |
+
index = min(work_loads, key=work_loads.get)
|
| 143 |
+
work_loads[index] += 1
|
| 144 |
+
return multi_clients[index]
|
utils/extra.py
CHANGED
|
@@ -66,21 +66,10 @@ def reset_cache_dir():
|
|
| 66 |
shutil.rmtree(downloads_dir, ignore_errors=True)
|
| 67 |
downloads_dir.mkdir(parents=True, exist_ok=True)
|
| 68 |
|
| 69 |
-
if cache_dir.exists():
|
| 70 |
-
for item in cache_dir.iterdir():
|
| 71 |
-
if item.is_dir():
|
| 72 |
-
shutil.rmtree(item, ignore_errors=True)
|
| 73 |
-
elif item.name.endswith(".session") or item.name == "drive.data":
|
| 74 |
-
continue
|
| 75 |
-
else:
|
| 76 |
-
try:
|
| 77 |
-
item.unlink()
|
| 78 |
-
except Exception:
|
| 79 |
-
pass
|
| 80 |
-
else:
|
| 81 |
cache_dir.mkdir(parents=True, exist_ok=True)
|
| 82 |
|
| 83 |
-
logger.info("Cache and downloads directory
|
| 84 |
|
| 85 |
|
| 86 |
def parse_content_disposition(content_disposition):
|
|
|
|
| 66 |
shutil.rmtree(downloads_dir, ignore_errors=True)
|
| 67 |
downloads_dir.mkdir(parents=True, exist_ok=True)
|
| 68 |
|
| 69 |
+
if not cache_dir.exists():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
cache_dir.mkdir(parents=True, exist_ok=True)
|
| 71 |
|
| 72 |
+
logger.info("Cache and downloads directory checked")
|
| 73 |
|
| 74 |
|
| 75 |
def parse_content_disposition(content_disposition):
|