File size: 3,077 Bytes
8ea93c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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()