config.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import yaml
  2. from pathlib import Path
  3. from pydantic import BaseModel
  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 # Changed to int
  21. redis_url: Optional[str] = 'redis://localhost:6379/8'
  22. file: Optional[str] = None
  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. # Convert proxies dictionary values to Proxy objects
  29. if self.proxies:
  30. self.proxies = {k: Proxy(**v) if isinstance(v, dict) else v for k, v in self.proxies.items()}
  31. class Browser(BaseModel):
  32. exe_path: Optional[str] = str(REPO_BASE_DIR / r"download\chrome-win\chrome.exe")
  33. no_imgs: Optional[bool] = True
  34. class Config(BaseModel):
  35. sub: Optional[Sub] = Sub()
  36. select_proxy: Optional[str] = "system"
  37. mimo_exe: Optional[str] = str(PROXY_POLL_DIR / r"mihomo-windows-amd64-go120.exe")
  38. redis_exe: Optional[str] = str(REPO_BASE_DIR / r"download\Redis-x64-5.0.14.1\redis-server.exe")
  39. worker_backend_py: Optional[str] = str(WORKER_DIR_BASE / r"worker\api\worker_server.py")
  40. sqluri: Optional[str] = r'G:\code\upwork\zhang_crawl_bio\output\temp.db'
  41. browser: Optional[Browser] = Browser()
  42. redis_port: Optional[int] = None # Changed to int
  43. def save(self):
  44. config_path = get_config_path()
  45. with open(config_path, "w", encoding="utf-8") as file:
  46. yaml.dump(self.model_dump(), file, encoding="utf-8", )
  47. return self
  48. def get_config_path():
  49. return CONFIG_PATH
  50. def read_config(config_path: Path):
  51. if not config_path.exists():
  52. config = Config()
  53. config.save()
  54. return config
  55. with open(config_path, "r", encoding="utf-8") as file:
  56. config_dict = yaml.safe_load(file)
  57. config = Config(**config_dict)
  58. return config
  59. config = read_config(get_config_path())
  60. import os
  61. WORKER_SERVICE_URL = os.getenv("WORKER_SERVICE_URL", "http://localhost:8003")
  62. def main():
  63. print(config)
  64. config.browser = Browser()
  65. config.save(
  66. )
  67. if __name__ == "__main__":
  68. main()