iptv/M3U8/scrapers/tvpass.py

74 lines
1.9 KiB
Python
Raw Normal View History

2025-12-08 13:21:43 -05:00
import re
2025-12-18 03:04:11 -05:00
from .utils import Cache, Time, get_logger, leagues, network
2025-12-08 13:21:43 -05:00
log = get_logger(__name__)
urls: dict[str, dict[str, str | float]] = {}
2025-12-13 16:57:14 -05:00
TAG = "TVP"
2025-12-08 13:21:43 -05:00
2025-12-16 02:30:44 -05:00
CACHE_FILE = Cache(f"{TAG.lower()}.json", exp=86_400)
2025-12-08 13:21:43 -05:00
2025-12-13 16:57:14 -05:00
BASE_URL = "https://tvpass.org/playlist/m3u"
2025-12-08 13:21:43 -05:00
2025-12-18 03:04:11 -05:00
async def get_events() -> dict[str, dict[str, str | float]]:
events = {}
2025-12-08 13:21:43 -05:00
2025-12-18 03:04:11 -05:00
if not (r := await network.request(BASE_URL, log=log)):
return events
2025-12-08 13:21:43 -05:00
2025-12-18 03:04:11 -05:00
now = Time.clean(Time.now())
2025-12-08 13:21:43 -05:00
2025-12-18 03:04:11 -05:00
data = r.text.splitlines()
2025-12-08 13:21:43 -05:00
for i, line in enumerate(data, start=1):
if line.startswith("#EXTINF"):
tvg_id_match = re.search(r'tvg-id="([^"]*)"', line)
2025-12-18 04:14:54 -05:00
2025-12-08 13:21:43 -05:00
tvg_name_match = re.search(r'tvg-name="([^"]*)"', line)
2025-12-18 04:14:54 -05:00
2025-12-08 13:21:43 -05:00
group_title_match = re.search(r'group-title="([^"]*)"', line)
tvg = tvg_id_match[1] if tvg_id_match else None
if not tvg and (url := data[i]).endswith("/sd"):
if tvg_name := tvg_name_match[1]:
sport = group_title_match[1].upper().strip()
event = "(".join(tvg_name.split("(")[:-1]).strip()
key = f"[{sport}] {event} ({TAG})"
channel = url.split("/")[-2]
tvg_id, logo = leagues.info(sport)
events[key] = {
"url": f"http://origin.thetvapp.to/hls/{channel}/mono.m3u8",
"logo": logo,
"id": tvg_id or "Live.Event.us",
"base": "https://tvpass.org",
2025-12-18 03:04:11 -05:00
"timestamp": now.timestamp(),
2025-12-08 13:21:43 -05:00
}
return events
2025-12-18 03:04:11 -05:00
async def scrape() -> None:
2025-12-08 13:21:43 -05:00
if cached := CACHE_FILE.load():
urls.update(cached)
2025-12-18 04:14:54 -05:00
2025-12-08 13:21:43 -05:00
log.info(f"Loaded {len(urls)} event(s) from cache")
2025-12-18 04:14:54 -05:00
2025-12-08 13:21:43 -05:00
return
log.info(f'Scraping from "{BASE_URL}"')
2025-12-18 03:04:11 -05:00
urls.update(await get_events())
2025-12-08 13:21:43 -05:00
CACHE_FILE.write(urls)
log.info(f"Collected and cached {len(urls)} new event(s)")