Repository navigation
Expand file tree
/
Copy pathconfig.py
More file actions
59 lines (50 loc) · 1.9 KB
/
Copy pathconfig.py
File metadata and controls
59 lines (50 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
import json
from typing import Dict, Any
from pathlib import Path
class Config:
def __init__(self):
self.config_file = Path("config.json")
self.env_file = Path(".env")
self._load_env_file()
self._config = self._load_config()
def _load_env_file(self):
"""Load environment variables from .env file"""
if self.env_file.exists():
with open(self.env_file, 'r') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#') and '=' in line:
key, value = line.split('=', 1)
os.environ[key.strip()] = value.strip()
def _load_config(self) -> Dict[str, Any]:
api_id = os.getenv("API_ID")
config = {
"api_id": int(api_id) if api_id else None,
"api_hash": os.getenv("API_HASH"),
"bot_token": os.getenv("BOT_TOKEN"),
"database_path": "database.db",
"sessions_dir": "sessions",
"temp_dir": "temp",
"tdata_dir": "TData",
"max_sessions": 10,
"rate_limit": 30,
"log_level": "INFO",
"encryption_key": os.getenv("ENCRYPTION_KEY"),
"webhook_url": os.getenv("WEBHOOK_URL"),
"proxy_url": os.getenv("PROXY_URL")
}
if self.config_file.exists():
with open(self.config_file, 'r') as f:
file_config = json.load(f)
config.update(file_config)
return config
def get(self, key: str, default=None):
return self._config.get(key, default)
def set(self, key: str, value: Any):
self._config[key] = value
self._save_config()
def _save_config(self):
with open(self.config_file, 'w') as f:
json.dump(self._config, f, indent=2)
config = Config()