63 lines
1.5 KiB
Python
63 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]:
|
|
parts = p.strip().split(":")
|
|
print(len(parts), parts)
|
|
with PROXIES_PATH.open() as f:
|
|
proxies = []
|
|
for p in f:
|
|
if not p.strip():
|
|
continue
|
|
|
|
parts = p.strip().split(":")
|
|
if len(parts) == 4:
|
|
host, port, user, password = parts
|
|
proxies.append(f"http://{user}:{password}@{host}:{port}")
|
|
else:
|
|
proxies.append(f"http://{p.strip()}")
|
|
|
|
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
|