Update app.py

This commit is contained in:
alantang 2025-04-11 15:46:49 +08:00 committed by GitHub
parent 13ae7ae830
commit 3f509cd977
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

55
app.py
View File

@ -2,8 +2,6 @@ import re
import requests
import os
from collections import defaultdict
import time
from concurrent.futures import ThreadPoolExecutor
# ================== 配置区域 ==================
# 需要删除的分组 (这些分组下的频道会被过滤)
@ -27,11 +25,6 @@ M3U_SOURCES = [
{"name": "MyIPTV", "url": "https://git.gra.phite.ro/alantang/auto-iptv/raw/branch/main/live_ipv6.m3u", "ua": "okhttp/4.12.0"}
]
# 最大响应时间(秒),超过此时间的频道将被视为异常
MAX_RESPONSE_TIME = 5
# 线程池最大工作线程数
MAX_WORKERS = 10
# ================== 核心功能 ==================
def robust_download(url, ua, max_retries=3):
headers = {'User-Agent': ua}
@ -42,10 +35,12 @@ def robust_download(url, ua, max_retries=3):
response.encoding = response.apparent_encoding
return response.text
except Exception as e:
if attempt == max_retries - 1:
raise
print(f"正在重试 {url} (第 {attempt + 1} 次)")
if attempt == max_retries - 1: raise
print(f"正在重试 {url} (第 {attempt+1} 次)")
def process_channel(line):
if any(f'group-title="{g}"' in line for g in DELETE_GROUPS):
return None
def process_channel(line):
"""频道信息处理流水线"""
@ -76,7 +71,6 @@ def process_channel(line):
return line
def parse_m3u(content):
channels = []
current_channel = {}
@ -90,32 +84,6 @@ def parse_m3u(content):
current_channel = {}
return channels
def measure_response_time(url, ua):
headers = {'User-Agent': ua}
try:
start_time = time.time()
response = requests.head(url, headers=headers, timeout=MAX_RESPONSE_TIME)
response.raise_for_status()
return time.time() - start_time
except Exception:
return float('inf')
def sort_channels_by_response_time(channels):
def measure_and_sort(channel):
url = channel["url"]
ua = M3U_SOURCES[0]["ua"] # 假设所有源使用相同的UA
response_time = measure_response_time(url, ua)
return response_time, channel
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
results = list(executor.map(measure_and_sort, channels))
valid_results = [result for result in results if result[0] < MAX_RESPONSE_TIME]
valid_results.sort(key=lambda x: x[0])
return [channel for _, channel in valid_results]
def generate_m3u_output(channels):
"""生成M3U格式内容"""
group_dict = defaultdict(list)
@ -133,13 +101,11 @@ def generate_m3u_output(channels):
output = ["#EXTM3U"]
for group, items in ordered_groups:
sorted_items = sort_channels_by_response_time(items)
for item in sorted_items:
for item in items:
output.append(item["meta"])
output.append(item["url"])
return "\n".join(output)
def generate_txt_output(channels):
"""生成TXT格式内容分组名称,频道名称,URL"""
txt_lines = []
@ -160,7 +126,6 @@ def generate_txt_output(channels):
return "\n".join(txt_lines)
def save_file(content, filename):
"""通用文件保存函数"""
# 创建 live 文件夹,如果不存在
@ -169,17 +134,16 @@ def save_file(content, filename):
os.makedirs(live_folder)
file_path = os.path.join(live_folder, filename)
try:
with open(file_path, "w", encoding="utf-8") as f:
with open(filename, "w", encoding="utf-8") as f:
f.write(content)
print(f"✅ 成功生成 {file_path} 文件")
print(f"✅ 成功生成 {filename} 文件")
return True
except PermissionError:
print(f"❌ 无写入权限: {file_path}")
print(f"❌ 无写入权限: {filename}")
except Exception as e:
print(f"❌ 保存文件失败: {str(e)}")
return False
def main():
all_channels = []
@ -210,6 +174,5 @@ def main():
print(f"\n处理完成!有效频道总数: {len(all_channels)}")
if __name__ == "__main__":
main()