shvchenko commited on
Commit
0b15a62
·
verified ·
1 Parent(s): 8b0711f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -20
app.py CHANGED
@@ -3,16 +3,19 @@ import json
3
  import random
4
  import string
5
  import gradio as gr
 
6
 
7
  # Local JSON file path
8
  CHANNELS_JSON = "channels_fixed.json"
9
 
10
  # --- Helpers ---
11
- def normalize(s):
12
- return re.sub(r'[^a-z0-9]', '', s.lower())
 
 
 
13
 
14
  def fuzzy_match(name, json_keys, cutoff=0.8):
15
- from difflib import get_close_matches
16
  matches = get_close_matches(normalize(name), [normalize(k) for k in json_keys], n=1, cutoff=cutoff)
17
  if matches:
18
  for k in json_keys:
@@ -26,17 +29,14 @@ def generate_filename(upper=False):
26
 
27
  # --- Main Processing ---
28
  def process_playlist(m3u_content):
29
- # Use pasted M3U content
30
  m3u_lines = m3u_content.splitlines()
31
 
32
- # Load JSON reference
33
  try:
34
  with open(CHANNELS_JSON, 'r', encoding='utf-8') as f:
35
  channels_ref = json.load(f)
36
  except Exception as e:
37
  return f"Error reading JSON: {e}", None, None
38
 
39
- # Split 24/7 vs Events
40
  m3u_24_7 = []
41
  m3u_events = []
42
  current_section = None
@@ -60,27 +60,45 @@ def process_playlist(m3u_content):
60
 
61
  log_lines = []
62
 
63
- def update_tvg_id(lines):
64
  updated = []
65
  for line in lines:
66
  if line.startswith('#EXTINF'):
67
  parts = line.split(',')
68
  ch_name = parts[-1].strip()
69
- match_key = fuzzy_match(ch_name, channels_ref.keys())
70
- if match_key:
71
- new_tvg_id = channels_ref[match_key].get('tvg_id', '')
72
- if 'tvg-id=' in line:
73
- line = re.sub(r'tvg-id=".*?"', f'tvg-id="{new_tvg_id}"', line)
74
- else:
75
- line = line.replace('#EXTINF:', f'#EXTINF:-1 tvg-id="{new_tvg_id}",')
76
- log_lines.append(f"✅ {ch_name} → {new_tvg_id}")
 
 
 
 
 
 
 
 
 
77
  else:
78
- log_lines.append(f"⚠️ {ch_name} (no match)")
 
 
 
 
 
 
 
 
 
79
  updated.append(line)
80
  return updated
81
 
82
- updated_24_7 = update_tvg_id(m3u_24_7)
83
- updated_events = update_tvg_id(m3u_events)
84
 
85
  filename_24_7 = generate_filename(upper=True)
86
  filename_events = generate_filename(upper=False)
@@ -103,8 +121,8 @@ demo = gr.Interface(
103
  gr.File(label="Download 24/7 Channels M3U"),
104
  gr.File(label="Download Events M3U")
105
  ],
106
- title="Project 1 Playlist Updater - Paste M3U8",
107
- description="Paste the M3U8 playlist content and update tvg-ids using channels_fixed.json with fuzzy matching."
108
  )
109
 
110
  demo.launch()
 
3
  import random
4
  import string
5
  import gradio as gr
6
+ from difflib import get_close_matches
7
 
8
  # Local JSON file path
9
  CHANNELS_JSON = "channels_fixed.json"
10
 
11
  # --- Helpers ---
12
+ def normalize(name):
13
+ name = name.lower()
14
+ name = re.sub(r'[^a-z0-9]', '', name)
15
+ name = re.sub(r'0+', '', name) # remove leading zeros
16
+ return name
17
 
18
  def fuzzy_match(name, json_keys, cutoff=0.8):
 
19
  matches = get_close_matches(normalize(name), [normalize(k) for k in json_keys], n=1, cutoff=cutoff)
20
  if matches:
21
  for k in json_keys:
 
29
 
30
  # --- Main Processing ---
31
  def process_playlist(m3u_content):
 
32
  m3u_lines = m3u_content.splitlines()
33
 
 
34
  try:
35
  with open(CHANNELS_JSON, '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
39
 
 
40
  m3u_24_7 = []
41
  m3u_events = []
42
  current_section = None
 
60
 
61
  log_lines = []
62
 
63
+ def update_tvg_id(lines, is_event=False):
64
  updated = []
65
  for line in lines:
66
  if line.startswith('#EXTINF'):
67
  parts = line.split(',')
68
  ch_name = parts[-1].strip()
69
+
70
+ if is_event:
71
+ match_brackets = re.search(r'\[(.*?)\]', ch_name)
72
+ ch_name_to_match = match_brackets.group(1) if match_brackets else ch_name
73
+
74
+ ch_name_norm = normalize(ch_name_to_match)
75
+ json_keys_norm = {k: normalize(k) for k in channels_ref.keys()}
76
+
77
+ match_key = None
78
+ matches = get_close_matches(ch_name_norm, list(json_keys_norm.values()), n=1, cutoff=0.7)
79
+ if matches:
80
+ for k, v in json_keys_norm.items():
81
+ if v == matches[0]:
82
+ match_key = k
83
+ break
84
+
85
+ new_tvg_id = channels_ref[match_key]['tvg_id'] if match_key else 'Live.Event.us'
86
  else:
87
+ match_key = fuzzy_match(ch_name, channels_ref.keys())
88
+ new_tvg_id = channels_ref[match_key]['tvg_id'] if match_key else 'Info.Guide.Dummy.us'
89
+
90
+ if 'tvg-id=' in line:
91
+ line = re.sub(r'tvg-id=".*?"', f'tvg-id="{new_tvg_id}"', line)
92
+ else:
93
+ line = line.replace('#EXTINF:', f'#EXTINF:-1 tvg-id="{new_tvg_id}",')
94
+
95
+ log_lines.append(f"{'✅' if (match_key or not is_event) else '⚠️'} {ch_name} → {new_tvg_id}")
96
+
97
  updated.append(line)
98
  return updated
99
 
100
+ updated_24_7 = update_tvg_id(m3u_24_7, is_event=False)
101
+ updated_events = update_tvg_id(m3u_events, is_event=True)
102
 
103
  filename_24_7 = generate_filename(upper=True)
104
  filename_events = generate_filename(upper=False)
 
121
  gr.File(label="Download 24/7 Channels M3U"),
122
  gr.File(label="Download Events M3U")
123
  ],
124
+ title="Project 1 Playlist Updater - Smart Events",
125
+ description="Paste the M3U8 playlist content. Updates 24/7 and Events channels tvg-id using channels_fixed.json with smart Events matching."
126
  )
127
 
128
  demo.launch()