main.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import asyncio
  2. import copy
  3. import pickle
  4. from time import time
  5. from tqdm import tqdm
  6. import utils.constants as constants
  7. from service.app import run_service
  8. from updates.fofa import get_channels_by_fofa
  9. from updates.hotel import get_channels_by_hotel
  10. from updates.multicast import get_channels_by_multicast
  11. from updates.online_search import get_channels_by_online_search
  12. from updates.subscribe import get_channels_by_subscribe_urls
  13. from utils.channel import (
  14. get_channel_items,
  15. append_total_data,
  16. process_sort_channel_list,
  17. write_channel_to_file,
  18. get_channel_data_cache_with_compare,
  19. format_channel_url_info,
  20. )
  21. from utils.config import config
  22. from utils.tools import (
  23. update_file,
  24. get_pbar_remaining,
  25. get_ip_address,
  26. convert_to_m3u,
  27. process_nested_dict,
  28. format_interval,
  29. check_ipv6_support,
  30. resource_path,
  31. get_urls_from_file,
  32. get_version_info
  33. )
  34. from utils.types import CategoryChannelData
  35. class UpdateSource:
  36. def __init__(self):
  37. self.update_progress = None
  38. self.run_ui = False
  39. self.tasks = []
  40. self.channel_items: CategoryChannelData = {}
  41. self.hotel_fofa_result = {}
  42. self.hotel_foodie_result = {}
  43. self.multicast_result = {}
  44. self.subscribe_result = {}
  45. self.online_search_result = {}
  46. self.channel_data: CategoryChannelData = {}
  47. self.pbar = None
  48. self.total = 0
  49. self.start_time = None
  50. async def visit_page(self, channel_names: list[str] = None):
  51. tasks_config = [
  52. ("hotel_fofa", get_channels_by_fofa, "hotel_fofa_result"),
  53. ("multicast", get_channels_by_multicast, "multicast_result"),
  54. ("hotel_foodie", get_channels_by_hotel, "hotel_foodie_result"),
  55. ("subscribe", get_channels_by_subscribe_urls, "subscribe_result"),
  56. (
  57. "online_search",
  58. get_channels_by_online_search,
  59. "online_search_result",
  60. ),
  61. ]
  62. for setting, task_func, result_attr in tasks_config:
  63. if (
  64. setting == "hotel_foodie" or setting == "hotel_fofa"
  65. ) and config.open_hotel == False:
  66. continue
  67. if config.open_method[setting]:
  68. if setting == "subscribe":
  69. subscribe_urls = get_urls_from_file(constants.subscribe_path)
  70. whitelist_urls = get_urls_from_file(constants.whitelist_path)
  71. task = asyncio.create_task(
  72. task_func(subscribe_urls, whitelist=whitelist_urls, callback=self.update_progress)
  73. )
  74. elif setting == "hotel_foodie" or setting == "hotel_fofa":
  75. task = asyncio.create_task(task_func(callback=self.update_progress))
  76. else:
  77. task = asyncio.create_task(
  78. task_func(channel_names, callback=self.update_progress)
  79. )
  80. self.tasks.append(task)
  81. setattr(self, result_attr, await task)
  82. def pbar_update(self, name: str = ""):
  83. if self.pbar.n < self.total:
  84. self.pbar.update()
  85. self.update_progress(
  86. f"正在进行{name}, 剩余{self.total - self.pbar.n}个接口, 预计剩余时间: {get_pbar_remaining(n=self.pbar.n, total=self.total, start_time=self.start_time)}",
  87. int((self.pbar.n / self.total) * 100),
  88. )
  89. def get_urls_len(self, is_filter: bool = False) -> int:
  90. data = copy.deepcopy(self.channel_data)
  91. if is_filter:
  92. process_nested_dict(data, seen={}, flag=r"cache:(.*)", force_str="!")
  93. processed_urls = set(
  94. url_info["url"]
  95. for channel_obj in data.values()
  96. for url_info_list in channel_obj.values()
  97. for url_info in url_info_list
  98. )
  99. return len(processed_urls)
  100. async def main(self):
  101. try:
  102. user_final_file = config.final_file
  103. main_start_time = time()
  104. if config.open_update:
  105. self.channel_items = get_channel_items()
  106. channel_names = [
  107. name
  108. for channel_obj in self.channel_items.values()
  109. for name in channel_obj.keys()
  110. ]
  111. if not channel_names:
  112. print(f"❌ No channel names found! Please check the {config.source_file}!")
  113. return
  114. await self.visit_page(channel_names)
  115. self.tasks = []
  116. append_total_data(
  117. self.channel_items.items(),
  118. channel_names,
  119. self.channel_data,
  120. self.hotel_fofa_result,
  121. self.multicast_result,
  122. self.hotel_foodie_result,
  123. self.subscribe_result,
  124. self.online_search_result,
  125. )
  126. channel_data_cache = copy.deepcopy(self.channel_data)
  127. ipv6_support = config.ipv6_support or check_ipv6_support()
  128. open_sort = config.open_sort
  129. if open_sort:
  130. urls_total = self.get_urls_len()
  131. self.total = self.get_urls_len(is_filter=True)
  132. print(f"Total urls: {urls_total}, need to sort: {self.total}")
  133. sort_callback = lambda: self.pbar_update(name="测速")
  134. self.update_progress(
  135. f"正在测速排序, 共{urls_total}个接口, {self.total}个接口需要进行测速",
  136. 0,
  137. )
  138. self.start_time = time()
  139. self.pbar = tqdm(total=self.total, desc="Sorting")
  140. self.channel_data = await process_sort_channel_list(
  141. self.channel_data,
  142. ipv6=ipv6_support,
  143. callback=sort_callback,
  144. )
  145. else:
  146. format_channel_url_info(self.channel_data)
  147. self.total = self.get_urls_len()
  148. self.pbar = tqdm(total=self.total, desc="Writing")
  149. self.start_time = time()
  150. write_channel_to_file(
  151. self.channel_data,
  152. ipv6=ipv6_support,
  153. callback=lambda: self.pbar_update(name="写入结果"),
  154. )
  155. self.pbar.close()
  156. update_file(user_final_file, constants.result_path)
  157. if config.open_history:
  158. if open_sort:
  159. get_channel_data_cache_with_compare(
  160. channel_data_cache, self.channel_data
  161. )
  162. with open(
  163. resource_path(constants.cache_path, persistent=True),
  164. "wb",
  165. ) as file:
  166. pickle.dump(channel_data_cache, file)
  167. convert_to_m3u(channel_names[0])
  168. print(
  169. f"🥳 Update completed! Total time spent: {format_interval(time() - main_start_time)}. Please check the {user_final_file} file!"
  170. )
  171. if self.run_ui:
  172. open_service = config.open_service
  173. service_tip = ", 可使用以下链接观看直播:" if open_service else ""
  174. tip = (
  175. f"✅ 服务启动成功{service_tip}"
  176. if open_service and config.open_update == False
  177. else f"🥳 更新完成, 耗时: {format_interval(time() - main_start_time)}, 请检查{user_final_file}文件{service_tip}"
  178. )
  179. self.update_progress(
  180. tip,
  181. 100,
  182. True,
  183. url=f"{get_ip_address()}" if open_service else None,
  184. )
  185. if open_service:
  186. run_service()
  187. except asyncio.exceptions.CancelledError:
  188. print("Update cancelled!")
  189. async def start(self, callback=None):
  190. def default_callback(self, *args, **kwargs):
  191. pass
  192. self.update_progress = callback or default_callback
  193. self.run_ui = True if callback else False
  194. await self.main()
  195. def stop(self):
  196. for task in self.tasks:
  197. task.cancel()
  198. self.tasks = []
  199. if self.pbar:
  200. self.pbar.close()
  201. if __name__ == "__main__":
  202. info = get_version_info()
  203. print(f"ℹ️ {info['name']} Version: {info['version']}")
  204. loop = asyncio.new_event_loop()
  205. asyncio.set_event_loop(loop)
  206. update_source = UpdateSource()
  207. loop.run_until_complete(update_source.start())