change / app.py
shvchenko's picture
Create app.py
8ea93c7 verified
import gradio as gr
import re
import tempfile
import requests
# GitHub raw M3U8 URL (converted to raw)
GITHUB_RAW_URL = "https://raw.githubusercontent.com/Drewski2423/DrewLive/main/Tims247.m3u8"
def fix_extinf_line(line):
line = line.replace('#EXTINF:-1', '', 1)
line = re.sub(r'\s*-1\s*', ' ', line)
tvg_id = re.search(r'tvg-id="([^"]+)"', line)
tvg_logo = re.search(r'tvg-logo="([^"]+)"', line)
group_title = re.search(r'group-title="([^"]+)"', line)
title_match = re.search(r',\s*(.+)', line)
tvg_id_val = tvg_id.group(1) if tvg_id else 'unknown.id'
tvg_logo_val = tvg_logo.group(1) if tvg_logo else ''
group_title_val = group_title.group(1).strip() if group_title else 'Misc'
title = title_match.group(1).strip() if title_match else 'Untitled Stream'
fixed_line = f'#EXTINF:-1 tvg-id="{tvg_id_val}"'
if tvg_logo_val:
fixed_line += f' tvg-logo="{tvg_logo_val}"'
fixed_line += f' group-title="{group_title_val}", {title}'
return fixed_line
def clean_content(content: str):
lines = content.splitlines()
cleaned_lines = []
for line in lines:
if line.strip().startswith('#EXTINF'):
cleaned_lines.append(fix_extinf_line(line))
else:
cleaned_lines.append(line)
return '\n'.join(cleaned_lines)
def clean_from_github():
try:
response = requests.get(GITHUB_RAW_URL)
response.raise_for_status()
content = response.text
cleaned = clean_content(content)
# Save to temp file
with tempfile.NamedTemporaryFile(mode='w+', suffix='.m3u', delete=False) as tmp:
tmp.write(cleaned)
tmp_path = tmp.name
return cleaned, tmp_path
except Exception as e:
return f"❌ Failed to load GitHub file: {e}", None
def clean_uploaded_file(file):
content = file.read().decode('utf-8')
cleaned = clean_content(content)
# Save to temp file
with tempfile.NamedTemporaryFile(mode='w+', suffix='.m3u', delete=False) as tmp:
tmp.write(cleaned)
tmp_path = tmp.name
return cleaned, tmp_path
with gr.Blocks() as demo:
gr.Markdown("""
## 🧹 M3U Cleaner - Fix Broken `#EXTINF` Lines
This tool loads and cleans `.m3u` playlists with broken metadata fields.
πŸ”— [Original M3U8 (GitHub - Tims247)](https://github.com/Drewski2423/DrewLive/blob/main/Tims247.m3u8)
Click the button below to fetch and clean the latest version from GitHub,
or upload your own `.m3u` file.
""")
with gr.Row():
github_btn = gr.Button("πŸ“‘ Load and Clean from GitHub")
input_file = gr.File(label="Or Upload Your Own .m3u File", file_types=[".m3u"])
output_text = gr.Textbox(label="🧼 Cleaned M3U Output (Preview)", lines=20)
output_download = gr.File(label="⬇️ Download Cleaned File (.m3u)")
github_btn.click(fn=clean_from_github, inputs=[], outputs=[output_text, output_download])
input_file.change(fn=clean_uploaded_file, inputs=input_file, outputs=[output_text, output_download])
demo.launch()