feat:get_speed_yt_dlp

This commit is contained in:
guorong.zheng 2024-11-19 18:28:17 +08:00
parent 865d919350
commit b032d985c6
6 changed files with 37 additions and 36 deletions

View file

@ -9,7 +9,7 @@ online_search_page_num = 1
urls_limit = 10
open_keep_all = False
open_sort = True
sort_timeout = 10
sort_timeout = 5
open_ffmpeg = True
open_filter_resolution = True
min_resolution = 1920x1080

View file

@ -12,7 +12,7 @@
| urls_limit | 10 | 单个频道接口数量 |
| open_keep_all | False | 保留所有检索结果,会保留非模板频道名称的结果,推荐手动维护时开启 |
| open_sort | True | 开启排序功能(响应速度、日期、分辨率) |
| sort_timeout | 10 | 单个接口测速超时时长,单位秒(s);数值越大测速所属时间越长,能提高获取接口数量,但质量会有所下降;数值越小测速所需时间越短,能获取低延时的接口,质量较好;调整此值能优化更新时间 |
| sort_timeout | 5 | 单个接口测速超时时长,单位秒(s);数值越大测速所属时间越长,能提高获取接口数量,但质量会有所下降;数值越小测速所需时间越短,能获取低延时的接口,质量较好;调整此值能优化更新时间 |
| open_ffmpeg | True | 开启使用 FFmpeg 进行测速,获取更准确的速度与分辨率信息,需要提前手动安装 |
| open_m3u_result | True | 开启转换生成 m3u 文件类型结果链接,支持显示频道图标 |
| open_filter_resolution | True | 开启分辨率过滤低于最小分辨率min_resolution的接口将会被过滤 |

View file

@ -12,7 +12,7 @@
| urls_limit | 10 | Number of interfaces per channel |
| open_keep_all | False | Retain all search results, retain results with non-template channel names, recommended to be turned on when manually maintaining |
| open_sort | True | Enable the sorting function (response speed, date, resolution) |
| sort_timeout | 10 | The timeout duration for speed testing of a single interface, in seconds (s). A larger value means a longer testing period, which can increase the number of interfaces obtained but may decrease their quality. A smaller value means a shorter testing time, which can obtain low-latency interfaces with better quality. Adjusting this value can optimize the update time. |
| sort_timeout | 5 | The timeout duration for speed testing of a single interface, in seconds (s). A larger value means a longer testing period, which can increase the number of interfaces obtained but may decrease their quality. A smaller value means a shorter testing time, which can obtain low-latency interfaces with better quality. Adjusting this value can optimize the update time. |
| open_ffmpeg | True | Enable speed testing using FFmpeg to obtain more accurate speed and resolution information. Manual installation is required in advance. |
| open_m3u_result | True | Enable the conversion to generate m3u file type result links, supporting the display of channel icons |
| open_filter_resolution | True | Enable resolution filtering, interfaces with resolution lower than the minimum resolution (min_resolution) will be filtered |

View file

@ -29,7 +29,6 @@ from updates.fofa import get_channels_by_fofa
from updates.online_search import get_channels_by_online_search
import os
from tqdm import tqdm
from tqdm.asyncio import tqdm_asyncio
from time import time
from flask import Flask, render_template_string
import sys
@ -173,7 +172,7 @@ class UpdateSource:
0,
)
self.start_time = time()
self.pbar = tqdm_asyncio(total=self.total, desc="Sorting")
self.pbar = tqdm(total=self.total, desc="Sorting")
self.channel_data = process_sort_channel_list(
self.channel_data,
ipv6=ipv6_support,

View file

@ -24,6 +24,7 @@ import base64
import pickle
import copy
import datetime
from concurrent.futures import ThreadPoolExecutor
handler = None
@ -618,22 +619,27 @@ def process_sort_channel_list(data, ipv6=False, callback=None):
ipv6_proxy = None if (not config.open_ipv6 or ipv6) else constants.ipv6_proxy
need_sort_data = copy.deepcopy(data)
process_nested_dict(need_sort_data, seen=set(), flag=r"cache:(.*)", force_str="!")
sort_results = [
sort_channel_list(
sort_data = {}
with ThreadPoolExecutor(max_workers=30) as executor:
futures = [
executor.submit(
sort_channel_list,
cate,
name,
info_list,
ipv6_proxy=ipv6_proxy,
callback=callback,
)
for cate, channel_obj in need_sort_data.items()
for name, info_list in channel_obj.items()
]
sort_data = {}
for result in sort_results:
if result:
cate, name, result_data = result["cate"], result["name"], result["data"]
append_data_to_info_data(sort_data, cate, name, result_data, check=False)
for cate, channel_obj in need_sort_data.items()
for name, info_list in channel_obj.items()
]
for future in futures:
result = future.result()
if result:
cate, name, result_data = result["cate"], result["name"], result["data"]
append_data_to_info_data(
sort_data, cate, name, result_data, check=False
)
for cate, obj in data.items():
for name, info_list in obj.items():
sort_info_list = sort_data.get(cate, {}).get(name, [])

View file

@ -8,23 +8,24 @@ import subprocess
import yt_dlp
def get_speed_yt_dlp(url, timeout=5):
def get_speed_yt_dlp(url, timeout=config.sort_timeout):
"""
Get the speed of the url by yt_dlp
"""
ydl_opts = {
"timeout": timeout,
"skip_download": True,
"quiet": True,
"no_warnings": True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
try:
try:
ydl_opts = {
"socket_timeout": timeout,
"skip_download": True,
"quiet": True,
"no_warnings": True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
start = time()
info = ydl.extract_info(url, download=False)
return int(round((time() - start) * 1000)) if info else float("inf")
except:
return float("inf")
except Exception as e:
return float("inf")
async def get_speed(url, timeout=config.sort_timeout, proxy=None):
"""
@ -176,20 +177,15 @@ def get_speed_by_info(url_info, ipv6_proxy=None, callback=None):
callback()
def sort_urls_by_speed_and_resolution(
data, ipv6_proxy=None, callback=None
):
def sort_urls_by_speed_and_resolution(data, ipv6_proxy=None, callback=None):
"""
Sort by speed and resolution
"""
response = asyncio.gather(
*(
get_speed_by_info(
url_info, ipv6_proxy=ipv6_proxy, callback=callback
)
for url_info in data
response = []
for url_info in data:
response.append(
get_speed_by_info(url_info, ipv6_proxy=ipv6_proxy, callback=callback)
)
)
valid_response = [res for res in response if res != float("inf")]
def combined_key(item):