Update app.py
Browse files
app.py
CHANGED
|
@@ -4,10 +4,6 @@ import random
|
|
| 4 |
import string
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
# Local file paths
|
| 8 |
-
M3U_FILE = "daddylive-channels-events.m3u8"
|
| 9 |
-
CHANNELS_JSON = "channels_fixed.json"
|
| 10 |
-
|
| 11 |
# --- Helpers ---
|
| 12 |
def normalize(s):
|
| 13 |
return re.sub(r'[^a-z0-9]', '', s.lower())
|
|
@@ -26,17 +22,17 @@ def generate_filename(upper=False):
|
|
| 26 |
return f"{chars}.m3u"
|
| 27 |
|
| 28 |
# --- Main Processing ---
|
| 29 |
-
def process_playlist():
|
| 30 |
# Lazy load playlist
|
| 31 |
try:
|
| 32 |
-
with open(
|
| 33 |
m3u_lines = f.read().splitlines()
|
| 34 |
except Exception as e:
|
| 35 |
return f"Error reading playlist: {e}", None, None
|
| 36 |
|
| 37 |
# Load JSON reference
|
| 38 |
try:
|
| 39 |
-
with open(
|
| 40 |
channels_ref = json.load(f)
|
| 41 |
except Exception as e:
|
| 42 |
return f"Error reading JSON: {e}", None, None
|
|
@@ -60,7 +56,6 @@ def process_playlist():
|
|
| 60 |
elif current_section == 'events':
|
| 61 |
m3u_events.append(line)
|
| 62 |
else:
|
| 63 |
-
# Include headers like #EXTM3U
|
| 64 |
m3u_24_7.append(line)
|
| 65 |
m3u_events.append(line)
|
| 66 |
|
|
@@ -70,15 +65,11 @@ def process_playlist():
|
|
| 70 |
updated = []
|
| 71 |
for line in lines:
|
| 72 |
if line.startswith('#EXTINF'):
|
| 73 |
-
# Extract channel name after last comma
|
| 74 |
parts = line.split(',')
|
| 75 |
ch_name = parts[-1].strip()
|
| 76 |
-
|
| 77 |
-
# Match against JSON
|
| 78 |
match_key = fuzzy_match(ch_name, channels_ref.keys())
|
| 79 |
if match_key:
|
| 80 |
new_tvg_id = channels_ref[match_key].get('tvg_id', '')
|
| 81 |
-
# Replace or add tvg-id
|
| 82 |
if 'tvg-id=' in line:
|
| 83 |
line = re.sub(r'tvg-id=".*?"', f'tvg-id="{new_tvg_id}"', line)
|
| 84 |
else:
|
|
@@ -92,11 +83,9 @@ def process_playlist():
|
|
| 92 |
updated_24_7 = update_tvg_id(m3u_24_7)
|
| 93 |
updated_events = update_tvg_id(m3u_events)
|
| 94 |
|
| 95 |
-
# Generate random filenames
|
| 96 |
filename_24_7 = generate_filename(upper=True)
|
| 97 |
filename_events = generate_filename(upper=False)
|
| 98 |
|
| 99 |
-
# Save outputs
|
| 100 |
with open(filename_24_7, 'w', encoding='utf-8') as f:
|
| 101 |
f.write('\n'.join(updated_24_7))
|
| 102 |
|
|
@@ -109,14 +98,17 @@ def process_playlist():
|
|
| 109 |
# --- Gradio UI ---
|
| 110 |
demo = gr.Interface(
|
| 111 |
fn=process_playlist,
|
| 112 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
| 113 |
outputs=[
|
| 114 |
gr.Textbox(label="Update Log", lines=25),
|
| 115 |
gr.File(label="Download 24/7 Channels M3U"),
|
| 116 |
gr.File(label="Download Events M3U")
|
| 117 |
],
|
| 118 |
title="Project 1 Playlist Updater - Local",
|
| 119 |
-
description="Updates tvg-id for 24/7 and Events channels based on
|
| 120 |
)
|
| 121 |
|
| 122 |
-
demo.launch()
|
|
|
|
| 4 |
import string
|
| 5 |
import gradio as gr
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# --- Helpers ---
|
| 8 |
def normalize(s):
|
| 9 |
return re.sub(r'[^a-z0-9]', '', s.lower())
|
|
|
|
| 22 |
return f"{chars}.m3u"
|
| 23 |
|
| 24 |
# --- Main Processing ---
|
| 25 |
+
def process_playlist(m3u_file_path, json_file_path):
|
| 26 |
# Lazy load playlist
|
| 27 |
try:
|
| 28 |
+
with open(m3u_file_path, 'r', encoding='utf-8') as f:
|
| 29 |
m3u_lines = f.read().splitlines()
|
| 30 |
except Exception as e:
|
| 31 |
return f"Error reading playlist: {e}", None, None
|
| 32 |
|
| 33 |
# Load JSON reference
|
| 34 |
try:
|
| 35 |
+
with open(json_file_path, 'r', encoding='utf-8') as f:
|
| 36 |
channels_ref = json.load(f)
|
| 37 |
except Exception as e:
|
| 38 |
return f"Error reading JSON: {e}", None, None
|
|
|
|
| 56 |
elif current_section == 'events':
|
| 57 |
m3u_events.append(line)
|
| 58 |
else:
|
|
|
|
| 59 |
m3u_24_7.append(line)
|
| 60 |
m3u_events.append(line)
|
| 61 |
|
|
|
|
| 65 |
updated = []
|
| 66 |
for line in lines:
|
| 67 |
if line.startswith('#EXTINF'):
|
|
|
|
| 68 |
parts = line.split(',')
|
| 69 |
ch_name = parts[-1].strip()
|
|
|
|
|
|
|
| 70 |
match_key = fuzzy_match(ch_name, channels_ref.keys())
|
| 71 |
if match_key:
|
| 72 |
new_tvg_id = channels_ref[match_key].get('tvg_id', '')
|
|
|
|
| 73 |
if 'tvg-id=' in line:
|
| 74 |
line = re.sub(r'tvg-id=".*?"', f'tvg-id="{new_tvg_id}"', line)
|
| 75 |
else:
|
|
|
|
| 83 |
updated_24_7 = update_tvg_id(m3u_24_7)
|
| 84 |
updated_events = update_tvg_id(m3u_events)
|
| 85 |
|
|
|
|
| 86 |
filename_24_7 = generate_filename(upper=True)
|
| 87 |
filename_events = generate_filename(upper=False)
|
| 88 |
|
|
|
|
| 89 |
with open(filename_24_7, 'w', encoding='utf-8') as f:
|
| 90 |
f.write('\n'.join(updated_24_7))
|
| 91 |
|
|
|
|
| 98 |
# --- Gradio UI ---
|
| 99 |
demo = gr.Interface(
|
| 100 |
fn=process_playlist,
|
| 101 |
+
inputs=[
|
| 102 |
+
gr.Textbox(label="M3U8 Playlist File Path"),
|
| 103 |
+
gr.Textbox(label="JSON File Path")
|
| 104 |
+
],
|
| 105 |
outputs=[
|
| 106 |
gr.Textbox(label="Update Log", lines=25),
|
| 107 |
gr.File(label="Download 24/7 Channels M3U"),
|
| 108 |
gr.File(label="Download Events M3U")
|
| 109 |
],
|
| 110 |
title="Project 1 Playlist Updater - Local",
|
| 111 |
+
description="Updates tvg-id for 24/7 and Events channels based on provided JSON with fuzzy matching."
|
| 112 |
)
|
| 113 |
|
| 114 |
+
demo.launch()
|