Update app.py

This commit is contained in:
alantang 2025-04-11 16:23:13 +08:00 committed by GitHub
parent fb83d8a359
commit 0a56fbc6b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

31
app.py
View File

@ -37,7 +37,7 @@ def robust_download(url, ua, max_retries=3):
if attempt == max_retries - 1:
print(f"❌ 无法下载: {url}. 错误: {str(e)}")
return None
print(f"正在重试 {url} (第 {attempt + 1} 次)")
print(f"正在重试 {url} (第 {attempt+1} 次)")
def process_channel(line):
if any(f'group-title="{g}"' in line for g in DELETE_GROUPS):
@ -96,24 +96,21 @@ def generate_m3u_output(channels):
return "\n".join(output)
def generate_txt_output(channels):
group_dict = defaultdict(list)
"""
Generates a plain text output for the given channels.
Each channel will be written in the format: "Channel Name: URL".
"""
output_lines = []
for channel in channels:
if match_name := re.search(r'tvg-name="([^"]+)"', channel["meta"]):
channel_name = match_name.group(1)
# Extract channel name from metadata
if match := re.search(r'tvg-name="([^"]+)"', channel["meta"]):
channel_name = match.group(1)
else:
channel_name = "Unknown Channel"
if match_group := re.search(r'group-title="([^"]+)"', channel["meta"]):
group_name = match_group.group(1)
else:
group_name = "Unknown Group"
group_dict[group_name].append(f"{channel_name}: {channel['url']}")
output_lines = []
for group, channel_list in group_dict.items():
output_lines.append(f"{group}:")
for channel in channel_list:
output_lines.append(f" {channel}")
output_lines.append("")
# Append formatted line to output
output_lines.append(f"{channel_name}: {channel['url']}")
return "\n".join(output_lines)
def save_file(content, filename):
@ -143,7 +140,8 @@ def main():
channels = parse_m3u(content)
processed = []
for ch in channels:
if cleaned_meta := process_channel(ch["meta"]):
# 修复: 检查 "meta" 键是否存在
if "meta" in ch and (cleaned_meta := process_channel(ch["meta"])):
processed.append({"meta": cleaned_meta, "url": ch["url"]})
all_channels.extend(processed)
@ -164,4 +162,3 @@ def main():
if __name__ == "__main__":
main()