From 00000d9543cb7f275ffcc53c31897e8df079e337 Mon Sep 17 00:00:00 2001 From: doms9 <96013514+doms9@users.noreply.github.com> Date: Tue, 28 Oct 2025 15:53:57 -0400 Subject: [PATCH] e --- M3U8/fetch.py | 3 + M3U8/scrapers/strfree.py | 208 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 M3U8/scrapers/strfree.py diff --git a/M3U8/fetch.py b/M3U8/fetch.py index 4323869..3b051bd 100644 --- a/M3U8/fetch.py +++ b/M3U8/fetch.py @@ -8,6 +8,7 @@ from scrapers import ( streambtw, streameast, streamed, + strfree, strmd, tvpass, watchfooty, @@ -42,6 +43,7 @@ async def main() -> None: asyncio.create_task(streambtw.scrape(network.client)), asyncio.create_task(streameast.scrape(network.client)), asyncio.create_task(streamed.scrape(network.client)), + asyncio.create_task(strfree.scrape(network.client)), asyncio.create_task(strmd.scrape(network.client)), asyncio.create_task(tvpass.scrape(network.client)), asyncio.create_task(watchfooty.scrape(network.client)), @@ -55,6 +57,7 @@ async def main() -> None: | streambtw.urls | streameast.urls | streamed.urls + | strfree.urls | strmd.urls | tvpass.urls | watchfooty.urls diff --git a/M3U8/scrapers/strfree.py b/M3U8/scrapers/strfree.py new file mode 100644 index 0000000..c0fd960 --- /dev/null +++ b/M3U8/scrapers/strfree.py @@ -0,0 +1,208 @@ +import asyncio +from functools import partial +from pathlib import Path +from urllib.parse import urljoin + +import httpx +from playwright.async_api import BrowserContext, async_playwright + +from .utils import Cache, Time, get_logger, leagues, network + +log = get_logger(__name__) + +urls: dict[str, dict[str, str | float]] = {} + +API_FILE = Cache(Path(__file__).parent / "caches" / "strmfree_api.json", exp=86_400) + +CACHE_FILE = Cache(Path(__file__).parent / "caches" / "strmfree.json", exp=10_800) + +BASE_URL = "https://streamfree.to" + + +async def refresh_api_cache( + client: httpx.AsyncClient, + url: str, +) -> dict[str, dict[str, list]]: + log.info("Refreshing API cache") + + try: + r = await client.get(url) + r.raise_for_status() + except Exception as e: + log.error(f'Failed to fetch "{url}": {e}') + return {} + + data = r.json() + + data["timestamp"] = Time.now().timestamp() + + return data + + +async def process_event( + url: str, + url_num: int, + context: BrowserContext, +) -> str | None: + + page = await context.new_page() + + captured: list[str] = [] + + got_one = asyncio.Event() + + handler = partial(network.capture_req, captured=captured, got_one=got_one) + + page.on("request", handler) + + try: + await page.goto( + url, + wait_until="domcontentloaded", + timeout=15_000, + ) + + wait_task = asyncio.create_task(got_one.wait()) + + try: + await asyncio.wait_for(wait_task, timeout=6) + except asyncio.TimeoutError: + log.warning(f"URL {url_num}) Timed out waiting for M3U8.") + return + + finally: + if not wait_task.done(): + wait_task.cancel() + + try: + await wait_task + except asyncio.CancelledError: + pass + + if captured: + log.info(f"URL {url_num}) Captured M3U8") + return captured[-1] + + log.warning(f"URL {url_num}) No M3U8 captured after waiting.") + return + + except Exception as e: + log.warning(f"URL {url_num}) Exception while processing: {e}") + return + + finally: + page.remove_listener("request", handler) + await page.close() + + +async def get_events( + client: httpx.AsyncClient, + url: str, + cached_keys: set[str], +) -> list[dict[str, str]]: + + if not (api_data := API_FILE.load(per_entry=False)): + api_data = await refresh_api_cache( + client, + urljoin(url, "streams"), + ) + + API_FILE.write(api_data) + + events: list[dict[str, str]] = [] + + now = Time.clean(Time.now()) + start_dt = now.delta(minutes=-30) + end_dt = now.delta(minutes=30) + + for category, streams in api_data["streams"].items(): + if not streams: + continue + + sport = "American Football" if category == "football" else category.capitalize() + + for stream in streams: + event_dt = Time.from_ts(stream["match_timestamp"]) + + if not start_dt <= event_dt <= end_dt: + continue + + name = stream["name"] + + key = f"[{sport}] {name} (STRFREE)" + + if cached_keys & {key}: + continue + + stream_url = stream["stream_key"] + + stream_logo = stream["thumbnail_url"] + + events.append( + { + "sport": sport, + "event": name, + "link": urljoin(url, f"player/{category}/{stream_url}"), + "logo": urljoin(url, stream_logo), + "timestamp": event_dt.timestamp(), + } + ) + + return events + + +async def scrape(client: httpx.AsyncClient) -> None: + cached_urls = CACHE_FILE.load() + cached_count = len(cached_urls) + urls.update(cached_urls) + + log.info(f"Loaded {cached_count} event(s) from cache") + + log.info(f'Scraping from "{BASE_URL}"') + + events = await get_events( + client, + BASE_URL, + set(cached_urls.keys()), + ) + + log.info(f"Processing {len(events)} new URL(s)") + + async with async_playwright() as p: + browser, context = await network.browser(p) + + for i, ev in enumerate(events, start=1): + handler = partial(process_event, url=ev["link"], url_num=i, context=context) + + url = await network.safe_process(handler, url_num=i, log=log) + + if url: + sport, event, logo, ts = ( + ev["sport"], + ev["event"], + ev["logo"], + ev["timestamp"], + ) + + key = f"[{sport}] {event} (STRFREE)" + + tvg_id, pic = leagues.get_tvg_info(sport, event) + + entry = { + "url": url, + "logo": logo or pic, + "base": "", + "timestamp": ts, + "id": tvg_id or "Live.Event.us", + } + + urls[key] = cached_urls[key] = entry + + await browser.close() + + if new_count := len(cached_urls) - cached_count: + log.info(f"Collected and cached {new_count} new event(s)") + else: + log.info("No new events found") + + CACHE_FILE.write(cached_urls)