qita.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import os
  2. import re
  3. import time
  4. import datetime
  5. import threading
  6. from queue import Queue
  7. import requests
  8. import eventlet
  9. eventlet.monkey_patch()
  10. # 线程安全的队列,用于存储下载任务
  11. task_queue = Queue()
  12. # 线程安全的列表,用于存储结果
  13. results = []
  14. channels = []
  15. error_channels = []
  16. with open("itv.txt", 'r', encoding='utf-8') as file:
  17. lines = file.readlines()
  18. for line in lines:
  19. line = line.strip()
  20. if line:
  21. channel_name, channel_url = line.split(',')
  22. if '卫视' not in channel_name and 'CCTV' not in channel_name and '河南' not in channel_name and '测试' not in channel_name and '医院' not in channel_name and '院内' not in channel_name:
  23. channels.append((channel_name, channel_url))
  24. # 定义工作线程函数
  25. def worker():
  26. while True:
  27. # 从队列中获取一个任务
  28. channel_name, channel_url = task_queue.get()
  29. try:
  30. channel_url_t = channel_url.rstrip(channel_url.split('/')[-1]) # m3u8链接前缀
  31. lines = requests.get(channel_url).text.strip().split('\n') # 获取m3u8文件内容
  32. ts_lists = [line.split('/')[-1] for line in lines if line.startswith('#') == False] # 获取m3u8文件下视频流后缀
  33. ts_lists_0 = ts_lists[0].rstrip(ts_lists[0].split('.ts')[-1]) # m3u8链接前缀
  34. ts_url = channel_url_t + ts_lists[0] # 拼接单个视频片段下载链接
  35. # 多获取的视频数据进行5秒钟限制
  36. with eventlet.Timeout(5, False):
  37. start_time = time.time()
  38. content = requests.get(ts_url).content
  39. end_time = time.time()
  40. response_time = (end_time - start_time) * 1
  41. if content:
  42. with open(ts_lists_0, 'ab') as f:
  43. f.write(content) # 写入文件
  44. file_size = len(content)
  45. # print(f"文件大小:{file_size} 字节")
  46. download_speed = file_size / response_time / 1024
  47. # print(f"下载速度:{download_speed:.3f} kB/s")
  48. normalized_speed = min(max(download_speed / 1024, 0.001), 100) # 将速率从kB/s转换为MB/s并限制在1~100之间
  49. #print(f"标准化后的速率:{normalized_speed:.3f} MB/s")
  50. # 删除下载的文件
  51. os.remove(ts_lists_0)
  52. result = channel_name, channel_url, f"{normalized_speed:.3f} MB/s"
  53. results.append(result)
  54. numberx = (len(results) + len(error_channels)) / len(channels) * 100
  55. print(f"可用频道:{len(results)} 个 , 不可用频道:{len(error_channels)} 个 , 总频道:{len(channels)} 个 ,总进度:{numberx:.2f} %。")
  56. except:
  57. error_channel = channel_name, channel_url
  58. error_channels.append(error_channel)
  59. numberx = (len(results) + len(error_channels)) / len(channels) * 100
  60. print(f"可用频道:{len(results)} 个 , 不可用频道:{len(error_channels)} 个 , 总频道:{len(channels)} 个 ,总进度:{numberx:.2f} %。")
  61. # 标记任务完成
  62. task_queue.task_done()
  63. # 创建多个工作线程
  64. num_threads = 10
  65. for _ in range(num_threads):
  66. t = threading.Thread(target=worker, daemon=True)
  67. #t = threading.Thread(target=worker, args=(event,len(channels))) # 将工作线程设置为守护线程
  68. t.start()
  69. #event.set()
  70. # 添加下载任务到队列
  71. for channel in channels:
  72. task_queue.put(channel)
  73. # 等待所有任务完成
  74. task_queue.join()
  75. def channel_key(channel_name):
  76. match = re.search(r'\d+', channel_name)
  77. if match:
  78. return int(match.group())
  79. else:
  80. return float('inf') # 返回一个无穷大的数字作为关键字
  81. # 对频道进行排序
  82. results.sort(key=lambda x: (x[0], -float(x[2].split()[0])))
  83. #results.sort(key=lambda x: channel_key(x[0]))
  84. now_today = datetime.date.today()
  85. # 将结果写入文件
  86. result_counter = 1 # 每个频道需要的个数
  87. with open("qita.txt", 'w', encoding='utf-8') as file:
  88. channel_counters = {}
  89. file.write('其他频道,#genre#\n')
  90. for result in results:
  91. channel_name, channel_url, speed = result
  92. if 'CCTV' not in channel_name and '卫视' not in channel_name and '河南' not in channel_name and '许昌' not in channel_name and '测试' not in channel_name:
  93. if channel_name in channel_counters:
  94. if channel_counters[channel_name] >= result_counter:
  95. continue
  96. else:
  97. file.write(f"{channel_name},{channel_url}\n")
  98. channel_counters[channel_name] += 1
  99. else:
  100. file.write(f"{channel_name},{channel_url}\n")
  101. channel_counters[channel_name] = 1
  102. file.write(f"{now_today}更新,#genre#\n")
  103. with open("qita.m3u", 'w', encoding='utf-8') as file:
  104. channel_counters = {}
  105. #file.write('其他频道,#genre#\n')
  106. for result in results:
  107. channel_name, channel_url, speed = result
  108. if 'CCTV' not in channel_name and '卫视' not in channel_name and '河南' not in channel_name and '测试' not in channel_name:
  109. if channel_name in channel_counters:
  110. if channel_counters[channel_name] >= result_counter:
  111. continue
  112. else:
  113. file.write(f'#EXTINF:-1 tvg-id="{channel_name}" tvg-logo="https://epg.112114.xyz/logo/{channel_name}.png" group-title=\"其他频道\",{channel_name}\n')
  114. file.write(f"{channel_url}\n")
  115. channel_counters[channel_name] += 1
  116. else:
  117. file.write(f'#EXTINF:-1 tvg-id="{channel_name}" tvg-logo="https://epg.112114.xyz/logo/{channel_name}.png" group-title=\"其他频道\",{channel_name}\n')
  118. file.write(f"{channel_url}\n")
  119. channel_counters[channel_name] = 1
  120. file.write(f"#EXTINF:-1 group-title=\"{now_today}更新\"\n")
  121. # 合并文件内容
  122. file_contents = []
  123. file_paths = ["cctv.txt", "weishi.txt", "hn.txt","qita.txt"] # 替换为实际的文件路径列表
  124. for file_path in file_paths:
  125. with open(file_path, 'r', encoding="utf-8") as file:
  126. content = file.read()
  127. file_contents.append(content)
  128. # 写入合并后的文件
  129. with open("itvlist.txt", "w", encoding="utf-8") as output:
  130. output.write('\n'.join(file_contents))
  131. # 合并文件内容
  132. file_contents = []
  133. file_paths = ["cctv.m3u", "weishi.m3u", "hn.m3u","qita.m3u"] # 替换为实际的文件路径列表
  134. for file_path in file_paths:
  135. with open(file_path, 'r', encoding="utf-8") as file:
  136. content = file.read()
  137. file_contents.append(content)
  138. # 写入合并后的文件
  139. with open("itvlist.m3u", "w", encoding="utf-8") as output:
  140. output.write('\n'.join(file_contents))