shvchenko commited on
Commit
8ea93c7
·
verified ·
1 Parent(s): 331e783

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import re
3
+ import tempfile
4
+ import requests
5
+
6
+ # GitHub raw M3U8 URL (converted to raw)
7
+ GITHUB_RAW_URL = "https://raw.githubusercontent.com/Drewski2423/DrewLive/main/Tims247.m3u8"
8
+
9
+ def fix_extinf_line(line):
10
+ line = line.replace('#EXTINF:-1', '', 1)
11
+ line = re.sub(r'\s*-1\s*', ' ', line)
12
+
13
+ tvg_id = re.search(r'tvg-id="([^"]+)"', line)
14
+ tvg_logo = re.search(r'tvg-logo="([^"]+)"', line)
15
+ group_title = re.search(r'group-title="([^"]+)"', line)
16
+ title_match = re.search(r',\s*(.+)', line)
17
+
18
+ tvg_id_val = tvg_id.group(1) if tvg_id else 'unknown.id'
19
+ tvg_logo_val = tvg_logo.group(1) if tvg_logo else ''
20
+ group_title_val = group_title.group(1).strip() if group_title else 'Misc'
21
+ title = title_match.group(1).strip() if title_match else 'Untitled Stream'
22
+
23
+ fixed_line = f'#EXTINF:-1 tvg-id="{tvg_id_val}"'
24
+ if tvg_logo_val:
25
+ fixed_line += f' tvg-logo="{tvg_logo_val}"'
26
+ fixed_line += f' group-title="{group_title_val}", {title}'
27
+ return fixed_line
28
+
29
+ def clean_content(content: str):
30
+ lines = content.splitlines()
31
+ cleaned_lines = []
32
+
33
+ for line in lines:
34
+ if line.strip().startswith('#EXTINF'):
35
+ cleaned_lines.append(fix_extinf_line(line))
36
+ else:
37
+ cleaned_lines.append(line)
38
+
39
+ return '\n'.join(cleaned_lines)
40
+
41
+ def clean_from_github():
42
+ try:
43
+ response = requests.get(GITHUB_RAW_URL)
44
+ response.raise_for_status()
45
+ content = response.text
46
+ cleaned = clean_content(content)
47
+
48
+ # Save to temp file
49
+ with tempfile.NamedTemporaryFile(mode='w+', suffix='.m3u', delete=False) as tmp:
50
+ tmp.write(cleaned)
51
+ tmp_path = tmp.name
52
+
53
+ return cleaned, tmp_path
54
+ except Exception as e:
55
+ return f"❌ Failed to load GitHub file: {e}", None
56
+
57
+ def clean_uploaded_file(file):
58
+ content = file.read().decode('utf-8')
59
+ cleaned = clean_content(content)
60
+
61
+ # Save to temp file
62
+ with tempfile.NamedTemporaryFile(mode='w+', suffix='.m3u', delete=False) as tmp:
63
+ tmp.write(cleaned)
64
+ tmp_path = tmp.name
65
+
66
+ return cleaned, tmp_path
67
+
68
+ with gr.Blocks() as demo:
69
+ gr.Markdown("""
70
+ ## 🧹 M3U Cleaner - Fix Broken `#EXTINF` Lines
71
+
72
+ This tool loads and cleans `.m3u` playlists with broken metadata fields.
73
+
74
+ 🔗 [Original M3U8 (GitHub - Tims247)](https://github.com/Drewski2423/DrewLive/blob/main/Tims247.m3u8)
75
+
76
+ Click the button below to fetch and clean the latest version from GitHub,
77
+ or upload your own `.m3u` file.
78
+ """)
79
+
80
+ with gr.Row():
81
+ github_btn = gr.Button("📡 Load and Clean from GitHub")
82
+ input_file = gr.File(label="Or Upload Your Own .m3u File", file_types=[".m3u"])
83
+
84
+ output_text = gr.Textbox(label="🧼 Cleaned M3U Output (Preview)", lines=20)
85
+ output_download = gr.File(label="⬇️ Download Cleaned File (.m3u)")
86
+
87
+ github_btn.click(fn=clean_from_github, inputs=[], outputs=[output_text, output_download])
88
+ input_file.change(fn=clean_uploaded_file, inputs=input_file, outputs=[output_text, output_download])
89
+
90
+ demo.launch()