config.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. import configparser
  2. import os
  3. import re
  4. import shutil
  5. import sys
  6. def resource_path(relative_path, persistent=False):
  7. """
  8. Get the resource path
  9. """
  10. base_path = os.path.abspath(".")
  11. total_path = os.path.join(base_path, relative_path)
  12. if persistent or os.path.exists(total_path):
  13. return total_path
  14. else:
  15. try:
  16. base_path = sys._MEIPASS
  17. return os.path.join(base_path, relative_path)
  18. except Exception:
  19. return total_path
  20. def get_resolution_value(resolution_str):
  21. """
  22. Get resolution value from string
  23. """
  24. pattern = r"(\d+)[xX*](\d+)"
  25. match = re.search(pattern, resolution_str)
  26. if match:
  27. width, height = map(int, match.groups())
  28. return width * height
  29. else:
  30. return 0
  31. class ConfigManager:
  32. def __init__(self):
  33. self.config = None
  34. self.load()
  35. def __getattr__(self, name, *args, **kwargs):
  36. return getattr(self.config, name, *args, **kwargs)
  37. @property
  38. def open_service(self):
  39. return self.config.getboolean("Settings", "open_service", fallback=True)
  40. @property
  41. def open_update(self):
  42. return self.config.getboolean("Settings", "open_update", fallback=True)
  43. @property
  44. def open_use_cache(self):
  45. return self.config.getboolean("Settings", "open_use_cache", fallback=True)
  46. @property
  47. def open_request(self):
  48. return self.config.getboolean("Settings", "open_request", fallback=False)
  49. @property
  50. def open_filter_speed(self):
  51. return self.config.getboolean(
  52. "Settings", "open_filter_speed", fallback=True
  53. )
  54. @property
  55. def open_filter_resolution(self):
  56. return self.config.getboolean(
  57. "Settings", "open_filter_resolution", fallback=True
  58. )
  59. @property
  60. def ipv_type(self):
  61. return self.config.get("Settings", "ipv_type", fallback="全部").lower()
  62. @property
  63. def open_ipv6(self):
  64. return (
  65. "ipv6" in self.ipv_type or "all" in self.ipv_type or "全部" in self.ipv_type
  66. )
  67. @property
  68. def ipv_type_prefer(self):
  69. return [
  70. type.strip().lower()
  71. for type in self.config.get(
  72. "Settings", "ipv_type_prefer", fallback="auto"
  73. ).split(",")
  74. ]
  75. @property
  76. def ipv4_num(self):
  77. return self.config.getint("Settings", "ipv4_num", fallback=15)
  78. @property
  79. def ipv6_num(self):
  80. return self.config.getint("Settings", "ipv6_num", fallback=15)
  81. @property
  82. def ipv6_support(self):
  83. return self.config.getboolean("Settings", "ipv6_support", fallback=False)
  84. @property
  85. def ipv_limit(self):
  86. return {
  87. "ipv4": self.ipv4_num,
  88. "ipv6": self.ipv6_num,
  89. }
  90. @property
  91. def origin_type_prefer(self):
  92. return [
  93. origin.strip().lower()
  94. for origin in self.config.get(
  95. "Settings",
  96. "origin_type_prefer",
  97. fallback="",
  98. ).split(",")
  99. if origin.strip().lower()
  100. ]
  101. @property
  102. def hotel_num(self):
  103. return self.config.getint("Settings", "hotel_num", fallback=10)
  104. @property
  105. def multicast_num(self):
  106. return self.config.getint("Settings", "multicast_num", fallback=10)
  107. @property
  108. def subscribe_num(self):
  109. return self.config.getint("Settings", "subscribe_num", fallback=10)
  110. @property
  111. def online_search_num(self):
  112. return self.config.getint("Settings", "online_search_num", fallback=10)
  113. @property
  114. def source_limits(self):
  115. return {
  116. "hotel": self.hotel_num,
  117. "multicast": self.multicast_num,
  118. "subscribe": self.subscribe_num,
  119. "online_search": self.online_search_num,
  120. }
  121. @property
  122. def min_speed(self):
  123. return self.config.getfloat("Settings", "min_speed", fallback=0.5)
  124. @property
  125. def min_resolution(self):
  126. return self.config.get("Settings", "min_resolution", fallback="1920x1080")
  127. @property
  128. def min_resolution_value(self):
  129. return get_resolution_value(self.min_resolution)
  130. @property
  131. def urls_limit(self):
  132. return self.config.getint("Settings", "urls_limit", fallback=30)
  133. @property
  134. def open_url_info(self):
  135. return self.config.getboolean("Settings", "open_url_info", fallback=True)
  136. @property
  137. def recent_days(self):
  138. return self.config.getint("Settings", "recent_days", fallback=30)
  139. @property
  140. def source_file(self):
  141. return self.config.get("Settings", "source_file", fallback="config/demo.txt")
  142. @property
  143. def final_file(self):
  144. return self.config.get("Settings", "final_file", fallback="output/result.txt")
  145. @property
  146. def open_m3u_result(self):
  147. return self.config.getboolean("Settings", "open_m3u_result", fallback=True)
  148. @property
  149. def open_keep_all(self):
  150. return self.config.getboolean("Settings", "open_keep_all", fallback=False)
  151. @property
  152. def open_subscribe(self):
  153. return self.config.getboolean("Settings", f"open_subscribe", fallback=True)
  154. @property
  155. def open_hotel(self):
  156. return self.config.getboolean("Settings", f"open_hotel", fallback=True)
  157. @property
  158. def open_hotel_fofa(self):
  159. return self.config.getboolean("Settings", f"open_hotel_fofa", fallback=True)
  160. @property
  161. def open_hotel_foodie(self):
  162. return self.config.getboolean("Settings", f"open_hotel_foodie", fallback=True)
  163. @property
  164. def open_multicast(self):
  165. return self.config.getboolean("Settings", f"open_multicast", fallback=True)
  166. @property
  167. def open_multicast_fofa(self):
  168. return self.config.getboolean("Settings", f"open_multicast_fofa", fallback=True)
  169. @property
  170. def open_multicast_foodie(self):
  171. return self.config.getboolean(
  172. "Settings", f"open_multicast_foodie", fallback=True
  173. )
  174. @property
  175. def open_online_search(self):
  176. return self.config.getboolean("Settings", f"open_online_search", fallback=True)
  177. @property
  178. def open_method(self):
  179. return {
  180. "subscribe": self.open_subscribe,
  181. "hotel": self.open_hotel,
  182. "multicast": self.open_multicast,
  183. "online_search": self.open_online_search,
  184. "hotel_fofa": self.open_hotel_fofa,
  185. "hotel_foodie": self.open_hotel_foodie,
  186. "multicast_fofa": self.open_multicast_fofa,
  187. "multicast_foodie": self.open_multicast_foodie,
  188. }
  189. @property
  190. def open_use_old_result(self):
  191. return self.config.getboolean("Settings", "open_use_old_result", fallback=True)
  192. @property
  193. def open_sort(self):
  194. return self.config.getboolean("Settings", "open_sort", fallback=True)
  195. @property
  196. def open_update_time(self):
  197. return self.config.getboolean("Settings", "open_update_time", fallback=True)
  198. @property
  199. def multicast_region_list(self):
  200. return [
  201. region.strip()
  202. for region in self.config.get(
  203. "Settings", "multicast_region_list", fallback="全部"
  204. ).split(",")
  205. if region.strip()
  206. ]
  207. @property
  208. def hotel_region_list(self):
  209. return [
  210. region.strip()
  211. for region in self.config.get(
  212. "Settings", "hotel_region_list", fallback="全部"
  213. ).split(",")
  214. if region.strip()
  215. ]
  216. @property
  217. def request_timeout(self):
  218. return self.config.getint("Settings", "request_timeout", fallback=10)
  219. @property
  220. def sort_timeout(self):
  221. return self.config.getint("Settings", "sort_timeout", fallback=10)
  222. @property
  223. def open_proxy(self):
  224. return self.config.getboolean("Settings", "open_proxy", fallback=False)
  225. @property
  226. def open_driver(self):
  227. return not os.environ.get("LITE") and self.config.getboolean(
  228. "Settings", "open_driver", fallback=True
  229. )
  230. @property
  231. def hotel_page_num(self):
  232. return self.config.getint("Settings", "hotel_page_num", fallback=1)
  233. @property
  234. def multicast_page_num(self):
  235. return self.config.getint("Settings", "multicast_page_num", fallback=1)
  236. @property
  237. def online_search_page_num(self):
  238. return config.getint("Settings", "online_search_page_num", fallback=1)
  239. @property
  240. def open_empty_category(self):
  241. return self.config.getboolean("Settings", "open_empty_category", fallback=True)
  242. @property
  243. def app_port(self):
  244. return os.environ.get("APP_PORT") or self.config.getint("Settings", "app_port", fallback=8000)
  245. @property
  246. def open_supply(self):
  247. return self.config.getboolean("Settings", "open_supply", fallback=True)
  248. def load(self):
  249. """
  250. Load the config
  251. """
  252. self.config = configparser.ConfigParser()
  253. user_config_path = resource_path("config/user_config.ini")
  254. default_config_path = resource_path("config/config.ini")
  255. config_files = [user_config_path, default_config_path]
  256. for config_file in config_files:
  257. if os.path.exists(config_file):
  258. with open(config_file, "r", encoding="utf-8") as f:
  259. self.config.read_file(f)
  260. break
  261. def set(self, section, key, value):
  262. """
  263. Set the config
  264. """
  265. self.config.set(section, key, value)
  266. def save(self):
  267. """
  268. Save config with write
  269. """
  270. user_config_file = "config/" + (
  271. "user_config.ini"
  272. if os.path.exists(resource_path("user_config.ini"))
  273. else "config.ini"
  274. )
  275. user_config_path = resource_path(user_config_file, persistent=True)
  276. if not os.path.exists(user_config_path):
  277. os.makedirs(os.path.dirname(user_config_path), exist_ok=True)
  278. with open(user_config_path, "w", encoding="utf-8") as configfile:
  279. self.config.write(configfile)
  280. def copy(self):
  281. """
  282. Copy config files to current directory
  283. """
  284. dest_folder = os.path.join(os.getcwd(), "config")
  285. try:
  286. src_dir = resource_path("config")
  287. if os.path.exists(src_dir):
  288. if not os.path.exists(dest_folder):
  289. os.makedirs(dest_folder, exist_ok=True)
  290. for root, _, files in os.walk(src_dir):
  291. for file in files:
  292. src_file_path = os.path.join(root, file)
  293. relative_path = os.path.relpath(src_file_path, src_dir)
  294. dest_file_path = os.path.join(dest_folder, relative_path)
  295. dest_file_dir = os.path.dirname(dest_file_path)
  296. if not os.path.exists(dest_file_dir):
  297. os.makedirs(dest_file_dir, exist_ok=True)
  298. if not os.path.exists(dest_file_path):
  299. shutil.copy(src_file_path, dest_file_path)
  300. except Exception as e:
  301. print(f"Failed to copy files: {str(e)}")
  302. config = ConfigManager()