Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,92 @@
|
|
| 1 |
import re
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
else:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 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()
|