shvchenko commited on
Commit
f7667b8
·
verified ·
1 Parent(s): d78d98b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -39
app.py CHANGED
@@ -1,47 +1,92 @@
1
  import re
2
  import requests
3
  import gradio as gr
 
4
 
5
- def filter_m3u(url):
6
- # Convert GitHub blob links to raw links
7
- if "github.com" in url and "blob" in url:
8
- url = url.replace("github.com", "raw.githubusercontent.com").replace("/blob/", "/")
9
-
10
- resp = requests.get(url)
11
- if resp.status_code != 200:
12
- return f"Error fetching file: {resp.status_code}"
13
-
14
- lines = resp.text.splitlines()
15
-
16
- # Regex: keep only MT, remove UK & EST, strip commas
17
- pattern = re.compile(
18
- r"^(.*\()(?:.*?UK / )?(.*?MT)(?: / .*?EST)?\)$"
19
- )
20
-
21
- filtered_lines = []
22
- for line in lines:
23
- match = pattern.match(line)
24
- if match:
25
- cleaned = match.group(1) + match.group(2) + ")"
26
- cleaned = cleaned.replace(",", "")
27
- filtered_lines.append(cleaned)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  else:
29
- filtered_lines.append(line)
30
-
31
- # Save to temporary file (HuggingFace gives a download link automatically)
32
- output_file = "/tmp/DaddyLiveEventsFiltered.m3u8"
33
- with open(output_file, "w", encoding="utf-8") as f:
34
- f.write("\n".join(filtered_lines))
35
-
36
- return output_file
37
-
38
- demo = gr.Interface(
39
- fn=filter_m3u,
40
- inputs=gr.Textbox(label="M3U URL", placeholder="Paste your .m3u8 link"),
41
- outputs=gr.File(label="Download filtered M3U"),
42
- title="M3U Cleaner",
43
- description="Paste a .m3u8 URL, it will remove UK & EST timestamps, keep only MT, and clean commas."
44
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  if __name__ == "__main__":
47
  demo.launch()
 
1
  import re
2
  import requests
3
  import gradio as gr
4
+ from pathlib import Path
5
 
6
+ def _to_raw_github(url: str) -> str:
7
+ if "github.com" in url and "/blob/" in url:
8
+ return url.replace("github.com", "raw.githubusercontent.com").replace("/blob/", "/")
9
+ return url
10
+
11
+ def _clean_parenthetical(text: str) -> str:
12
+ """
13
+ Inside the first (...) group, keep only the MT entry and remove commas from the date.
14
+ Examples handled:
15
+ (Aug 21, 2025 4:55 PM UK / Aug 21, 2025 9:55 AM MT / Aug 21, 2025 11:55 AM EST)
16
+ (Aug 21, 2025 9:55 AM MT)
17
+ Leaves non-matching lines untouched.
18
+ """
19
+ m = re.search(r"\(([^)]*)\)", text)
20
+ if not m:
21
+ return text
22
+
23
+ inside = m.group(1)
24
+ parts = [p.strip() for p in inside.split(" / ")]
25
+ # Prefer the segment that explicitly ends with MT (avoid matching team names etc.)
26
+ mt_seg = None
27
+ for p in parts:
28
+ if re.search(r"\bMT\b", p):
29
+ mt_seg = p
30
+ break
31
+ if not mt_seg:
32
+ return text # no MT segment—don’t touch
33
+
34
+ # Remove commas from the date in the MT segment
35
+ mt_seg_clean = mt_seg.replace(",", "")
36
+ # Rebuild the string with only the MT segment
37
+ start, end = m.span()
38
+ return text[:start] + f"({mt_seg_clean})" + text[end:]
39
+
40
+ def _filter_text(content: str) -> str:
41
+ out_lines = []
42
+ for line in content.splitlines():
43
+ # Only try to clean lines that actually contain timezones structure
44
+ if "(" in line and " / " in line and "MT" in line:
45
+ out_lines.append(_clean_parenthetical(line))
46
  else:
47
+ out_lines.append(line)
48
+ return "\n".join(out_lines)
49
+
50
+ def filter_from_url(url: str):
51
+ if not url or not url.strip():
52
+ return "Please paste a URL."
53
+ url = _to_raw_github(url.strip())
54
+ try:
55
+ r = requests.get(url, timeout=20)
56
+ except Exception as e:
57
+ return f"Error fetching file: {e}"
58
+ if r.status_code != 200:
59
+ return f"Error fetching file: HTTP {r.status_code}"
60
+
61
+ filtered = _filter_text(r.text)
62
+ out_path = Path("/tmp/DaddyLiveEventsFiltered.m3u8")
63
+ out_path.write_text(filtered, encoding="utf-8")
64
+ return str(out_path)
65
+
66
+ def filter_from_file(file_obj):
67
+ if file_obj is None:
68
+ return "Please upload a file."
69
+ text = Path(file_obj.name).read_text(encoding="utf-8", errors="ignore")
70
+ filtered = _filter_text(text)
71
+ out_path = Path("/tmp/DaddyLiveEventsFiltered.m3u8")
72
+ out_path.write_text(filtered, encoding="utf-8")
73
+ return str(out_path)
74
+
75
+ with gr.Blocks(title="M3U Cleaner") as demo:
76
+ gr.Markdown("### Paste a `.m3u8` URL **or** upload a file. Keeps only **MT** time and removes commas in the date.")
77
+ with gr.Tab("From URL"):
78
+ url_in = gr.Textbox(label="M3U URL", placeholder="https://…/file.m3u8")
79
+ url_btn = gr.Button("Clean")
80
+ url_out = gr.File(label="Download filtered M3U")
81
+ url_btn.click(filter_from_url, inputs=url_in, outputs=url_out)
82
+ with gr.Tab("From File"):
83
+ file_in = gr.File(label="Upload .m3u or .m3u8")
84
+ file_btn = gr.Button("Clean")
85
+ file_out = gr.File(label="Download filtered M3U")
86
+ file_btn.click(filter_from_file, inputs=file_in, outputs=file_out)
87
+
88
+ # Queue is recommended on Spaces
89
+ demo.queue()
90
 
91
  if __name__ == "__main__":
92
  demo.launch()