update_tmp.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import os
  2. import sys
  3. sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
  4. from updates.subscribe import get_channels_by_subscribe_urls
  5. from utils.driver.tools import get_soup_driver
  6. from utils.config import config
  7. import utils.constants as constants
  8. from utils.channel import format_channel_name
  9. from utils.tools import get_pbar_remaining, resource_path, get_name_url
  10. import json
  11. # import asyncio
  12. from requests import Session
  13. from collections import defaultdict
  14. from time import time
  15. from tqdm import tqdm
  16. def get_region_urls_from_IPTV_Multicast_source():
  17. """
  18. Get the region urls from IPTV_Multicast_source
  19. """
  20. region_url = {}
  21. origin_url = "https://github.com/xisohi/IPTV-Multicast-source/blob/main/README.md"
  22. soup = get_soup_driver(origin_url)
  23. tbody = soup.find("tbody")
  24. trs = tbody.find_all("tr") if tbody else []
  25. for tr in trs:
  26. tds = tr.find_all("td")
  27. name = tds[0].get_text().strip()
  28. unicom = tds[1].find("a", href=True).get("href")
  29. mobile = tds[2].find("a", href=True).get("href")
  30. telecom = tds[3].find("a", href=True).get("href")
  31. if name not in region_url:
  32. region_url[name] = {}
  33. region_url[name]["联通"] = unicom
  34. region_url[name]["移动"] = mobile
  35. region_url[name]["电信"] = telecom
  36. with open(
  37. resource_path("updates/multicast/multicast_map.json"), "w", encoding="utf-8"
  38. ) as f:
  39. json.dump(region_url, f, ensure_ascii=False, indent=4)
  40. def get_multicast_urls_info_from_region_list():
  41. """
  42. Get the multicast urls info from region
  43. """
  44. urls_info = []
  45. with open(
  46. resource_path("updates/multicast/multicast_map.json"), "r", encoding="utf-8"
  47. ) as f:
  48. region_url = json.load(f)
  49. urls_info = [
  50. {"region": region, "type": type, "url": url}
  51. for region, value in region_url.items()
  52. for type, url in value.items()
  53. ]
  54. return urls_info
  55. async def get_multicast_region_result():
  56. """
  57. Get multicast region result
  58. """
  59. multicast_region_urls_info = get_multicast_urls_info_from_region_list()
  60. multicast_result = await get_channels_by_subscribe_urls(
  61. multicast_region_urls_info, multicast=True
  62. )
  63. with open(
  64. resource_path("updates/multicast/multicast_region_result.json"),
  65. "w",
  66. encoding="utf-8",
  67. ) as f:
  68. json.dump(multicast_result, f, ensure_ascii=False, indent=4)
  69. def get_multicast_region_type_result_txt():
  70. """
  71. Get multicast region type result txt
  72. """
  73. with open(
  74. resource_path("updates/multicast/multicast_map.json"), "r", encoding="utf-8"
  75. ) as f:
  76. region_url = json.load(f)
  77. session = Session()
  78. for region, value in region_url.items():
  79. for type, url in value.items():
  80. response = session.get(url)
  81. content = response.text
  82. with open(
  83. resource_path(f"config/rtp/{region}_{type}.txt"),
  84. "w",
  85. encoding="utf-8",
  86. ) as f:
  87. f.write(content)
  88. def get_multicast_region_result_by_rtp_txt(callback=None):
  89. """
  90. Get multicast region result by rtp txt
  91. """
  92. rtp_path = resource_path("config/rtp")
  93. config_region_list = set(config.multicast_region_list)
  94. rtp_file_list = [
  95. filename.rsplit(".", 1)[0]
  96. for filename in os.listdir(rtp_path)
  97. if filename.endswith(".txt")
  98. and "_" in filename
  99. and (
  100. filename.rsplit(".", 1)[0].partition("_")[0] in config_region_list
  101. or config_region_list & {"all", "ALL", "全部"}
  102. )
  103. ]
  104. total_files = len(rtp_file_list)
  105. if callback:
  106. callback(f"正在读取本地组播数据, 共{total_files}个文件", 0)
  107. pbar = tqdm(total=total_files, desc="Loading local multicast rtp files")
  108. multicast_result = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
  109. start_time = time()
  110. for filename in rtp_file_list:
  111. region, _, type = filename.partition("_")
  112. with open(
  113. os.path.join(rtp_path, f"{filename}.txt"), "r", encoding="utf-8"
  114. ) as f:
  115. for line in f:
  116. name_url = get_name_url(line, pattern=constants.rtp_pattern)
  117. if name_url and name_url[0]:
  118. channel_name = format_channel_name(name_url[0]["name"])
  119. url = name_url[0]["url"]
  120. if url not in multicast_result[channel_name][region][type]:
  121. multicast_result[channel_name][region][type].append(url)
  122. pbar.update()
  123. if callback:
  124. remaining_files = total_files - pbar.n
  125. estimated_time = get_pbar_remaining(pbar.n, total_files, start_time)
  126. callback(
  127. f"正在读取{region}_{type}的组播数据, 剩余{remaining_files}个文件, 预计剩余时间: {estimated_time}",
  128. int((pbar.n / total_files) * 100),
  129. )
  130. with open(
  131. resource_path("updates/multicast/multicast_region_result.json"),
  132. "w",
  133. encoding="utf-8",
  134. ) as f:
  135. json.dump(multicast_result, f, ensure_ascii=False, indent=4)
  136. pbar.close()
  137. return multicast_result
  138. if __name__ == "__main__":
  139. get_region_urls_from_IPTV_Multicast_source()
  140. # asyncio.run(get_multicast_region_result())
  141. get_multicast_region_type_result_txt()
  142. # get_multicast_region_result_by_rtp_txt()