config.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import yaml
  2. from pathlib import Path
  3. from pydantic import BaseModel, Field
  4. from typing import List, Dict, Union,Optional,Any
  5. from utils.pydantic_auto_field import AutoLoadModel
  6. APP_PATH = Path(__file__).parent.parent
  7. OUTPUT_DIR = APP_PATH / "output"
  8. CONFIG_PATH = APP_PATH / "config.yaml"
  9. REPO_BASE_DIR = Path(APP_PATH.parent.parent)
  10. DOWNLOAD_DIR = REPO_BASE_DIR / "download"
  11. PROXY_POLL_DIR = DOWNLOAD_DIR / "proxy_pool"
  12. WORKER_DIR_BASE = APP_PATH.parent.parent
  13. class Proxy(BaseModel):
  14. name: Optional[str] = ''
  15. port: Optional[int] = -1
  16. file_path: Optional[str] = ''
  17. startup: Optional[bool] = False
  18. class Sub(AutoLoadModel):
  19. url: Optional[str] = None
  20. start_port: Optional[int] = 9660
  21. redis_url: Optional[str] = 'redis://localhost:6379/8'
  22. file: Optional[str] = str(REPO_BASE_DIR / r'download\proxy_pool\6137e542.yaml')
  23. temp_dir: Optional[str] = str(PROXY_POLL_DIR / "temp")
  24. auto_start: Optional[bool] = True
  25. proxies: Optional[Dict[Union[int,str], Proxy]] = {}
  26. def __init__(self, **data):
  27. super().__init__(**data)
  28. if self.proxies:
  29. self.proxies = {k: Proxy(**v) if isinstance(v, dict) else v for k, v in self.proxies.items()}
  30. class Browser(BaseModel):
  31. exe_path: Optional[str] = str(REPO_BASE_DIR / r"download\GoogleChromePortable\GoogleChromePortable.exe")
  32. no_imgs: Optional[bool] = True
  33. class Backend(BaseModel):
  34. host: Optional[str] = "localhost"
  35. port: Optional[int] = 5835
  36. class RedisConfig(BaseModel):
  37. host: Optional[str] = "localhost"
  38. port: Optional[int] = 6379
  39. db: Optional[int] = 1
  40. exe: Optional[str] = str(REPO_BASE_DIR / r"download\Redis-x64-5.0.14.1\redis-server.exe")
  41. class Config(BaseModel):
  42. sub: Optional[Sub] = Sub()
  43. select_proxy: Optional[str] = "system"
  44. mimo_exe: Optional[str] = str(PROXY_POLL_DIR / r"mihomo-windows-amd64-go120.exe")
  45. pandoc_exe: Optional[str] = str(REPO_BASE_DIR / r"download\pandoc-3.6.3-windows-x86_64\pandoc.exe")
  46. worker_backend_py: Optional[str] = str(WORKER_DIR_BASE / r"worker\api\worker_server.py")
  47. sqluri: Optional[str] = 'sqlite:///' + str(REPO_BASE_DIR / r'output\temp.db')
  48. browser: Optional[Browser] = Browser()
  49. backend: Optional[Backend] = Backend()
  50. worker_backend: Optional[Backend] = Field(default_factory=lambda: Backend(host="localhost", port=8003))
  51. redis: Optional[RedisConfig] = RedisConfig()
  52. def save(self):
  53. config_path = get_config_path()
  54. with open(config_path, "w", encoding="utf-8") as file:
  55. yaml.dump(self.model_dump(), file)
  56. return self
  57. def get_config_path():
  58. return CONFIG_PATH
  59. def read_config(config_path: Path):
  60. if not config_path.exists():
  61. config = Config()
  62. config.save()
  63. return config
  64. with open(config_path, "r", encoding="utf-8") as file:
  65. config_dict = yaml.safe_load(file)
  66. return Config(**config_dict)
  67. config = read_config(get_config_path())
  68. WORKER_SERVICE_URL = f"http://{config.worker_backend.host}:{config.worker_backend.port}"
  69. def main():
  70. print(config)
  71. config.save()
  72. if __name__ == "__main__":
  73. main()