shvchenko commited on
Commit
3aba0e1
·
verified ·
1 Parent(s): 577e626

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -0
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import json
3
+ import random
4
+ import string
5
+ import gradio as gr
6
+ from difflib import get_close_matches
7
+
8
+ CHANNELS_JSON = "channels_fixed.json"
9
+
10
+ def normalize(name):
11
+ name = name.lower()
12
+ name = re.sub(r'[^a-z0-9]', '', name)
13
+ name = re.sub(r'0+', '', name)
14
+ return name
15
+
16
+ def fuzzy_match(name, json_keys, cutoff=0.8):
17
+ matches = get_close_matches(normalize(name), [normalize(k) for k in json_keys], n=1, cutoff=cutoff)
18
+ if matches:
19
+ for k in json_keys:
20
+ if normalize(k) == matches[0]:
21
+ return k
22
+ return None
23
+
24
+ def generate_filename(upper=False):
25
+ chars = ''.join(random.choices(string.ascii_uppercase if upper else string.ascii_lowercase, k=3))
26
+ return f"{chars}.m3u"
27
+
28
+ def process_playlist(m3u_content):
29
+ m3u_lines = m3u_content.splitlines()
30
+
31
+ try:
32
+ with open(CHANNELS_JSON, 'r', encoding='utf-8') as f:
33
+ channels_ref = json.load(f)
34
+ except Exception as e:
35
+ return f"Error reading JSON: {e}", None, None
36
+
37
+ m3u_24_7 = []
38
+ m3u_events = []
39
+ current_section = None
40
+
41
+ for line in m3u_lines:
42
+ if line.startswith('#EXTINF'):
43
+ if 'group-title="24/7 CHANNELS' in line:
44
+ current_section = '24_7'
45
+ elif 'EVENTS|' in line:
46
+ current_section = 'events'
47
+ else:
48
+ current_section = None
49
+
50
+ if current_section == '24_7':
51
+ m3u_24_7.append(line)
52
+ elif current_section == 'events':
53
+ # Remove 24/7 group-title if mistakenly present
54
+ line = re.sub(r'group-title="24/7 CHANNELS[^"]*"', '', line)
55
+ m3u_events.append(line)
56
+ else:
57
+ m3u_24_7.append(line)
58
+ m3u_events.append(line)
59
+
60
+ log_lines = []
61
+
62
+ def update_tvg_id(lines, is_event=False):
63
+ updated = []
64
+ for line in lines:
65
+ if line.startswith('#EXTINF'):
66
+ parts = line.split(',')
67
+ ch_name = parts[-1].strip()
68
+
69
+ if is_event:
70
+ match_brackets = re.search(r'\[(.*?)\]', ch_name)
71
+ ch_name_to_match = match_brackets.group(1) if match_brackets else ch_name
72
+
73
+ ch_name_norm = normalize(ch_name_to_match)
74
+ json_keys_norm = {k: normalize(k) for k in channels_ref.keys()}
75
+
76
+ match_key = None
77
+ matches = get_close_matches(ch_name_norm, list(json_keys_norm.values()), n=1, cutoff=0.7)
78
+ if matches:
79
+ for k, v in json_keys_norm.items():
80
+ if v == matches[0]:
81
+ match_key = k
82
+ break
83
+
84
+ new_tvg_id = channels_ref[match_key]['tvg_id'] if match_key else 'Live.Event.us'
85
+ # Replace any tvg-id="test" with Live.Event.us
86
+ if 'tvg-id="test"' in line:
87
+ new_tvg_id = 'Live.Event.us'
88
+ else:
89
+ match_key = fuzzy_match(ch_name, channels_ref.keys())
90
+ new_tvg_id = channels_ref[match_key]['tvg_id'] if match_key else 'Info.Guide.Dummy.us'
91
+ if 'tvg-id="test"' in line:
92
+ new_tvg_id = 'Info.Guide.Dummy.us'
93
+
94
+ if 'tvg-id=' in line:
95
+ line = re.sub(r'tvg-id=".*?"', f'tvg-id="{new_tvg_id}"', line)
96
+ else:
97
+ line = line.replace('#EXTINF:', f'#EXTINF:-1 tvg-id="{new_tvg_id}",')
98
+
99
+ log_lines.append(f"{'✅' if (match_key or not is_event) else '⚠️'} {ch_name} → {new_tvg_id}")
100
+
101
+ updated.append(line)
102
+ return updated
103
+
104
+ updated_24_7 = update_tvg_id(m3u_24_7, is_event=False)
105
+ updated_events = update_tvg_id(m3u_events, is_event=True)
106
+
107
+ filename_24_7 = generate_filename(upper=True)
108
+ filename_events = generate_filename(upper=False)
109
+
110
+ with open(filename_24_7, 'w', encoding='utf-8') as f:
111
+ f.write('\n'.join(updated_24_7))
112
+
113
+ with open(filename_events, 'w', encoding='utf-8') as f:
114
+ f.write('\n'.join(updated_events))
115
+
116
+ log_text = '\n'.join(log_lines)
117
+ return log_text, filename_24_7, filename_events
118
+
119
+ # --- Gradio UI ---
120
+ demo = gr.Interface(
121
+ fn=process_playlist,
122
+ inputs=[gr.Textbox(label="Paste M3U8 Playlist Here", lines=25)],
123
+ outputs=[
124
+ gr.Textbox(label="Update Log", lines=25),
125
+ gr.File(label="Download 24/7 Channels M3U"),
126
+ gr.File(label="Download Events M3U")
127
+ ],
128
+ title="Project 1 Playlist Updater - Smart Events",
129
+ description="Paste the M3U8 playlist content. Updates 24/7 and Events channels tvg-id using channels_fixed.json with smart Events matching."
130
+ )
131
+
132
+ demo.launch()