main.py 8.2 KB

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