Update ip_check.py

This commit is contained in:
frxz751113
2024-08-09 10:35:50 +08:00
committed by GitHub
parent 16406a5e65
commit a72ebe3337
+107 -117
View File
@@ -10,133 +10,69 @@ eventlet.monkey_patch()
# 线程安全的队列,用于存储下载任务 # 线程安全的队列,用于存储下载任务
task_queue = Queue() task_queue = Queue()
lock = threading.Lock()
# 线程安全的列表,用于存储结果 # 线程安全的列表,用于存储结果
results = [] results = []
channels = [] channels = []
error_channels = [] error_channels = []
headers={'User-Agent': 'okhttp/3.12.10(Linux;Android9;V2049ABuild/TP1A.220624.014;wv)AppleWebKit/537.36(KHTML,likeGecko)Version/4.0Chrome/116.0.0.0MobileSafari/537.36'}
se=requests.Session()
with open("综合源.txt", 'r', encoding='utf-8') as file: with open("综合源.txt", 'r', encoding='utf-8') as file:
lines = file.readlines() lines = file.readlines()
for line in lines: for line in lines:
line = line.strip() line = line.strip()
count = line.count(',') if line:
if count == 1: channel_name, channel_url = line.split(',')
if line: if '卫视' in channel_name or 'CCTV' in channel_name or '' in channel_name or '' in channel_name or '' in channel_name:
channel_name, channel_url = line.split(',') channels.append((channel_name, channel_url))
if 'http' in channel_url:
channels.append((channel_name, channel_url))
file.close()
channels = set(channels)
# 定义工作线程函数 # 定义工作线程函数
def worker(): def worker():
while True: while True:
# 从队列中获取一个任务 # 从队列中获取一个任务
channel_name, channel_url = task_queue.get() channel_name, channel_url = task_queue.get()
if ".m3u8" in channel_url or ".flv" in channel_url or ".mp4" in channel_url: try:
try: channel_url_t = channel_url.rstrip(channel_url.split('/')[-1]) # m3u8链接前缀
channel_url_t = channel_url.rstrip(channel_url.split('/')[-1]) # m3u8链接前缀 lines = requests.get(channel_url).text.strip().split('\n') # 获取m3u8文件内容
lines = requests.get(channel_url,headers=headers, timeout=3, stream=True).text.strip().split('\n') # 获取m3u8文件内容 ts_lists = [line.split('/')[-1] for line in lines if line.startswith('#') == False] # 获取m3u8文件下视频流后缀
ts_lists = [line.split('/')[-1] for line in lines if line.startswith('#') == False] # 获取m3u8文件下视频流后 ts_lists_0 = ts_lists[0].rstrip(ts_lists[0].split('.ts')[-1]) # m3u8链接前
ts_lists_0 = ts_lists[0].rstrip(ts_lists[0].split('.ts')[-1]) # m3u8链接前缀 ts_url = channel_url_t + ts_lists[0] # 拼接单个视频片段下载链接
ts_url = channel_url_t + ts_lists[0] # 拼接单个视频片段下载链接
# 多获取的视频数据进行5秒钟限制
# 多获取的视频数据进行5秒钟限制 with eventlet.Timeout(5, False):
with eventlet.Timeout(5, False): start_time = time.time()
start_time = time.time() content = requests.get(ts_url).content
content = requests.get(ts_url,headers=headers, timeout=(1,4), stream=True).content end_time = time.time()
end_time = time.time() response_time = (end_time - start_time) * 1
response_time = (end_time - start_time) * 1
if content: if content:
with open(ts_lists_0, 'ab') as f: with open(ts_lists_0, 'ab') as f:
f.write(content) # 写入文件 f.write(content) # 写入文件
file_size = len(content) file_size = len(content)
# print(f"文件大小:{file_size} 字节") # print(f"文件大小:{file_size} 字节")
download_speed = file_size / response_time / 1024 download_speed = file_size / response_time / 1024
# print(f"下载速度:{download_speed:.3f} kB/s") # print(f"下载速度:{download_speed:.3f} kB/s")
normalized_speed = min(max(download_speed / 1024, 0.001), 100) # 将速率从kB/s转换为MB/s并限制在1~100之间 normalized_speed = min(max(download_speed / 1024, 0.001), 100) # 将速率从kB/s转换为MB/s并限制在1~100之间
#print(f'{channel_url}') #print(f"标准化后的速率:{normalized_speed:.3f} MB/s")
print(f"m3u8 标准化后的速率:{normalized_speed:.3f} MB/s {channel_url}")
# 删除下载的文件
# 删除下载的文件 os.remove(ts_lists_0)
os.remove(ts_lists_0) result = channel_name, channel_url, f"{normalized_speed:.3f} MB/s"
result = channel_name, channel_url, f"{normalized_speed:.3f} MB/s" results.append(result)
# 获取锁
lock.acquire()
if normalized_speed > 0.001:
results.append(result)
else:
error_channel = channel_name, channel_url
error_channels.append(error_channel)
# 释放锁
lock.release()
numberx = (len(results) + len(error_channels)) / len(channels) * 100
# print(f"可用频道:{len(results)} 个 , 不可用频道:{len(error_channels)} 个 , 总频道:{len(channels)} 个 ,总进度:{numberx:.2f} %。")
except:
error_channel = channel_name, channel_url
# 获取锁
lock.acquire()
error_channels.append(error_channel)
# 释放锁
lock.release()
numberx = (len(results) + len(error_channels)) / len(channels) * 100 numberx = (len(results) + len(error_channels)) / len(channels) * 100
else: print(f"可用频道:{len(results)} 个 , 不可用频道:{len(error_channels)} 个 , 总频道:{len(channels)} 个 ,总进度:{numberx:.2f} %。")
try: except:
now=time.time() error_channel = channel_name, channel_url
res=se.get(channel_url,headers=headers,timeout=5,stream=True) error_channels.append(error_channel)
if res.status_code==200: numberx = (len(results) + len(error_channels)) / len(channels) * 100
for k in res.iter_content(chunk_size=2097152): print(f"可用频道:{len(results)} 个 , 不可用频道:{len(error_channels)} 个 , 总频道:{len(channels)} 个 ,总进度:{numberx:.2f} %。")
# 这里的chunk_size是1MB,每次读取1MB测试视频流
# 如果能获取视频流,则输出读取的时间以及链接
if time.time()-now > 60:
res.close()
print(f'Time out\t{channel_url}')
error_channel = channel_name, channel_url
# 获取锁
lock.acquire()
error_channels.append(error_channel)
# 释放锁
lock.release()
break
else:
if k: # 检查是否有内容
print(f'{time.time()-now:.2f}\t{channel_url}')
response_time = (time.time()-now) * 1
download_speed = 2097152 / response_time / 1024
normalized_speed = min(max(download_speed / 1024, 0.001), 100)
if response_time > 1:
result = channel_name, channel_url, f"{normalized_speed:.3f} MB/s"
# 获取锁
lock.acquire()
results.append(result)
# 释放锁
lock.release()
else:
print(f'X\t{channel_url}')
break
except:
error_channel = channel_name, channel_url
# 获取锁
lock.acquire()
error_channels.append(error_channel)
# 释放锁
lock.release()
# 无法连接并超时的情况下输出“X”
print(f'X\t{channel_url}')
# 减少CPU占用
time.sleep(0)
# 标记任务完成 # 标记任务完成
task_queue.task_done() task_queue.task_done()
# 创建多个工作线程 # 创建多个工作线程
num_threads = 40 num_threads = 32
for _ in range(num_threads): for _ in range(num_threads):
t = threading.Thread(target=worker, daemon=True) t = threading.Thread(target=worker, daemon=True)
#t = threading.Thread(target=worker, args=(event,len(channels))) # 将工作线程设置为守护线程 #t = threading.Thread(target=worker, args=(event,len(channels))) # 将工作线程设置为守护线程
@@ -150,18 +86,72 @@ for channel in channels:
# 等待所有任务完成 # 等待所有任务完成
task_queue.join() task_queue.join()
now_today = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
def channel_key(channel_name):
match = re.search(r'\d+', channel_name)
if match:
return int(match.group())
else:
return float('inf') # 返回一个无穷大的数字作为关键字
# 对频道进行排序
results.sort(key=lambda x: (x[0], -float(x[2].split()[0])))
results.sort(key=lambda x: channel_key(x[0]))
now_today = datetime.date.today()
# 将结果写入文件 # 将结果写入文件
with open("综合源.txt", 'w', encoding='utf-8') as file: with open("itv_results.txt", 'w', encoding='utf-8') as file:
file.write('失效连接,#genre#\n')
for result in error_channels:
channel_name, channel_url = result
file.write(f"{channel_name},{channel_url}\n")
file.write('有效连接,#genre#\n')
for result in results: for result in results:
channel_name, channel_url, speed = result channel_name, channel_url, speed = result
file.write(f"{channel_name},{channel_url}\n") file.write(f"{channel_name},{channel_url},{speed}\n")
file.write(f"测试完成时间,{now_today}\n")
file.close()
result_counter = 8 # 每个频道需要的个数
with open("itvlist.txt", 'w', encoding='utf-8') as file:
channel_counters = {}
file.write('央视频道,#genre#\n')
for result in results:
channel_name, channel_url, speed = result
if 'CCTV' in channel_name:
if channel_name in channel_counters:
if channel_counters[channel_name] >= result_counter:
continue
else:
file.write(f"{channel_name},{channel_url}\n")
channel_counters[channel_name] += 1
else:
file.write(f"{channel_name},{channel_url}\n")
channel_counters[channel_name] = 1
channel_counters = {}
file.write('卫视频道,#genre#\n')
for result in results:
channel_name, channel_url, speed = result
if '卫视' in channel_name:
if channel_name in channel_counters:
if channel_counters[channel_name] >= result_counter:
continue
else:
file.write(f"{channel_name},{channel_url}\n")
channel_counters[channel_name] += 1
else:
file.write(f"{channel_name},{channel_url}\n")
channel_counters[channel_name] = 1
channel_counters = {}
file.write('其他频道,#genre#\n')
for result in results:
channel_name, channel_url, speed = result
if 'CCTV' not in channel_name and '卫视' not in channel_name and '测试' not in channel_name:
if channel_name in channel_counters:
if channel_counters[channel_name] >= result_counter:
continue
else:
file.write(f"{channel_name},{channel_url}\n")
channel_counters[channel_name] += 1
else:
file.write(f"{channel_name},{channel_url}\n")
channel_counters[channel_name] = 1
file.write(f"{now_today}更新,#genre#\n")