2025-12-13 21:29:13 -05:00
|
|
|
import asyncio
|
2025-12-13 16:57:14 -05:00
|
|
|
from functools import partial
|
2025-12-15 02:06:46 -05:00
|
|
|
from urllib.parse import urljoin
|
2025-12-13 16:57:14 -05:00
|
|
|
|
|
|
|
|
import httpx
|
|
|
|
|
from playwright.async_api import async_playwright
|
|
|
|
|
from selectolax.parser import HTMLParser
|
|
|
|
|
|
|
|
|
|
from .utils import Cache, Time, get_logger, leagues, network
|
|
|
|
|
|
|
|
|
|
log = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
urls: dict[str, dict[str, str | float]] = {}
|
|
|
|
|
|
|
|
|
|
TAG = "STRMHUB"
|
|
|
|
|
|
|
|
|
|
CACHE_FILE = Cache(f"{TAG.lower()}.json", exp=10_800)
|
|
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
HTML_CACHE = Cache(f"{TAG.lower()}-html.json", exp=28_800)
|
|
|
|
|
|
|
|
|
|
BASE_URL = "https://streamhub.pro/"
|
2025-12-13 16:57:14 -05:00
|
|
|
|
|
|
|
|
|
2025-12-13 21:29:13 -05:00
|
|
|
CATEGORIES = {
|
|
|
|
|
"Soccer": "sport_68c02a4464a38",
|
|
|
|
|
"American Football": "sport_68c02a4465113",
|
|
|
|
|
# "Baseball": "sport_68c02a446582f",
|
|
|
|
|
"Basketball": "sport_68c02a4466011",
|
|
|
|
|
# "Cricket": "sport_68c02a44669f3",
|
|
|
|
|
"Hockey": "sport_68c02a4466f56",
|
|
|
|
|
"MMA": "sport_68c02a44674e9",
|
|
|
|
|
"Racing": "sport_68c02a4467a48",
|
|
|
|
|
# "Rugby": "sport_68c02a4467fc1",
|
|
|
|
|
# "Tennis": "sport_68c02a4468cf7",
|
|
|
|
|
# "Volleyball": "sport_68c02a4469422",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-12-15 15:53:36 -05:00
|
|
|
async def get_html_data(
|
|
|
|
|
client: httpx.AsyncClient,
|
|
|
|
|
date: str,
|
|
|
|
|
sport_id: str,
|
|
|
|
|
) -> bytes:
|
2025-12-15 02:06:46 -05:00
|
|
|
|
2025-12-15 15:53:36 -05:00
|
|
|
try:
|
|
|
|
|
r = await client.get(
|
|
|
|
|
urljoin(BASE_URL, f"events/{date}"),
|
|
|
|
|
params={"sport_id": sport_id},
|
|
|
|
|
)
|
2025-12-15 02:06:46 -05:00
|
|
|
|
2025-12-13 16:57:14 -05:00
|
|
|
r.raise_for_status()
|
|
|
|
|
except Exception as e:
|
2025-12-15 15:53:36 -05:00
|
|
|
log.error(f'Failed to fetch "{r.url}": {e}')
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2025-12-13 21:29:13 -05:00
|
|
|
return b""
|
|
|
|
|
|
|
|
|
|
return r.content
|
|
|
|
|
|
|
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
async def refresh_html_cache(
|
|
|
|
|
client: httpx.AsyncClient,
|
2025-12-15 15:53:36 -05:00
|
|
|
date: str,
|
2025-12-15 02:06:46 -05:00
|
|
|
sport_id: str,
|
|
|
|
|
ts: float,
|
|
|
|
|
) -> dict[str, dict[str, str | float]]:
|
2025-12-13 21:29:13 -05:00
|
|
|
|
2025-12-15 15:53:36 -05:00
|
|
|
html_data = await get_html_data(client, date, sport_id)
|
2025-12-13 21:29:13 -05:00
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
soup = HTMLParser(html_data)
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
events = {}
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
for section in soup.css(".events-section"):
|
|
|
|
|
if not (sport_node := section.css_first(".section-titlte")):
|
|
|
|
|
continue
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
sport = sport_node.text(strip=True)
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
logo = section.css_first(".league-icon img").attributes.get("src")
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
for event in section.css(".section-event"):
|
|
|
|
|
event_name = "Live Event"
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
if teams := event.css_first(".event-competitors"):
|
|
|
|
|
home, away = teams.text(strip=True).split("vs.")
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
event_name = f"{away} vs {home}"
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
if not (event_button := event.css_first(".event-button a")) or not (
|
|
|
|
|
href := event_button.attributes.get("href")
|
|
|
|
|
):
|
|
|
|
|
continue
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
event_date = event.css_first(".event-countdown").attributes.get(
|
|
|
|
|
"data-start"
|
|
|
|
|
)
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
event_dt = Time.from_str(event_date, timezone="UTC")
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
key = f"[{sport}] {event_name} ({TAG})"
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
events[key] = {
|
|
|
|
|
"sport": sport,
|
|
|
|
|
"event": event_name,
|
|
|
|
|
"link": href,
|
|
|
|
|
"logo": logo,
|
|
|
|
|
"timestamp": ts,
|
|
|
|
|
"event_ts": event_dt.timestamp(),
|
|
|
|
|
}
|
2025-12-13 16:57:14 -05:00
|
|
|
|
|
|
|
|
return events
|
|
|
|
|
|
|
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
async def get_events(
|
|
|
|
|
client: httpx.AsyncClient,
|
|
|
|
|
cached_keys: set[str],
|
|
|
|
|
) -> list[dict[str, str]]:
|
|
|
|
|
now = Time.clean(Time.now())
|
|
|
|
|
|
|
|
|
|
if not (events := HTML_CACHE.load()):
|
|
|
|
|
log.info("Refreshing HTML cache")
|
|
|
|
|
|
2025-12-15 15:53:36 -05:00
|
|
|
dates = [now.date(), now.delta(days=1).date()]
|
|
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
tasks = [
|
|
|
|
|
refresh_html_cache(
|
|
|
|
|
client,
|
2025-12-15 15:53:36 -05:00
|
|
|
date,
|
2025-12-15 02:06:46 -05:00
|
|
|
sport_id,
|
|
|
|
|
now.timestamp(),
|
|
|
|
|
)
|
2025-12-15 15:53:36 -05:00
|
|
|
for date in dates
|
2025-12-15 02:06:46 -05:00
|
|
|
for sport_id in CATEGORIES.values()
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
results = await asyncio.gather(*tasks)
|
|
|
|
|
|
|
|
|
|
events = {k: v for data in results for k, v in data.items()}
|
|
|
|
|
|
|
|
|
|
HTML_CACHE.write(events)
|
|
|
|
|
|
|
|
|
|
live = []
|
|
|
|
|
|
|
|
|
|
start_ts = now.delta(hours=-1).timestamp()
|
|
|
|
|
end_ts = now.delta(minutes=5).timestamp()
|
|
|
|
|
|
|
|
|
|
for k, v in events.items():
|
|
|
|
|
if cached_keys & {k}:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if not start_ts <= v["event_ts"] <= end_ts:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
live.append({**v})
|
|
|
|
|
|
|
|
|
|
return live
|
|
|
|
|
|
|
|
|
|
|
2025-12-13 16:57:14 -05:00
|
|
|
async def scrape(client: httpx.AsyncClient) -> None:
|
|
|
|
|
cached_urls = CACHE_FILE.load()
|
|
|
|
|
valid_urls = {k: v for k, v in cached_urls.items() if v["url"]}
|
|
|
|
|
valid_count = cached_count = len(valid_urls)
|
|
|
|
|
urls.update(valid_urls)
|
|
|
|
|
|
|
|
|
|
log.info(f"Loaded {cached_count} event(s) from cache")
|
|
|
|
|
|
|
|
|
|
log.info(f'Scraping from "{BASE_URL}"')
|
|
|
|
|
|
|
|
|
|
events = await get_events(client, set(cached_urls.keys()))
|
|
|
|
|
|
|
|
|
|
log.info(f"Processing {len(events)} new URL(s)")
|
|
|
|
|
|
|
|
|
|
if events:
|
|
|
|
|
async with async_playwright() as p:
|
|
|
|
|
browser, context = await network.browser(p)
|
|
|
|
|
|
|
|
|
|
for i, ev in enumerate(events, start=1):
|
|
|
|
|
handler = partial(
|
|
|
|
|
network.process_event,
|
|
|
|
|
url=ev["link"],
|
|
|
|
|
url_num=i,
|
|
|
|
|
context=context,
|
|
|
|
|
timeout=5,
|
|
|
|
|
log=log,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
url = await network.safe_process(
|
|
|
|
|
handler,
|
|
|
|
|
url_num=i,
|
|
|
|
|
log=log,
|
|
|
|
|
)
|
|
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
sport, event, logo, link, ts = (
|
2025-12-13 16:57:14 -05:00
|
|
|
ev["sport"],
|
|
|
|
|
ev["event"],
|
|
|
|
|
ev["logo"],
|
|
|
|
|
ev["link"],
|
2025-12-15 15:53:36 -05:00
|
|
|
ev["event_ts"],
|
2025-12-13 16:57:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
key = f"[{sport}] {event} ({TAG})"
|
|
|
|
|
|
|
|
|
|
tvg_id, pic = leagues.get_tvg_info(sport, event)
|
|
|
|
|
|
|
|
|
|
entry = {
|
|
|
|
|
"url": url,
|
|
|
|
|
"logo": logo or pic,
|
|
|
|
|
"base": "https://storytrench.net/",
|
2025-12-15 02:06:46 -05:00
|
|
|
"timestamp": ts,
|
2025-12-13 16:57:14 -05:00
|
|
|
"id": tvg_id or "Live.Event.us",
|
|
|
|
|
"link": link,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cached_urls[key] = entry
|
|
|
|
|
|
|
|
|
|
if url:
|
|
|
|
|
valid_count += 1
|
|
|
|
|
urls[key] = entry
|
|
|
|
|
|
|
|
|
|
await browser.close()
|
|
|
|
|
|
|
|
|
|
if new_count := valid_count - 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)
|