2025-08-19 10:54:50 -04:00
|
|
|
import re
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import httpx
|
|
|
|
|
|
2025-09-24 12:30:55 -04:00
|
|
|
from .utils import get_logger, leagues, load_cache, now, write_cache
|
2025-08-30 16:45:19 -04:00
|
|
|
|
|
|
|
|
log = get_logger(__name__)
|
|
|
|
|
|
2025-09-03 03:14:52 -04:00
|
|
|
urls: dict[str, dict[str, str]] = {}
|
2025-08-19 10:54:50 -04:00
|
|
|
|
2025-09-03 03:14:52 -04:00
|
|
|
BASE_URL = "https://tvpass.org/playlist/m3u"
|
2025-08-19 10:54:50 -04:00
|
|
|
|
2025-09-03 03:14:52 -04:00
|
|
|
CACHE_FILE = Path(__file__).parent / "caches" / "tvpass.json"
|
2025-08-28 19:43:35 -04:00
|
|
|
|
2025-08-30 16:45:19 -04:00
|
|
|
|
2025-09-04 19:53:27 -04:00
|
|
|
async def fetch_m3u8(client: httpx.AsyncClient) -> list[str]:
|
2025-08-19 10:54:50 -04:00
|
|
|
try:
|
2025-09-03 03:14:52 -04:00
|
|
|
r = await client.get(BASE_URL)
|
2025-08-19 10:54:50 -04:00
|
|
|
r.raise_for_status()
|
|
|
|
|
except Exception as e:
|
2025-09-03 03:14:52 -04:00
|
|
|
log.error(f'Failed to fetch "{BASE_URL}"\n{e}')
|
2025-09-04 19:53:27 -04:00
|
|
|
return []
|
2025-08-19 10:54:50 -04:00
|
|
|
|
|
|
|
|
return r.text.splitlines()
|
|
|
|
|
|
|
|
|
|
|
2025-09-20 23:26:18 -04:00
|
|
|
async def scrape(client: httpx.AsyncClient) -> None:
|
|
|
|
|
if cached := load_cache(CACHE_FILE, exp=86_400, nearest_hr=True):
|
2025-08-19 10:54:50 -04:00
|
|
|
urls.update(cached)
|
2025-09-03 00:00:22 -04:00
|
|
|
log.info(f"Collected {len(urls)} event(s) from cache")
|
2025-08-19 10:54:50 -04:00
|
|
|
return
|
|
|
|
|
|
2025-09-03 03:14:52 -04:00
|
|
|
log.info(f'Scraping from "{BASE_URL}"')
|
2025-08-19 10:54:50 -04:00
|
|
|
|
2025-09-04 19:53:27 -04:00
|
|
|
for i, line in enumerate(data := await fetch_m3u8(client)):
|
2025-08-27 10:26:56 -04:00
|
|
|
if line.startswith("#EXTINF"):
|
|
|
|
|
tvg_id_match = re.search(r'tvg-id="([^"]*)"', line)
|
2025-09-29 13:42:51 -04:00
|
|
|
tvg_name_match = re.search(r'tvg-name="([^"]*)"', line)
|
2025-08-28 12:18:30 -04:00
|
|
|
group_title_match = re.search(r'group-title="([^"]*)"', line)
|
2025-08-19 10:54:50 -04:00
|
|
|
|
2025-09-29 13:42:51 -04:00
|
|
|
tvg = tvg_id_match[1] if tvg_id_match else None
|
2025-08-19 10:54:50 -04:00
|
|
|
|
2025-09-29 13:42:51 -04:00
|
|
|
if not tvg and (url := data[i + 1]).endswith("/hd"):
|
|
|
|
|
if tvg_name := tvg_name_match[1]:
|
|
|
|
|
sport = group_title_match[1].upper().strip()
|
2025-08-19 10:54:50 -04:00
|
|
|
|
2025-09-29 13:42:51 -04:00
|
|
|
event = "(".join(tvg_name.split("(")[:-1]).strip()
|
2025-08-19 10:54:50 -04:00
|
|
|
|
2025-09-29 13:42:51 -04:00
|
|
|
key = f"[{sport}] {event} (TVP)"
|
2025-09-13 04:42:55 -04:00
|
|
|
|
2025-09-20 23:26:18 -04:00
|
|
|
channel = url.split("/")[-2]
|
|
|
|
|
|
2025-09-24 12:30:55 -04:00
|
|
|
tvg_id, logo = leagues.info(sport)
|
2025-09-21 10:28:15 -04:00
|
|
|
|
2025-09-13 04:42:55 -04:00
|
|
|
entry = {
|
2025-09-20 23:26:18 -04:00
|
|
|
"url": f"http://origin.thetvapp.to/hls/{channel}/mono.m3u8",
|
2025-09-21 10:28:15 -04:00
|
|
|
"logo": logo,
|
2025-09-30 17:27:42 -04:00
|
|
|
"id": tvg_id,
|
2025-09-13 04:42:55 -04:00
|
|
|
"base": "https://tvpass.org",
|
2025-09-05 12:00:23 -04:00
|
|
|
"timestamp": now.timestamp(),
|
2025-08-30 16:45:19 -04:00
|
|
|
}
|
2025-08-19 10:54:50 -04:00
|
|
|
|
2025-09-13 04:42:55 -04:00
|
|
|
urls[key] = entry
|
|
|
|
|
|
2025-09-15 09:26:20 -04:00
|
|
|
write_cache(CACHE_FILE, urls)
|
2025-08-30 16:45:19 -04:00
|
|
|
|
2025-09-04 19:53:27 -04:00
|
|
|
log.info(f"Cached {len(urls)} event(s)")
|