Files
monitor-shared-utils/monitor_utils/config.py
T
2026-06-21 14:59:37 -07:00

62 lines
1.5 KiB
Python

import json
import pathlib
from functools import cache
from monitor_utils.constants import (
PROXIES_PATH,
PROJECT_ROOT,
DEVELOPMENT_MODE,
PRODUCTION_BOT_TOKEN,
DEVELOPMENT_BOT_TOKEN,
DevelopmentMode,
)
def is_production(override_production=False) -> bool:
if override_production:
return False
return DEVELOPMENT_MODE == DevelopmentMode.PRODUCTION
def get_bot_token(is_production: bool) -> str:
if is_production:
return PRODUCTION_BOT_TOKEN
return DEVELOPMENT_BOT_TOKEN
@cache
def get_proxies() -> list[str]:
with PROXIES_PATH.open() as f:
proxies = []
for line in f:
line = line.strip()
if not line:
continue
parts = line.split(":", 3)
if len(parts) == 4 and not parts[1].startswith("//"):
host, port, user, password = parts
proxies.append(f"http://{user}:{password}@{host}:{port}")
else:
proxies.append(f"http://{line}")
if not proxies:
raise ValueError("Proxy list cannot be empty")
return proxies
def get_config(
monitor_name: str, use_production_path: bool = False
) -> tuple[dict, pathlib.Path]:
monitor_name = monitor_name.replace(".json", "")
if use_production_path:
config_path = PROJECT_ROOT / "configs" / f"{monitor_name}.json"
else:
config_path = PROJECT_ROOT / "tests" / "configs" / f"{monitor_name}.json"
with open(config_path) as f:
return json.load(f), config_path