2025-12-13 21:29:13 -05:00
|
|
|
import asyncio
|
2025-12-13 16:57:14 -05:00
|
|
|
from functools import partial
|
2026-04-17 13:09:13 -04:00
|
|
|
from urllib.parse import urljoin
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
from playwright.async_api import Browser, Page, TimeoutError
|
2025-12-13 16:57:14 -05:00
|
|
|
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"
|
|
|
|
|
|
2026-01-23 23:44:59 -05:00
|
|
|
CACHE_FILE = Cache(TAG, exp=10_800)
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2026-04-07 11:26:01 -04:00
|
|
|
HTML_FILE = Cache(f"{TAG}-html", exp=19_800)
|
2025-12-15 02:06:46 -05:00
|
|
|
|
2026-04-04 17:01:58 -04:00
|
|
|
BASE_URL = "https://livesports4u.net"
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2026-03-04 18:15:39 -05:00
|
|
|
SPORT_ENDPOINTS = [
|
|
|
|
|
f"sport_{sport_id}"
|
|
|
|
|
for sport_id in [
|
|
|
|
|
# "68c02a4465113", # American Football
|
2026-03-07 11:52:59 -05:00
|
|
|
# "68c02a446582f", # Baseball
|
2026-03-04 18:15:39 -05:00
|
|
|
"68c02a4466011", # Basketball
|
2026-04-04 17:01:58 -04:00
|
|
|
"68c02a4466f56", # Hockey
|
2026-03-07 11:52:59 -05:00
|
|
|
# "68c02a44674e9", # MMA
|
|
|
|
|
# "68c02a4467a48", # Racing
|
2026-03-04 18:15:39 -05:00
|
|
|
"68c02a4464a38", # Soccer
|
2026-03-07 11:52:59 -05:00
|
|
|
# "68c02a4468cf7", # Tennis
|
2026-03-04 18:15:39 -05:00
|
|
|
]
|
|
|
|
|
]
|
2025-12-13 21:29:13 -05:00
|
|
|
|
|
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
async def process_event(
|
|
|
|
|
url: str,
|
|
|
|
|
url_num: int,
|
|
|
|
|
page: Page,
|
|
|
|
|
) -> str | None:
|
2026-04-05 17:26:17 -04:00
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
captured: list[str] = []
|
2026-04-05 17:26:17 -04:00
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
got_one = asyncio.Event()
|
2026-04-05 17:26:17 -04:00
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
handler = partial(
|
|
|
|
|
network.capture_req,
|
|
|
|
|
captured=captured,
|
|
|
|
|
got_one=got_one,
|
|
|
|
|
)
|
2026-04-05 17:26:17 -04:00
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
page.on("request", handler)
|
2026-04-05 17:26:17 -04:00
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
try:
|
|
|
|
|
resp = await page.goto(
|
|
|
|
|
url,
|
|
|
|
|
wait_until="domcontentloaded",
|
|
|
|
|
timeout=6_000,
|
|
|
|
|
)
|
2026-04-05 17:26:17 -04:00
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
if not resp or resp.status != 200:
|
|
|
|
|
log.warning(
|
|
|
|
|
f"URL {url_num}) Status Code: {resp.status if resp else 'None'}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
btn = page.locator("button.btn.btn-sm.btn-success.streamLink")
|
|
|
|
|
|
|
|
|
|
iframe_src = await btn.get_attribute("data-src", timeout=1_250)
|
|
|
|
|
except TimeoutError:
|
|
|
|
|
log.warning(f"URL {url_num}) No iframe source found.")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
await page.goto(
|
|
|
|
|
iframe_src,
|
|
|
|
|
wait_until="domcontentloaded",
|
|
|
|
|
timeout=5_000,
|
2026-04-05 17:26:17 -04:00
|
|
|
)
|
|
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
wait_task = asyncio.create_task(got_one.wait())
|
2026-04-05 17:26:17 -04:00
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
try:
|
2026-04-18 13:31:31 -04:00
|
|
|
await asyncio.wait_for(wait_task, timeout=5)
|
2026-04-17 13:09:13 -04:00
|
|
|
except asyncio.TimeoutError:
|
|
|
|
|
log.warning(f"URL {url_num}) Timed out waiting for M3U8.")
|
2026-04-05 17:26:17 -04:00
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
return
|
2026-04-05 17:26:17 -04:00
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
finally:
|
|
|
|
|
if not wait_task.done():
|
|
|
|
|
wait_task.cancel()
|
2026-04-05 17:26:17 -04:00
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
try:
|
|
|
|
|
await wait_task
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
if captured:
|
|
|
|
|
log.info(f"URL {url_num}) Captured M3U8")
|
|
|
|
|
|
|
|
|
|
return captured[0]
|
2026-04-05 17:26:17 -04:00
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
log.warning(f"URL {url_num}) No M3U8 captured after waiting.")
|
2026-04-05 17:26:17 -04:00
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
except Exception as e:
|
|
|
|
|
log.warning(f"URL {url_num}) {e}")
|
2026-04-05 17:26:17 -04:00
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
page.remove_listener("request", handler)
|
2026-04-05 17:26:17 -04:00
|
|
|
|
|
|
|
|
|
2025-12-15 02:06:46 -05:00
|
|
|
async def refresh_html_cache(
|
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]]:
|
2026-02-11 23:22:53 -05:00
|
|
|
|
2025-12-18 03:04:11 -05:00
|
|
|
events = {}
|
2025-12-13 21:29:13 -05:00
|
|
|
|
2025-12-18 03:04:11 -05:00
|
|
|
if not (
|
|
|
|
|
html_data := await network.request(
|
2026-02-18 15:47:50 -05:00
|
|
|
urljoin(BASE_URL, f"events/{date}"),
|
2025-12-18 03:04:11 -05:00
|
|
|
log=log,
|
|
|
|
|
params={"sport_id": sport_id},
|
|
|
|
|
)
|
|
|
|
|
):
|
|
|
|
|
return events
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2025-12-18 03:04:11 -05:00
|
|
|
soup = HTMLParser(html_data.content)
|
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
|
|
|
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,
|
|
|
|
|
"event_ts": event_dt.timestamp(),
|
2026-01-01 00:09:43 -05:00
|
|
|
"timestamp": ts,
|
2025-12-15 02:06:46 -05:00
|
|
|
}
|
2025-12-13 16:57:14 -05:00
|
|
|
|
|
|
|
|
return events
|
|
|
|
|
|
|
|
|
|
|
2026-02-18 15:47:50 -05:00
|
|
|
async def get_events(cached_keys: list[str]) -> list[dict[str, str]]:
|
2025-12-15 02:06:46 -05:00
|
|
|
now = Time.clean(Time.now())
|
|
|
|
|
|
2026-04-07 11:26:01 -04:00
|
|
|
if not (events := HTML_FILE.load()):
|
2025-12-15 02:06:46 -05:00
|
|
|
log.info("Refreshing HTML cache")
|
|
|
|
|
|
|
|
|
|
tasks = [
|
|
|
|
|
refresh_html_cache(
|
2025-12-15 15:53:36 -05:00
|
|
|
date,
|
2025-12-15 02:06:46 -05:00
|
|
|
sport_id,
|
|
|
|
|
now.timestamp(),
|
|
|
|
|
)
|
2025-12-18 03:04:11 -05:00
|
|
|
for date in [now.date(), now.delta(days=1).date()]
|
2026-03-07 11:52:59 -05:00
|
|
|
for sport_id in SPORT_ENDPOINTS
|
2025-12-15 02:06:46 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
results = await asyncio.gather(*tasks)
|
|
|
|
|
|
|
|
|
|
events = {k: v for data in results for k, v in data.items()}
|
|
|
|
|
|
2026-04-07 11:26:01 -04:00
|
|
|
HTML_FILE.write(events)
|
2025-12-15 02:06:46 -05:00
|
|
|
|
|
|
|
|
live = []
|
|
|
|
|
|
2026-04-05 17:26:17 -04:00
|
|
|
start_ts = now.delta(minutes=-30).timestamp()
|
|
|
|
|
end_ts = now.delta(minutes=30).timestamp()
|
2025-12-15 02:06:46 -05:00
|
|
|
|
|
|
|
|
for k, v in events.items():
|
2025-12-18 03:04:11 -05:00
|
|
|
if k in cached_keys:
|
2025-12-15 02:06:46 -05:00
|
|
|
continue
|
|
|
|
|
|
2025-12-16 14:27:05 -05:00
|
|
|
if not start_ts <= v["event_ts"] <= end_ts:
|
2025-12-15 02:06:46 -05:00
|
|
|
continue
|
|
|
|
|
|
2026-02-12 18:15:01 -05:00
|
|
|
live.append(v)
|
2025-12-15 02:06:46 -05:00
|
|
|
|
|
|
|
|
return live
|
|
|
|
|
|
|
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
async def scrape(browser: Browser) -> None:
|
2025-12-13 16:57:14 -05:00
|
|
|
cached_urls = CACHE_FILE.load()
|
2025-12-18 04:14:54 -05:00
|
|
|
|
2026-01-02 15:42:36 -05:00
|
|
|
valid_urls = {k: v for k, v in cached_urls.items() if v["url"]}
|
2025-12-18 04:14:54 -05:00
|
|
|
|
2026-01-02 15:42:36 -05:00
|
|
|
valid_count = cached_count = len(valid_urls)
|
|
|
|
|
|
|
|
|
|
urls.update(valid_urls)
|
2025-12-13 16:57:14 -05:00
|
|
|
|
|
|
|
|
log.info(f"Loaded {cached_count} event(s) from cache")
|
|
|
|
|
|
2026-02-18 15:47:50 -05:00
|
|
|
log.info(f'Scraping from "{BASE_URL}"')
|
2026-02-11 23:22:53 -05:00
|
|
|
|
2026-03-02 00:50:28 -05:00
|
|
|
if events := await get_events(cached_urls.keys()):
|
2026-02-19 18:16:27 -05:00
|
|
|
log.info(f"Processing {len(events)} new URL(s)")
|
2026-04-18 13:53:53 -04:00
|
|
|
|
2026-04-17 13:09:13 -04:00
|
|
|
async with network.event_context(browser) as context:
|
|
|
|
|
for i, ev in enumerate(events, start=1):
|
|
|
|
|
async with network.event_page(context) as page:
|
|
|
|
|
|
|
|
|
|
handler = partial(
|
|
|
|
|
process_event,
|
|
|
|
|
url=(link := ev["link"]),
|
|
|
|
|
url_num=i,
|
|
|
|
|
page=page,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
url = await network.safe_process(
|
|
|
|
|
handler,
|
|
|
|
|
url_num=i,
|
|
|
|
|
semaphore=network.PW_S,
|
|
|
|
|
log=log,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
sport, event, ts = (
|
|
|
|
|
ev["sport"],
|
|
|
|
|
ev["event"],
|
|
|
|
|
ev["event_ts"],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
key = f"[{sport}] {event} ({TAG})"
|
|
|
|
|
|
|
|
|
|
tvg_id, logo = leagues.get_tvg_info(sport, event)
|
|
|
|
|
|
|
|
|
|
entry = {
|
|
|
|
|
"url": url,
|
|
|
|
|
"logo": logo,
|
|
|
|
|
"base": "https://hardsmart.click",
|
|
|
|
|
"timestamp": ts,
|
|
|
|
|
"id": tvg_id or "Live.Event.us",
|
|
|
|
|
"link": link,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cached_urls[key] = entry
|
|
|
|
|
|
|
|
|
|
if url:
|
|
|
|
|
valid_count += 1
|
|
|
|
|
|
|
|
|
|
entry["url"] = url.split("?st")[0]
|
|
|
|
|
|
|
|
|
|
urls[key] = entry
|
2025-12-13 16:57:14 -05:00
|
|
|
|
2026-03-02 00:50:28 -05:00
|
|
|
log.info(f"Collected and cached {valid_count - cached_count} new event(s)")
|
2025-12-18 04:14:54 -05:00
|
|
|
|
2025-12-13 16:57:14 -05:00
|
|
|
else:
|
|
|
|
|
log.info("No new events found")
|
|
|
|
|
|
|
|
|
|
CACHE_FILE.write(cached_urls)
|