Skip to content

Commit

Permalink
整理文件
Browse files Browse the repository at this point in the history
  • Loading branch information
ssili126 committed Jan 31, 2024
1 parent 0ac987b commit f7feb6a
Show file tree
Hide file tree
Showing 28 changed files with 210 additions and 210 deletions.
File renamed without changes.
138 changes: 69 additions & 69 deletions IPTV_speed.py → temp/IPTV_speed.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@
import re
import requests
import concurrent.futures

def test_speed(channel_name, channel_url):
try:
response = requests.get(channel_url, timeout=2)
if response.status_code == 200:
speed = response.elapsed.total_seconds()
return channel_name, channel_url, f"{speed:.3f} seconds"
else:
return channel_name, channel_url, "Failed"
except:
return channel_name, channel_url, "Failed"

def channel_key(channel):
match = re.search(r'\d+', channel)
if match:
return int(match.group())
else:
return float('inf') # 返回一个无穷大的数字作为关键字

channels = []

with open("IPTV.txt", 'r', encoding='utf-8') as file:
lines = file.readlines()

for line in lines:
line = line.strip()
import re
import requests
import concurrent.futures

def test_speed(channel_name, channel_url):
try:
response = requests.get(channel_url, timeout=2)
if response.status_code == 200:
speed = response.elapsed.total_seconds()
return channel_name, channel_url, f"{speed:.3f} seconds"
else:
return channel_name, channel_url, "Failed"
except:
return channel_name, channel_url, "Failed"

def channel_key(channel):
match = re.search(r'\d+', channel)
if match:
return int(match.group())
else:
return float('inf') # 返回一个无穷大的数字作为关键字

channels = []

with open("IPTV.txt", 'r', encoding='utf-8') as file:
lines = file.readlines()

for line in lines:
line = line.strip()
if line:
if 'rtp' in line or 'udp' in line:
pass
else:
channel_name, channel_url = line.split(',')
channels.append((channel_name, channel_url))

with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor:
futures = []

for channel in channels:
channel_name, channel_url = channel
futures.append(executor.submit(test_speed, channel_name, channel_url))

results = []
for future in concurrent.futures.as_completed(futures):
result = future.result()
results.append(result)

results.sort(key=lambda x: (x[0], x[2]))

with open("speed_results.txt", 'w', encoding='utf-8') as file:
for result in results:
channel_name, channel_url, speed = result
file.write(f"{channel_name},{channel_url},{speed}\n")


channels = []
with open("speed_results.txt", 'r', encoding='utf-8') as file:
for line in file:
line = line.strip()
if line:
channel_name, channel_url, speed = line.split(',')
if speed != "Failed":
channels.append((channel_name,channel_url))


# 对频道进行排序
channels.sort(key=lambda x: channel_key(x[0]))

with open("IPTV_speed.txt", 'w', encoding='utf-8') as file:
for channel_name,channel_url in channels:
file.write(f'{channel_name},{channel_url}\n')
else:
channel_name, channel_url = line.split(',')
channels.append((channel_name, channel_url))

with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor:
futures = []

for channel in channels:
channel_name, channel_url = channel
futures.append(executor.submit(test_speed, channel_name, channel_url))

results = []
for future in concurrent.futures.as_completed(futures):
result = future.result()
results.append(result)

results.sort(key=lambda x: (x[0], x[2]))

with open("speed_results.txt", 'w', encoding='utf-8') as file:
for result in results:
channel_name, channel_url, speed = result
file.write(f"{channel_name},{channel_url},{speed}\n")


channels = []
with open("speed_results.txt", 'r', encoding='utf-8') as file:
for line in file:
line = line.strip()
if line:
channel_name, channel_url, speed = line.split(',')
if speed != "Failed":
channels.append((channel_name,channel_url))


# 对频道进行排序
channels.sort(key=lambda x: channel_key(x[0]))

with open("IPTV_speed.txt", 'w', encoding='utf-8') as file:
for channel_name,channel_url in channels:
file.write(f'{channel_name},{channel_url}\n')
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
210 changes: 105 additions & 105 deletions download_speed.py → temp/download_speed.py
Original file line number Diff line number Diff line change
@@ -1,105 +1,105 @@
import os
import cv2
import re
import time
import threading
from queue import Queue
import requests
import eventlet
eventlet.monkey_patch()

# 线程安全的队列,用于存储下载任务
task_queue = Queue()

# 线程安全的列表,用于存储结果
results = []

channels = []

with open("IPTV.txt", 'r', encoding='utf-8') as file:
lines = file.readlines()
for line in lines:
line = line.strip()
if line:
channel_name, channel_url = line.split(',')
channels.append((channel_name, channel_url))

# 定义工作线程函数
def worker():
while True:
# 从队列中获取一个任务
channel_name, channel_url = task_queue.get()
try:
channel_url_t = channel_url.rstrip(channel_url.split('/')[-1]) # m3u8链接前缀
lines = requests.get(channel_url).text.strip().split('\n') # 获取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_url = channel_url_t + ts_lists[0] # 拼接单个视频片段下载链接

# 多获取的视频数据进行5秒钟限制
with eventlet.Timeout(5, False):
start_time = time.time()
content = requests.get(ts_url).content
end_time = time.time()
response_time = (end_time - start_time) * 1

if content:
with open(ts_lists_0, 'ab') as f:
f.write(content) # 写入文件
file_size = len(content)
download_speed = file_size / response_time / 1024
normalized_speed = min(max(download_speed / 1024, 0.001), 100) # 将速率从kB/s转换为MB/s并限制在1~100之间

# 获取帧宽度和帧高度
cap = cv2.VideoCapture(ts_lists_0)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
cap.release()

# 删除下载的文件
os.remove(ts_lists_0)
if frame_width != 0:
result = channel_name, channel_url, f"{normalized_speed:.3f} MB/s", f"{frame_width}X{frame_height}"
results.append(result)
print(f"可用频道数:{len(results)}")
except:
pass
# 标记任务完成
task_queue.task_done()


# 创建多个工作线程
num_threads = 10
for _ in range(num_threads):
t = threading.Thread(target=worker, daemon=True) # 将工作线程设置为守护线程
t.start()

# 添加下载任务到队列
for channel in channels:
task_queue.put(channel)

# 等待所有任务完成
task_queue.join()


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]))

# 将结果写入文件
with open("download_results.txt", 'w', encoding='utf-8') as file:
for result in results:
channel_name, channel_url, speed, resolution = result
file.write(f"{channel_name},{channel_url},{speed},{resolution}\n")

with open("download_speed.txt", 'w', encoding='utf-8') as file:
for result in results:
channel_name, channel_url, speed, resolution = result
file.write(f"{channel_name},{channel_url}\n")
import os
import cv2
import re
import time
import threading
from queue import Queue
import requests
import eventlet
eventlet.monkey_patch()

# 线程安全的队列,用于存储下载任务
task_queue = Queue()

# 线程安全的列表,用于存储结果
results = []

channels = []

with open("IPTV.txt", 'r', encoding='utf-8') as file:
lines = file.readlines()
for line in lines:
line = line.strip()
if line:
channel_name, channel_url = line.split(',')
channels.append((channel_name, channel_url))

# 定义工作线程函数
def worker():
while True:
# 从队列中获取一个任务
channel_name, channel_url = task_queue.get()
try:
channel_url_t = channel_url.rstrip(channel_url.split('/')[-1]) # m3u8链接前缀
lines = requests.get(channel_url).text.strip().split('\n') # 获取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_url = channel_url_t + ts_lists[0] # 拼接单个视频片段下载链接

# 多获取的视频数据进行5秒钟限制
with eventlet.Timeout(5, False):
start_time = time.time()
content = requests.get(ts_url).content
end_time = time.time()
response_time = (end_time - start_time) * 1

if content:
with open(ts_lists_0, 'ab') as f:
f.write(content) # 写入文件
file_size = len(content)
download_speed = file_size / response_time / 1024
normalized_speed = min(max(download_speed / 1024, 0.001), 100) # 将速率从kB/s转换为MB/s并限制在1~100之间

# 获取帧宽度和帧高度
cap = cv2.VideoCapture(ts_lists_0)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
cap.release()

# 删除下载的文件
os.remove(ts_lists_0)
if frame_width != 0:
result = channel_name, channel_url, f"{normalized_speed:.3f} MB/s", f"{frame_width}X{frame_height}"
results.append(result)
print(f"可用频道数:{len(results)}")
except:
pass
# 标记任务完成
task_queue.task_done()


# 创建多个工作线程
num_threads = 10
for _ in range(num_threads):
t = threading.Thread(target=worker, daemon=True) # 将工作线程设置为守护线程
t.start()

# 添加下载任务到队列
for channel in channels:
task_queue.put(channel)

# 等待所有任务完成
task_queue.join()


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]))

# 将结果写入文件
with open("download_results.txt", 'w', encoding='utf-8') as file:
for result in results:
channel_name, channel_url, speed, resolution = result
file.write(f"{channel_name},{channel_url},{speed},{resolution}\n")

with open("download_speed.txt", 'w', encoding='utf-8') as file:
for result in results:
channel_name, channel_url, speed, resolution = result
file.write(f"{channel_name},{channel_url}\n")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit f7feb6a

Please sign in to comment.