main.py 8.5 KB

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