Update app.py

This commit is contained in:
alantang 2025-03-25 13:16:21 +08:00 committed by GitHub
parent f873aee1a0
commit b0982289c1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

43
app.py
View File

@ -196,24 +196,31 @@ def save_m3u(lines, filename):
def save_txt(lines, filename): def save_txt(lines, filename):
"""保存为 live.txt 格式(格式:分组名称,频道名称,URL""" """保存为 live.txt 格式(格式:分组名称,频道名称,URL"""
with open(filename, 'w', encoding='utf-8') as f: try:
i = 0 with open(filename, 'w', encoding='utf-8') as f:
while i < len(lines): i = 0
if lines[i].startswith("#EXTINF"): while i < len(lines):
# 提取分组名称 if lines[i].startswith("#EXTINF"):
group_match = re.search(r'group-title="([^"]+)"', lines[i]) # 提取分组名称
group = group_match.group(1) if group_match else "" group_match = re.search(r'group-title="([^"]+)"', lines[i])
group = group_match.group(1) if group_match else ""
# 提取频道名称(最后一个逗号后的内容)
channel_name = lines[i].split(',')[-1].strip() # 优先使用 tvg-name 作为频道名称,否则取最后一个逗号后的内容
channel_match = re.search(r'tvg-name="([^"]+)"', lines[i])
# 获取URL确保索引不越界 channel_name = channel_match.group(1).strip() if channel_match else lines[i].split(',')[-1].strip()
if i+1 < len(lines):
url = lines[i+1].strip() # 获取URL
f.write(f"{group},{channel_name},{url}\n") if i+1 < len(lines):
i += 2 url = lines[i+1].strip()
else: f.write(f"{group},{channel_name},{url}\n")
i += 1 i += 2
else:
i += 1
print(f"✅ TXT文件 {filename} 已生成,共写入 {(i//2)} 个频道")
except PermissionError:
print(f"❌ 错误:无权限写入文件 {filename}")
except Exception as e:
print(f"❌ 保存文件时发生未知错误:{str(e)}")
# ========== 主流程 ========== # ========== 主流程 ==========