itvtest.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 '卫视' in channel_name or 'CCTV' in channel_name or '农民' in channel_name or '戏曲' in channel_name or '梨园' 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,timeout=1).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,timeout=1).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.start()
  68. # 添加下载任务到队列
  69. for channel in channels:
  70. task_queue.put(channel)
  71. # 等待所有任务完成
  72. task_queue.join()
  73. def channel_key(channel_name):
  74. match = re.search(r'\d+', channel_name)
  75. if match:
  76. return int(match.group())
  77. else:
  78. return float('inf') # 返回一个无穷大的数字作为关键字
  79. # 对频道进行排序
  80. results.sort(key=lambda x: (x[0], -float(x[2].split()[0])))
  81. results.sort(key=lambda x: channel_key(x[0]))
  82. now_today = datetime.date.today()
  83. # 将结果写入文件
  84. with open("itv_results.txt", 'w', encoding='utf-8') as file:
  85. for result in results:
  86. channel_name, channel_url, speed = result
  87. file.write(f"{channel_name},{channel_url},{speed}\n")
  88. with open("itv_speed.txt", 'w', encoding='utf-8') as file:
  89. for result in results:
  90. channel_name, channel_url, speed = result
  91. file.write(f"{channel_name},{channel_url}\n")
  92. result_counter = 1 # 每个频道需要的个数
  93. with open("itvlist.txt", 'w', encoding='utf-8') as file:
  94. channel_counters = {}
  95. file.write('央视频道,#genre#\n')
  96. for result in results:
  97. channel_name, channel_url, speed = result
  98. if 'CCTV' in channel_name:
  99. if channel_name in channel_counters:
  100. if channel_counters[channel_name] >= result_counter:
  101. continue
  102. else:
  103. file.write(f"{channel_name},{channel_url}\n")
  104. channel_counters[channel_name] += 1
  105. else:
  106. file.write(f"{channel_name},{channel_url}\n")
  107. channel_counters[channel_name] = 1
  108. channel_counters = {}
  109. file.write('卫视频道,#genre#\n')
  110. for result in results:
  111. channel_name, channel_url, speed = result
  112. if '卫视' in channel_name:
  113. if channel_name in channel_counters:
  114. if channel_counters[channel_name] >= result_counter:
  115. continue
  116. else:
  117. file.write(f"{channel_name},{channel_url}\n")
  118. channel_counters[channel_name] += 1
  119. else:
  120. file.write(f"{channel_name},{channel_url}\n")
  121. channel_counters[channel_name] = 1
  122. channel_counters = {}
  123. file.write('许昌频道,#genre#\n')
  124. for result in results:
  125. channel_name, channel_url, speed = result
  126. if '许昌' in channel_name:
  127. if channel_name in channel_counters:
  128. if channel_counters[channel_name] >= result_counter:
  129. continue
  130. else:
  131. file.write(f"{channel_name},{channel_url}\n")
  132. channel_counters[channel_name] += 1
  133. else:
  134. file.write(f"{channel_name},{channel_url}\n")
  135. channel_counters[channel_name] = 1
  136. channel_counters = {}
  137. file.write('其他频道,#genre#\n')
  138. for result in results:
  139. channel_name, channel_url, speed = result
  140. if 'CCTV' not in channel_name and '卫视' not in channel_name and '许昌' not in channel_name and '测试' not in channel_name:
  141. if channel_name in channel_counters:
  142. if channel_counters[channel_name] >= result_counter:
  143. continue
  144. else:
  145. file.write(f"{channel_name},{channel_url}\n")
  146. channel_counters[channel_name] += 1
  147. else:
  148. file.write(f"{channel_name},{channel_url}\n")
  149. channel_counters[channel_name] = 1
  150. file.write(f"{now_today}更新,#genre#\n")
  151. with open("itvlist.m3u", 'w', encoding='utf-8') as file:
  152. channel_counters = {}
  153. file.write('#EXTM3U\n')
  154. for result in results:
  155. channel_name, channel_url, speed = result
  156. if 'CCTV' in channel_name:
  157. if channel_name in channel_counters:
  158. if channel_counters[channel_name] >= result_counter:
  159. continue
  160. else:
  161. file.write(f'#EXTINF:-1 tvg-id="{channel_name}" tvg-logo="https://epg.112114.xyz/logo/{channel_name}.png" group-title=\"央视频道\",{channel_name}\n')
  162. file.write(f"{channel_url}\n")
  163. channel_counters[channel_name] += 1
  164. else:
  165. file.write(f'#EXTINF:-1 tvg-id="{channel_name}" tvg-logo="https://epg.112114.xyz/logo/{channel_name}.png" group-title=\"央视频道\",{channel_name}\n')
  166. file.write(f"{channel_url}\n")
  167. channel_counters[channel_name] = 1
  168. channel_counters = {}
  169. #file.write('河南频道,#genre#\n')
  170. for result in results:
  171. channel_name, channel_url, speed = result
  172. if '河南' in channel_name:
  173. if channel_name in channel_counters:
  174. if channel_counters[channel_name] >= result_counter:
  175. continue
  176. else:
  177. file.write(f'#EXTINF:-1 tvg-id="{channel_name}" tvg-logo="https://epg.112114.xyz/logo/{channel_name}.png" group-title=\"河南频道\",{channel_name}\n')
  178. file.write(f"{channel_url}\n")
  179. channel_counters[channel_name] += 1
  180. else:
  181. file.write(f'#EXTINF:-1 tvg-id="{channel_name}" tvg-logo="https://epg.112114.xyz/logo/{channel_name}.png" group-title=\"河南频道\",{channel_name}\n')
  182. file.write(f"{channel_url}\n")
  183. channel_counters[channel_name] = 1
  184. channel_counters = {}
  185. #file.write('卫视频道,#genre#\n')
  186. for result in results:
  187. channel_name, channel_url, speed = result
  188. if '卫视' in channel_name:
  189. if channel_name in channel_counters:
  190. if channel_counters[channel_name] >= result_counter:
  191. continue
  192. else:
  193. file.write(f'#EXTINF:-1 tvg-id="{channel_name}" tvg-logo="https://epg.112114.xyz/logo/{channel_name}.png" group-title=\"卫视频道\",{channel_name}\n')
  194. file.write(f"{channel_url}\n")
  195. channel_counters[channel_name] += 1
  196. else:
  197. file.write(f'#EXTINF:-1 tvg-id="{channel_name}" tvg-logo="https://epg.112114.xyz/logo/{channel_name}.png" group-title=\"卫视频道\",{channel_name}\n')
  198. file.write(f"{channel_url}\n")
  199. channel_counters[channel_name] = 1
  200. channel_counters = {}
  201. #file.write('许昌频道,#genre#\n')
  202. for result in results:
  203. channel_name, channel_url, speed = result
  204. if '许昌' in channel_name:
  205. if channel_name in channel_counters:
  206. if channel_counters[channel_name] >= result_counter:
  207. continue
  208. else:
  209. file.write(f'#EXTINF:-1 tvg-id="{channel_name}" tvg-logo="https://epg.112114.xyz/logo/{channel_name}.png" group-title=\"许昌频道\",{channel_name}\n')
  210. file.write(f"{channel_url}\n")
  211. channel_counters[channel_name] += 1
  212. else:
  213. file.write(f'#EXTINF:-1 tvg-id="{channel_name}" tvg-logo="https://epg.112114.xyz/logo/{channel_name}.png" group-title=\"许昌频道\",{channel_name}\n')
  214. file.write(f"{channel_url}\n")
  215. channel_counters[channel_name] = 1
  216. channel_counters = {}
  217. #file.write('香港频道,#genre#\n')
  218. for result in results:
  219. channel_name, channel_url, speed = result
  220. if '凤凰' in channel_name:
  221. if channel_name in channel_counters:
  222. if channel_counters[channel_name] >= result_counter:
  223. continue
  224. else:
  225. file.write(f'#EXTINF:-1 tvg-id="{channel_name}" tvg-logo="https://epg.112114.xyz/logo/{channel_name}.png" group-title=\"香港频道\",{channel_name}\n')
  226. file.write(f"{channel_url}\n")
  227. channel_counters[channel_name] += 1
  228. else:
  229. file.write(f'#EXTINF:-1 tvg-id="{channel_name}" tvg-logo="https://epg.112114.xyz/logo/{channel_name}.png" group-title=\"香港频道\",{channel_name}\n')
  230. file.write(f"{channel_url}\n")
  231. channel_counters[channel_name] = 1
  232. channel_counters = {}
  233. #file.write('其他频道,#genre#\n')
  234. for result in results:
  235. channel_name, channel_url, speed = result
  236. 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 and '测试' not in channel_name:
  237. if channel_name in channel_counters:
  238. if channel_counters[channel_name] >= result_counter:
  239. continue
  240. else:
  241. file.write(f'#EXTINF:-1 tvg-id="{channel_name}" tvg-logo="https://epg.112114.xyz/logo/{channel_name}.png" group-title=\"其他频道\",{channel_name}\n')
  242. file.write(f"{channel_url}\n")
  243. channel_counters[channel_name] += 1
  244. else:
  245. file.write(f'#EXTINF:-1 tvg-id="{channel_name}" tvg-logo="https://epg.112114.xyz/logo/{channel_name}.png" group-title=\"其他频道\",{channel_name}\n')
  246. file.write(f"{channel_url}\n")
  247. channel_counters[channel_name] = 1
  248. file.write(f"#EXTINF:-1 group-title=\"{now_today}更新\"\n")