2025-09-30 17:27:42 -04:00
|
|
|
import asyncio
|
2025-09-30 17:36:59 -04:00
|
|
|
import re
|
2025-09-30 17:27:42 -04:00
|
|
|
from functools import partial
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import httpx
|
2025-10-09 20:18:37 -04:00
|
|
|
from playwright.async_api import BrowserContext, async_playwright
|
2025-09-30 17:27:42 -04:00
|
|
|
from selectolax.parser import HTMLParser
|
|
|
|
|
|
2025-10-01 11:57:49 -04:00
|
|
|
from .utils import Cache, Time, get_logger, leagues, network
|
2025-09-30 17:27:42 -04:00
|
|
|
|
|
|
|
|
log = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
urls: dict[str, dict[str, str | float]] = {}
|
|
|
|
|
|
2025-10-01 11:57:49 -04:00
|
|
|
CACHE_FILE = Cache(Path(__file__).parent / "caches" / "streamed.json", exp=10_800)
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-01 11:57:49 -04:00
|
|
|
HTML_CACHE = Cache(Path(__file__).parent / "caches" / "streamed_php.json", exp=86_400)
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-01 11:57:49 -04:00
|
|
|
BASE_URL = "https://streamed.site/webmaster.php"
|
2025-09-30 17:27:42 -04:00
|
|
|
|
|
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
async def process_event(
|
|
|
|
|
url: str,
|
|
|
|
|
url_num: int,
|
|
|
|
|
context: BrowserContext,
|
|
|
|
|
) -> str | None:
|
|
|
|
|
page = await context.new_page()
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
captured: list[str] = []
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
got_one = asyncio.Event()
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
handler = partial(network.capture_req, captured=captured, got_one=got_one)
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
page.on("request", handler)
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
try:
|
|
|
|
|
await page.goto(url, wait_until="domcontentloaded", timeout=15_000)
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
wait_task = asyncio.create_task(got_one.wait())
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
try:
|
|
|
|
|
await asyncio.wait_for(wait_task, timeout=10)
|
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
|
log.warning(f"URL {url_num}) Timed out waiting for M3U8.")
|
|
|
|
|
return
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
finally:
|
|
|
|
|
if not wait_task.done():
|
|
|
|
|
wait_task.cancel()
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
try:
|
|
|
|
|
await wait_task
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
pass
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
if captured:
|
|
|
|
|
log.info(f"URL {url_num}) Captured M3U8")
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
return captured[-1]
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
log.warning(f"URL {url_num}) No M3U8 captured after waiting.")
|
|
|
|
|
return
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
except Exception as e:
|
|
|
|
|
log.warning(f"URL {url_num}) Exception while processing: {e}")
|
|
|
|
|
return
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
finally:
|
|
|
|
|
page.remove_listener("request", handler)
|
|
|
|
|
await page.close()
|
2025-09-30 17:27:42 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def refresh_html_cache(client: httpx.AsyncClient, url: str) -> dict[str, str]:
|
|
|
|
|
try:
|
|
|
|
|
r = await client.get(url)
|
|
|
|
|
r.raise_for_status()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
log.error(f'Failed to fetch "{url}"\n{e}')
|
|
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
soup = HTMLParser(r.text)
|
|
|
|
|
events = {}
|
2025-10-01 14:56:15 -04:00
|
|
|
now = Time.now()
|
2025-09-30 17:27:42 -04:00
|
|
|
|
|
|
|
|
for row in soup.css("div.wrap div.row"):
|
|
|
|
|
if not (date := row.css_first("div.date")):
|
|
|
|
|
continue
|
|
|
|
|
|
2025-10-01 14:56:15 -04:00
|
|
|
event_dt = Time.from_str(date.text(strip=True))
|
2025-09-30 17:27:42 -04:00
|
|
|
|
|
|
|
|
if event_dt.date() != now.date():
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
league = row.css_first("div.league")
|
|
|
|
|
|
|
|
|
|
title = row.css_first("div.title")
|
|
|
|
|
|
|
|
|
|
hds_a = row.css_first("div.hds a")
|
|
|
|
|
|
|
|
|
|
if not (league and title and hds_a):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
sport, event = league.text(strip=True), title.text(strip=True)
|
|
|
|
|
|
|
|
|
|
onclick = hds_a.attributes.get("onclick", "")
|
|
|
|
|
|
2025-09-30 17:36:59 -04:00
|
|
|
if not (m := re.search(r"openPlayerPopup\(\s*(\d+)\s*\)", onclick)):
|
2025-09-30 17:27:42 -04:00
|
|
|
continue
|
|
|
|
|
|
2025-10-10 17:14:32 -04:00
|
|
|
key = f"[{sport}] {event} (STRMED)"
|
2025-09-30 17:27:42 -04:00
|
|
|
|
|
|
|
|
events[key] = {
|
|
|
|
|
"sport": sport,
|
|
|
|
|
"event": event,
|
2025-09-30 17:36:59 -04:00
|
|
|
"link": f"https://streamed.site/set.php?{m[1]}",
|
2025-09-30 18:11:17 -04:00
|
|
|
"timestamp": now.timestamp(),
|
2025-10-01 11:57:49 -04:00
|
|
|
"event_ts": event_dt.timestamp(),
|
2025-09-30 17:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return events
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_events(
|
|
|
|
|
client: httpx.AsyncClient,
|
|
|
|
|
url: str,
|
|
|
|
|
cached_keys: set[str],
|
|
|
|
|
) -> list[dict[str, str]]:
|
|
|
|
|
|
2025-10-01 22:28:01 -04:00
|
|
|
if not (events := HTML_CACHE.load()):
|
2025-09-30 17:27:42 -04:00
|
|
|
events = await refresh_html_cache(client, url)
|
2025-10-01 11:57:49 -04:00
|
|
|
HTML_CACHE.write(events)
|
2025-09-30 17:27:42 -04:00
|
|
|
|
|
|
|
|
live = []
|
2025-10-01 11:57:49 -04:00
|
|
|
|
2025-10-02 12:57:25 -04:00
|
|
|
now = Time.clean(Time.now())
|
2025-10-01 14:56:15 -04:00
|
|
|
start_ts = now.delta(minutes=-30).timestamp()
|
|
|
|
|
end_ts = now.delta(minutes=30).timestamp()
|
2025-09-30 17:27:42 -04:00
|
|
|
|
|
|
|
|
for k, v in events.items():
|
|
|
|
|
if cached_keys & {k}:
|
|
|
|
|
continue
|
|
|
|
|
|
2025-10-04 15:17:00 -04:00
|
|
|
if not start_ts <= v["event_ts"] <= end_ts:
|
2025-09-30 17:27:42 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
live.append({**v})
|
|
|
|
|
|
|
|
|
|
return live
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def scrape(client: httpx.AsyncClient) -> None:
|
2025-10-01 11:57:49 -04:00
|
|
|
cached_urls = CACHE_FILE.load()
|
2025-09-30 17:27:42 -04:00
|
|
|
cached_count = len(cached_urls)
|
|
|
|
|
urls.update(cached_urls)
|
|
|
|
|
|
2025-10-01 11:57:49 -04:00
|
|
|
log.info(f"Loaded {cached_count} event(s) from cache")
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-01 11:57:49 -04:00
|
|
|
log.info(f'Scraping from "{BASE_URL}"')
|
2025-09-30 17:27:42 -04:00
|
|
|
|
|
|
|
|
events = await get_events(
|
|
|
|
|
client,
|
2025-10-01 11:57:49 -04:00
|
|
|
BASE_URL,
|
2025-09-30 17:27:42 -04:00
|
|
|
set(cached_urls.keys()),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
log.info(f"Processing {len(events)} new URL(s)")
|
|
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
async with async_playwright() as p:
|
|
|
|
|
browser, context = await network.browser(p, browser="brave")
|
|
|
|
|
|
|
|
|
|
for i, ev in enumerate(events, start=1):
|
|
|
|
|
url = await network.safe_process(
|
|
|
|
|
lambda: process_event(
|
|
|
|
|
ev["link"],
|
|
|
|
|
url_num=i,
|
|
|
|
|
context=context,
|
|
|
|
|
),
|
|
|
|
|
url_num=i,
|
|
|
|
|
log=log,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if url:
|
|
|
|
|
sport, event, ts = ev["sport"], ev["event"], ev["event_ts"]
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
tvg_id, logo = leagues.info(sport)
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
key = f"[{sport}] {event} (STRMD)"
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
entry = {
|
|
|
|
|
"url": url,
|
|
|
|
|
"logo": logo,
|
|
|
|
|
"base": "",
|
|
|
|
|
"timestamp": ts,
|
|
|
|
|
"id": tvg_id or "Live.Event.us",
|
|
|
|
|
}
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
urls[key] = cached_urls[key] = entry
|
2025-09-30 17:27:42 -04:00
|
|
|
|
2025-10-09 20:18:37 -04:00
|
|
|
await browser.close()
|
2025-09-30 17:27:42 -04:00
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
2025-10-01 11:57:49 -04:00
|
|
|
CACHE_FILE.write(cached_urls)
|