shvchenko commited on
Commit
6fd3e73
·
verified ·
1 Parent(s): d4f15fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -13
app.py CHANGED
@@ -4,6 +4,8 @@ import gzip
4
  import io
5
  import xml.etree.ElementTree as ET
6
  import gradio as gr
 
 
7
 
8
  # URLs
9
  CHANNELS_JSON_URL = "https://raw.githubusercontent.com/pigzillaaa/daddylive/main/channels.json"
@@ -28,11 +30,40 @@ def fetch_epg(url):
28
  cid = channel.get("id")
29
  display_name = channel.findtext("display-name")
30
  if cid:
31
- epg_map[cid.strip().upper()] = cid.strip()
32
  if display_name:
33
- epg_map[display_name.strip().upper()] = cid.strip() if cid else None
34
  return epg_map
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  # ---------------- Main Processing ----------------
37
  def fix_channels():
38
  # Load sources
@@ -49,16 +80,10 @@ def fix_channels():
49
  new_id = None
50
 
51
  if group == "UNITED STATES":
52
- if old_id.upper() in us_epg:
53
- new_id = us_epg[old_id.upper()]
54
- elif ch_name.upper() in us_epg:
55
- new_id = us_epg[ch_name.upper()]
56
 
57
  elif group == "CANADA":
58
- if old_id.upper() in ca_epg:
59
- new_id = ca_epg[old_id.upper()]
60
- elif ch_name.upper() in ca_epg:
61
- new_id = ca_epg[ch_name.upper()]
62
 
63
  # Apply update if found
64
  if new_id:
@@ -76,7 +101,11 @@ def fix_channels():
76
  with open(updated_file, "w", encoding="utf-8") as f:
77
  json.dump(updated, f, indent=4, ensure_ascii=False)
78
 
79
- log_text = "\n".join(log_lines)
 
 
 
 
80
  return updated_file, log_text
81
 
82
  # ---------------- Gradio UI ----------------
@@ -85,10 +114,10 @@ demo = gr.Interface(
85
  inputs=[],
86
  outputs=[
87
  gr.File(label="Download Updated channels.json"),
88
- gr.Textbox(label="Log (US & Canada only)", lines=20, interactive=False)
89
  ],
90
  title="Project 2: Channels.json Updater",
91
- description="Updates tvg_id for UNITED STATES and CANADA channels using new EPG XMLs. Outputs a single updated file + log."
92
  )
93
 
94
  if __name__ == "__main__":
 
4
  import io
5
  import xml.etree.ElementTree as ET
6
  import gradio as gr
7
+ import re
8
+ import difflib
9
 
10
  # URLs
11
  CHANNELS_JSON_URL = "https://raw.githubusercontent.com/pigzillaaa/daddylive/main/channels.json"
 
30
  cid = channel.get("id")
31
  display_name = channel.findtext("display-name")
32
  if cid:
33
+ epg_map[cid.strip()] = cid.strip()
34
  if display_name:
35
+ epg_map[display_name.strip()] = cid.strip() if cid else None
36
  return epg_map
37
 
38
+ # ---------------- Matching Helpers ----------------
39
+ def normalize(s):
40
+ return re.sub(r'[^a-z0-9]', '', s.lower())
41
+
42
+ def find_best_match(old_id, ch_name, epg_map):
43
+ if not old_id and not ch_name:
44
+ return None
45
+
46
+ # Build normalized map
47
+ epg_norm_map = {normalize(k): v for k, v in epg_map.items() if k}
48
+
49
+ old_norm = normalize(old_id)
50
+ name_norm = normalize(ch_name)
51
+
52
+ # Try normalized id
53
+ if old_norm in epg_norm_map:
54
+ return epg_norm_map[old_norm]
55
+
56
+ # Try normalized name
57
+ if name_norm in epg_norm_map:
58
+ return epg_norm_map[name_norm]
59
+
60
+ # Fuzzy match on normalized values
61
+ all_norm_keys = list(epg_norm_map.keys())
62
+ for candidate in difflib.get_close_matches(name_norm, all_norm_keys, n=1, cutoff=0.85):
63
+ return epg_norm_map[candidate]
64
+
65
+ return None
66
+
67
  # ---------------- Main Processing ----------------
68
  def fix_channels():
69
  # Load sources
 
80
  new_id = None
81
 
82
  if group == "UNITED STATES":
83
+ new_id = find_best_match(old_id, ch_name, us_epg)
 
 
 
84
 
85
  elif group == "CANADA":
86
+ new_id = find_best_match(old_id, ch_name, ca_epg)
 
 
 
87
 
88
  # Apply update if found
89
  if new_id:
 
101
  with open(updated_file, "w", encoding="utf-8") as f:
102
  json.dump(updated, f, indent=4, ensure_ascii=False)
103
 
104
+ # Sort log: show all ✅ first, ⚠️ unmatched at bottom
105
+ fixed = [l for l in log_lines if l.startswith("✅")]
106
+ unmatched = [l for l in log_lines if l.startswith("⚠️")]
107
+ log_text = "\n".join(fixed + ["", "---- Unmatched ----"] + unmatched if unmatched else fixed)
108
+
109
  return updated_file, log_text
110
 
111
  # ---------------- Gradio UI ----------------
 
114
  inputs=[],
115
  outputs=[
116
  gr.File(label="Download Updated channels.json"),
117
+ gr.Textbox(label="Log (US & Canada only)", lines=25, interactive=False)
118
  ],
119
  title="Project 2: Channels.json Updater",
120
+ description="Updates tvg_id for UNITED STATES and CANADA channels using new EPG XMLs. Uses fuzzy matching to handle naming differences (e.g., TSN1 ⇔ TSN.1). Outputs a single updated file + log."
121
  )
122
 
123
  if __name__ == "__main__":