iptv/M3U8/scrapers/tvpass.py

67 lines
2 KiB
Python
Raw Normal View History

2025-08-19 10:54:50 -04:00
import re
from pathlib import Path
import httpx
2025-09-15 09:26:20 -04:00
from .utils import LOGOS, get_logger, 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-08-27 10:26:56 -04:00
async def main(client: httpx.AsyncClient) -> None:
2025-09-06 16:16:36 -04:00
if cached := load_cache(CACHE_FILE, exp=86400, 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)
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
tvg_id = tvg_id_match[1] if tvg_id_match else None
2025-08-27 10:26:56 -04:00
tvg_name = tvg_name_match[1] if tvg_name_match else None
2025-08-28 19:43:35 -04:00
sport = group_title_match[1].upper().strip() if group_title_match else None
2025-08-19 10:54:50 -04:00
if tvg_id == "":
url = data[i + 1]
2025-08-27 10:26:56 -04:00
if tvg_name:
2025-08-28 12:18:30 -04:00
tvg_name = "(".join(tvg_name.split("(")[:-1]).strip()
2025-08-19 10:54:50 -04:00
2025-08-30 14:29:49 -04:00
if url.endswith("/hd"):
2025-09-13 04:42:55 -04:00
key = f"[{sport}] {tvg_name} (TVP)"
entry = {
2025-08-30 16:45:19 -04:00
"url": f"http://origin.thetvapp.to/hls/{url.split('/')[-2]}/mono.m3u8",
2025-09-13 13:32:32 -04:00
"logo": LOGOS.get(sport, LOGOS["default"]),
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)")