Spaces:
Runtime error
Runtime error
Update config.py
Browse files
config.py
CHANGED
|
@@ -1,62 +1,43 @@
|
|
| 1 |
-
# filename: config.py (Corrected Version)
|
| 2 |
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
-
# Load environment variables from a .env file for local development
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
-
# --- Helper function to cast environment variables (CORRECTED) ---
|
| 11 |
def get_env(key, default=None, cast_to=str):
|
| 12 |
-
"""
|
| 13 |
-
Fetches an environment variable, casts it to a specified type,
|
| 14 |
-
and correctly handles non-string default values.
|
| 15 |
-
"""
|
| 16 |
value = os.getenv(key, default)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
if value
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# --- The Fix ---
|
| 23 |
-
# If the retrieved value is already the type we want (e.g., default=True was used),
|
| 24 |
-
# return it directly without trying to process it as a string.
|
| 25 |
-
if isinstance(value, cast_to):
|
| 26 |
-
return value
|
| 27 |
|
| 28 |
-
|
| 29 |
-
if cast_to == bool:
|
| 30 |
-
return value.lower() in ('true', '1', 't', 'y', 'yes')
|
| 31 |
-
|
| 32 |
-
try:
|
| 33 |
-
return cast_to(value)
|
| 34 |
-
except (ValueError, TypeError):
|
| 35 |
-
return default
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# --- Telegram Credentials (Mandatory) ---
|
| 39 |
API_ID = get_env("API_ID", cast_to=int)
|
| 40 |
API_HASH = get_env("API_HASH")
|
| 41 |
BOT_TOKEN = get_env("BOT_TOKEN")
|
| 42 |
OWNER_ID = get_env("OWNER_ID", cast_to=int)
|
| 43 |
-
|
| 44 |
if not all([API_ID, API_HASH, BOT_TOKEN, OWNER_ID]):
|
| 45 |
raise ValueError("Error: One or more mandatory Telegram credentials are not set.")
|
| 46 |
|
| 47 |
-
# --- Database Configuration
|
| 48 |
mongo_vars = {}
|
| 49 |
for key, value in os.environ.items():
|
| 50 |
if match := re.match(r"MONGODB_URL_(\d+)", key):
|
| 51 |
-
if value:
|
| 52 |
-
mongo_vars[int(match.group(1))] = value
|
| 53 |
-
|
| 54 |
MONGODB_URIS = [mongo_vars[key] for key in sorted(mongo_vars.keys())]
|
| 55 |
-
|
| 56 |
if not MONGODB_URIS:
|
| 57 |
-
raise ValueError("Error: No MONGODB_URL_<n>
|
| 58 |
|
| 59 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
TERABOX_WORKER_URL = get_env("TERABOX_WORKER_URL")
|
| 61 |
if not TERABOX_WORKER_URL:
|
| 62 |
raise ValueError("Error: TERABOX_WORKER_URL is not set.")
|
|
@@ -68,10 +49,6 @@ BACKUP_CHANNEL_ID = get_env("BACKUP_CHANNEL_ID", cast_to=int)
|
|
| 68 |
# --- Performance, Limits & Toggles ---
|
| 69 |
PREMIUM_WORKERS = get_env("PREMIUM_WORKERS", 1, cast_to=int)
|
| 70 |
FREE_WORKERS = get_env("FREE_WORKERS", 1, cast_to=int)
|
| 71 |
-
|
| 72 |
_free_limit_mb = get_env("FREE_USER_FILE_SIZE_LIMIT", 50, cast_to=int)
|
| 73 |
FREE_USER_FILE_SIZE_LIMIT_BYTES = _free_limit_mb * 1024 * 1024
|
| 74 |
-
|
| 75 |
-
# This line now works correctly with the fixed helper function.
|
| 76 |
ENABLE_FFMPEG = get_env("ENABLE_FFMPEG", True, cast_to=bool)
|
| 77 |
-
|
|
|
|
| 1 |
+
# filename: config.py (Corrected Version 2)
|
| 2 |
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
|
|
|
| 7 |
load_dotenv()
|
| 8 |
|
|
|
|
| 9 |
def get_env(key, default=None, cast_to=str):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
value = os.getenv(key, default)
|
| 11 |
+
if value is None: return None
|
| 12 |
+
if isinstance(value, cast_to): return value
|
| 13 |
+
if cast_to == bool: return value.lower() in ('true', '1', 't', 'y', 'yes')
|
| 14 |
+
try: return cast_to(value)
|
| 15 |
+
except (ValueError, TypeError): return default
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# --- Telegram Credentials ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
API_ID = get_env("API_ID", cast_to=int)
|
| 19 |
API_HASH = get_env("API_HASH")
|
| 20 |
BOT_TOKEN = get_env("BOT_TOKEN")
|
| 21 |
OWNER_ID = get_env("OWNER_ID", cast_to=int)
|
|
|
|
| 22 |
if not all([API_ID, API_HASH, BOT_TOKEN, OWNER_ID]):
|
| 23 |
raise ValueError("Error: One or more mandatory Telegram credentials are not set.")
|
| 24 |
|
| 25 |
+
# --- Database Configuration ---
|
| 26 |
mongo_vars = {}
|
| 27 |
for key, value in os.environ.items():
|
| 28 |
if match := re.match(r"MONGODB_URL_(\d+)", key):
|
| 29 |
+
if value: mongo_vars[int(match.group(1))] = value
|
|
|
|
|
|
|
| 30 |
MONGODB_URIS = [mongo_vars[key] for key in sorted(mongo_vars.keys())]
|
|
|
|
| 31 |
if not MONGODB_URIS:
|
| 32 |
+
raise ValueError("Error: No MONGODB_URL_<n> variables found. Set at least MONGODB_URL_1.")
|
| 33 |
|
| 34 |
+
# ** NEW LINE ADDED HERE **
|
| 35 |
+
# Get the specific database name to use within the cluster.
|
| 36 |
+
DATABASE_NAME = get_env("DATABASE_NAME", "terabox_bot_db")
|
| 37 |
+
if not DATABASE_NAME:
|
| 38 |
+
raise ValueError("Error: DATABASE_NAME environment variable is not set.")
|
| 39 |
+
|
| 40 |
+
# --- Bot Functionality ---
|
| 41 |
TERABOX_WORKER_URL = get_env("TERABOX_WORKER_URL")
|
| 42 |
if not TERABOX_WORKER_URL:
|
| 43 |
raise ValueError("Error: TERABOX_WORKER_URL is not set.")
|
|
|
|
| 49 |
# --- Performance, Limits & Toggles ---
|
| 50 |
PREMIUM_WORKERS = get_env("PREMIUM_WORKERS", 1, cast_to=int)
|
| 51 |
FREE_WORKERS = get_env("FREE_WORKERS", 1, cast_to=int)
|
|
|
|
| 52 |
_free_limit_mb = get_env("FREE_USER_FILE_SIZE_LIMIT", 50, cast_to=int)
|
| 53 |
FREE_USER_FILE_SIZE_LIMIT_BYTES = _free_limit_mb * 1024 * 1024
|
|
|
|
|
|
|
| 54 |
ENABLE_FFMPEG = get_env("ENABLE_FFMPEG", True, cast_to=bool)
|
|
|