iptv/M3U8/scrapers/streamsgate.py

178 lines
4.8 KiB
Python
Raw Normal View History

import json
import re
2025-12-08 13:21:43 -05:00
from functools import partial
from typing import Any
from playwright.async_api import Browser
from selectolax.parser import HTMLParser
2025-12-08 13:21:43 -05:00
from .utils import Cache, Time, get_logger, leagues, network
log = get_logger(__name__)
urls: dict[str, dict[str, str | float]] = {}
2025-12-15 21:59:13 -05:00
TAG = "STRMSGATE"
2025-12-13 16:57:14 -05:00
CACHE_FILE = Cache(TAG, exp=10_800)
2025-12-08 13:21:43 -05:00
BASE_URL = "https://streamingon.org/index.php"
2025-12-08 13:21:43 -05:00
async def get_events(cached_keys: list[str]) -> list[dict[str, str]]:
now = Time.clean(Time.now())
2025-12-08 13:21:43 -05:00
events = []
2025-12-08 13:21:43 -05:00
if not (
html_data := await network.request(
BASE_URL,
params={
"sport": "all",
"league": "all",
"sort": "time",
"stream": "available",
"day": "all",
},
log=log,
)
):
return events
2025-12-08 13:21:43 -05:00
link_data_ptrn = re.compile(r"var\s+linkData\s+=\s+({.*?});", re.I | re.S)
2025-12-08 13:21:43 -05:00
if not (match := link_data_ptrn.search(html_data.text)):
log.warning("No `linkData` variable found.")
return events
2025-12-08 13:21:43 -05:00
link_data: dict[str, dict[str, Any]] = json.loads(match[1])
2025-12-08 13:21:43 -05:00
start_dt = now.delta(minutes=-30)
end_dt = now.delta(minutes=30)
2025-12-08 13:21:43 -05:00
soup = HTMLParser(html_data.content)
2025-12-08 13:21:43 -05:00
for body in soup.css(".sport-body"):
if not (date_elem := body.css_first(".date-label")):
continue
2025-12-08 13:21:43 -05:00
event_date = date_elem.text(strip=True)
2026-03-03 19:48:15 -05:00
for card in soup.css(".game-card"):
if not (event_id := card.attributes.get("data-id")):
continue
2025-12-08 13:21:43 -05:00
if not (league_elem := card.css_first(".card-league")):
continue
2025-12-08 13:21:43 -05:00
if not (teams := card.css(".card-teams .card-team-name")):
continue
2025-12-08 13:21:43 -05:00
if not (time_elem := card.css_first(".card-time")):
continue
2025-12-08 13:21:43 -05:00
event_dt = Time.from_str(
f"{event_date} {time_elem.text(strip=True)}",
timezone="CET",
)
2025-12-08 13:21:43 -05:00
if not start_dt <= event_dt <= end_dt:
continue
2025-12-08 13:21:43 -05:00
sport = league_elem.text(strip=True)
2025-12-08 13:21:43 -05:00
team_1, team_2 = (team.text(strip=True) for team in teams)
2025-12-18 03:04:11 -05:00
event_name = f"{team_2} vs {team_1}"
2025-12-18 03:04:11 -05:00
if f"[{sport}] {event_name} ({TAG})" in cached_keys:
continue
2025-12-08 13:21:43 -05:00
if not (event_info := link_data.get(event_id)):
continue
2025-12-08 13:21:43 -05:00
if not (stream_links := event_info.get("streamLinks")):
continue
2025-12-08 13:21:43 -05:00
if not (url := stream_links[0].get("url")):
continue
2025-12-08 13:21:43 -05:00
events.append(
{
"sport": sport,
"event": event_name,
"link": url,
"timestamp": now.timestamp(),
}
)
2025-12-08 13:21:43 -05:00
return events
async def scrape(browser: Browser) -> None:
2025-12-08 13:21:43 -05:00
cached_urls = CACHE_FILE.load()
2025-12-18 04:14:54 -05:00
2026-02-28 15:42:50 -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-02-28 15:42:50 -05:00
valid_count = cached_count = len(valid_urls)
urls.update(valid_urls)
2025-12-08 13:21:43 -05:00
log.info(f"Loaded {cached_count} event(s) from cache")
log.info(f'Scraping from "{BASE_URL}"')
2026-03-02 00:50:28 -05:00
if events := await get_events(cached_urls.keys()):
log.info(f"Processing {len(events)} new URL(s)")
2026-01-24 11:50:43 -05:00
async with network.event_context(browser, stealth=False) as context:
for i, ev in enumerate(events, start=1):
async with network.event_page(context) as page:
2025-12-23 13:28:56 -05:00
handler = partial(
network.process_event,
url=(link := ev["link"]),
2025-12-23 13:28:56 -05:00
url_num=i,
page=page,
2025-12-23 13:28:56 -05:00
log=log,
2025-12-16 20:39:52 -05:00
)
2025-12-08 13:21:43 -05:00
2025-12-23 13:28:56 -05:00
url = await network.safe_process(
handler,
url_num=i,
semaphore=network.PW_S,
log=log,
)
2026-02-28 15:42:50 -05:00
sport, event, ts = (
ev["sport"],
ev["event"],
ev["timestamp"],
)
key = f"[{sport}] {event} ({TAG})"
2025-12-23 13:28:56 -05:00
2026-02-28 15:42:50 -05:00
tvg_id, logo = leagues.get_tvg_info(sport, event)
2025-12-08 13:21:43 -05:00
2026-02-28 15:42:50 -05:00
entry = {
"url": url,
"logo": logo,
"base": "https://instreams.click/",
"timestamp": ts,
"id": tvg_id or "Live.Event.us",
"link": link,
}
cached_urls[key] = entry
if url:
valid_count += 1
2025-12-08 13:21:43 -05:00
2026-02-28 15:42:50 -05:00
urls[key] = entry
2025-12-08 13:21:43 -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-08 13:21:43 -05:00
else:
log.info("No new events found")
CACHE_FILE.write(cached_urls)