Spaces:
Runtime error
Runtime error
Update config.py
Browse files
config.py
CHANGED
|
@@ -1,26 +1,39 @@
|
|
| 1 |
-
# filename: config.py
|
| 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 |
-
# In production (like Docker/Hugging Face), these will be set directly.
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
-
# --- Helper function to cast environment variables ---
|
| 12 |
def get_env(key, default=None, cast_to=str):
|
| 13 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 14 |
value = os.getenv(key, default)
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# --- Telegram Credentials (Mandatory) ---
|
| 26 |
API_ID = get_env("API_ID", cast_to=int)
|
|
@@ -29,17 +42,15 @@ BOT_TOKEN = get_env("BOT_TOKEN")
|
|
| 29 |
OWNER_ID = get_env("OWNER_ID", cast_to=int)
|
| 30 |
|
| 31 |
if not all([API_ID, API_HASH, BOT_TOKEN, OWNER_ID]):
|
| 32 |
-
raise ValueError("Error: One or more mandatory Telegram credentials are not set
|
| 33 |
|
| 34 |
# --- Database Configuration (Mandatory) ---
|
| 35 |
-
# This logic automatically finds all MONGODB_URL_n secrets you've set.
|
| 36 |
mongo_vars = {}
|
| 37 |
for key, value in os.environ.items():
|
| 38 |
if match := re.match(r"MONGODB_URL_(\d+)", key):
|
| 39 |
if value:
|
| 40 |
mongo_vars[int(match.group(1))] = value
|
| 41 |
|
| 42 |
-
# Sort the found URLs by their number (1, 2, 3...) to create the final list
|
| 43 |
MONGODB_URIS = [mongo_vars[key] for key in sorted(mongo_vars.keys())]
|
| 44 |
|
| 45 |
if not MONGODB_URIS:
|
|
@@ -61,5 +72,6 @@ FREE_WORKERS = get_env("FREE_WORKERS", 1, cast_to=int)
|
|
| 61 |
_free_limit_mb = get_env("FREE_USER_FILE_SIZE_LIMIT", 50, cast_to=int)
|
| 62 |
FREE_USER_FILE_SIZE_LIMIT_BYTES = _free_limit_mb * 1024 * 1024
|
| 63 |
|
| 64 |
-
#
|
| 65 |
ENABLE_FFMPEG = get_env("ENABLE_FFMPEG", True, cast_to=bool)
|
|
|
|
|
|
| 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 |
+
# If no value is found at all, return None
|
| 19 |
+
if value is None:
|
| 20 |
+
return None
|
| 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 |
+
# If we have a string from the environment, process it.
|
| 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)
|
|
|
|
| 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 (Mandatory) ---
|
|
|
|
| 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:
|
|
|
|
| 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 |
+
|