This commit is contained in:
doms9 2025-10-01 11:57:49 -04:00
parent 7103b0f1c4
commit 00000d9937
17 changed files with 597 additions and 524 deletions

View file

@ -3,15 +3,15 @@ from pathlib import Path
import httpx
from .utils import get_logger, leagues, load_cache, now, write_cache
from .utils import Cache, Time, get_logger, leagues
log = get_logger(__name__)
log = get_logger()
urls: dict[str, dict[str, str]] = {}
BASE_URL = "https://tvpass.org/playlist/m3u"
CACHE_FILE = Path(__file__).parent / "caches" / "tvpass.json"
CACHE_FILE = Cache(Path(__file__).parent / "caches" / "tvpass.json", exp=86_400)
async def fetch_m3u8(client: httpx.AsyncClient) -> list[str]:
@ -19,21 +19,25 @@ async def fetch_m3u8(client: httpx.AsyncClient) -> list[str]:
r = await client.get(BASE_URL)
r.raise_for_status()
except Exception as e:
log.error(f'Failed to fetch "{BASE_URL}"\n{e}')
log.error(f'Failed to fetch "{BASE_URL}": {e}')
return []
return r.text.splitlines()
async def scrape(client: httpx.AsyncClient) -> None:
if cached := load_cache(CACHE_FILE, exp=86_400, nearest_hr=True):
if cached := CACHE_FILE.load(nearest_hr=True):
urls.update(cached)
log.info(f"Collected {len(urls)} event(s) from cache")
log.info(f"Loaded {len(urls)} event(s) from cache")
return
log.info(f'Scraping from "{BASE_URL}"')
for i, line in enumerate(data := await fetch_m3u8(client)):
if not (data := await fetch_m3u8(client)):
log.warning("No M3U8 data received")
return
for i, line in enumerate(data):
if line.startswith("#EXTINF"):
tvg_id_match = re.search(r'tvg-id="([^"]*)"', line)
tvg_name_match = re.search(r'tvg-name="([^"]*)"', line)
@ -56,13 +60,13 @@ async def scrape(client: httpx.AsyncClient) -> None:
entry = {
"url": f"http://origin.thetvapp.to/hls/{channel}/mono.m3u8",
"logo": logo,
"id": tvg_id,
"id": tvg_id or "Live.Event.us",
"base": "https://tvpass.org",
"timestamp": now.timestamp(),
"timestamp": Time.now().timestamp(),
}
urls[key] = entry
write_cache(CACHE_FILE, urls)
CACHE_FILE.write(urls)
log.info(f"Cached {len(urls)} event(s)")