iptv/M3U8/scrapers/streamhub.py

169 lines
4.6 KiB
Python
Raw Normal View History

2025-12-13 21:29:13 -05:00
import asyncio
2025-12-13 16:57:14 -05:00
from functools import partial
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)
BASE_URL = "https://streamhub.pro/live-now"
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",
}
async def get_html_data(client: httpx.AsyncClient, sport: str) -> bytes:
2025-12-13 16:57:14 -05:00
try:
2025-12-13 21:29:13 -05:00
r = await client.get(BASE_URL, params={"sport_id": sport})
2025-12-13 16:57:14 -05:00
r.raise_for_status()
except Exception as e:
log.error(f'Failed to fetch "{BASE_URL}": {e}')
2025-12-13 21:29:13 -05:00
return b""
return r.content
async def get_events(
client: httpx.AsyncClient, cached_keys: set[str]
) -> list[dict[str, str]]:
tasks = [get_html_data(client, sport) for sport in CATEGORIES.values()]
results = await asyncio.gather(*tasks)
2025-12-13 16:57:14 -05:00
2025-12-13 21:29:13 -05:00
soups = [HTMLParser(html) for html in results]
2025-12-13 16:57:14 -05:00
events = []
2025-12-13 21:29:13 -05:00
for soup in soups:
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-13 21:29:13 -05:00
sport = sport_node.text(strip=True)
2025-12-13 16:57:14 -05:00
2025-12-13 21:29:13 -05:00
logo = section.css_first(".league-icon img").attributes.get("src")
2025-12-13 16:57:14 -05:00
2025-12-13 21:29:13 -05:00
for event in section.css(".section-event"):
event_name = "Live Event"
2025-12-13 16:57:14 -05:00
2025-12-13 21:29:13 -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-13 21:29:13 -05:00
event_name = f"{away} vs {home}"
2025-12-13 16:57:14 -05:00
2025-12-13 21:29:13 -05:00
if not (event_button := event.css_first("div.event-button a")) or not (
href := event_button.attributes.get("href")
):
continue
2025-12-13 16:57:14 -05:00
2025-12-13 21:29:13 -05:00
key = f"[{sport}] {event_name} ({TAG})"
2025-12-13 16:57:14 -05:00
2025-12-13 21:29:13 -05:00
if cached_keys & {key}:
continue
2025-12-13 16:57:14 -05:00
2025-12-13 21:29:13 -05:00
events.append(
{
"sport": sport,
"event": event_name,
"link": href,
"logo": logo,
}
)
2025-12-13 16:57:14 -05:00
return events
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:
now = Time.now().timestamp()
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,
)
sport, event, logo, link = (
ev["sport"],
ev["event"],
ev["logo"],
ev["link"],
)
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/",
"timestamp": now,
"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)