iptv/M3U8/scrapers/shark.py

173 lines
4.3 KiB
Python
Raw Normal View History

2025-12-08 13:21:43 -05:00
import re
from functools import partial
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]] = {}
2025-12-13 16:57:14 -05:00
TAG = "SHARK"
2025-12-08 13:21:43 -05:00
2025-12-16 02:30:44 -05:00
CACHE_FILE = Cache(f"{TAG.lower()}.json", exp=10_800)
2025-12-08 13:21:43 -05:00
2025-12-16 02:30:44 -05:00
HTML_CACHE = Cache(f"{TAG.lower()}-html.json", exp=19_800)
2025-12-08 13:21:43 -05:00
2025-12-13 16:57:14 -05:00
BASE_URL = "https://sharkstreams.net"
2025-12-08 13:21:43 -05:00
2025-12-18 03:04:11 -05:00
async def process_event(url: str, url_num: int) -> str | None:
if not (r := await network.request(url, log=log)):
log.info(f"URL {url_num}) Failed to load url.")
2025-12-18 04:14:54 -05:00
2025-12-08 13:21:43 -05:00
return
data: dict[str, list[str]] = r.json()
2025-12-18 03:04:11 -05:00
if not (urls := data.get("urls")):
2025-12-08 13:21:43 -05:00
log.info(f"URL {url_num}) No M3U8 found")
2025-12-18 04:14:54 -05:00
2025-12-08 13:21:43 -05:00
return
log.info(f"URL {url_num}) Captured M3U8")
2025-12-18 04:14:54 -05:00
2025-12-18 03:04:11 -05:00
return urls[0]
2025-12-08 13:21:43 -05:00
2025-12-18 03:04:11 -05:00
async def refresh_html_cache(now_ts: float) -> dict[str, dict[str, str | float]]:
2025-12-08 13:21:43 -05:00
log.info("Refreshing HTML cache")
2025-12-18 03:04:11 -05:00
events = {}
2025-12-08 13:21:43 -05:00
2025-12-18 03:04:11 -05:00
if not (html_data := await network.request(BASE_URL, log=log)):
return events
2025-12-08 13:21:43 -05:00
pattern = re.compile(r"openEmbed\('([^']+)'\)", re.IGNORECASE)
2025-12-18 03:04:11 -05:00
soup = HTMLParser(html_data.content)
2025-12-08 13:21:43 -05:00
for row in soup.css(".row"):
date_node = row.css_first(".ch-date")
2025-12-18 04:14:54 -05:00
2025-12-08 13:21:43 -05:00
sport_node = row.css_first(".ch-category")
name_node = row.css_first(".ch-name")
if not (date_node and sport_node and name_node):
continue
event_dt = Time.from_str(date_node.text(strip=True), timezone="EST")
2025-12-18 04:14:54 -05:00
2025-12-08 13:21:43 -05:00
sport = sport_node.text(strip=True)
2025-12-18 04:14:54 -05:00
2025-12-08 13:21:43 -05:00
event_name = name_node.text(strip=True)
embed_btn = row.css_first("a.hd-link.secondary")
if not embed_btn or not (onclick := embed_btn.attributes.get("onclick")):
continue
if not (match := pattern.search(onclick)):
continue
link = match[1].replace("player.php", "get-stream.php")
key = f"[{sport}] {event_name} ({TAG})"
events[key] = {
"sport": sport,
"event": event_name,
"link": link,
"event_ts": event_dt.timestamp(),
"timestamp": now_ts,
}
return events
2025-12-18 03:04:11 -05:00
async def get_events(cached_keys: list[str]) -> list[dict[str, str]]:
2025-12-08 13:21:43 -05:00
now = Time.clean(Time.now())
if not (events := HTML_CACHE.load()):
2025-12-18 03:04:11 -05:00
events = await refresh_html_cache(now.timestamp())
2025-12-08 13:21:43 -05:00
HTML_CACHE.write(events)
live = []
start_ts = now.delta(hours=-1).timestamp()
end_ts = now.delta(minutes=10).timestamp()
for k, v in events.items():
2025-12-18 03:04:11 -05:00
if k in cached_keys:
2025-12-08 13:21:43 -05:00
continue
if not start_ts <= v["event_ts"] <= end_ts:
continue
live.append({**v})
return live
2025-12-18 03:04:11 -05:00
async def scrape() -> None:
2025-12-08 13:21:43 -05:00
cached_urls = CACHE_FILE.load()
2025-12-18 04:14:54 -05:00
2025-12-08 13:21:43 -05:00
cached_count = len(cached_urls)
2025-12-18 04:14:54 -05:00
2025-12-08 13:21:43 -05:00
urls.update(cached_urls)
log.info(f"Loaded {cached_count} event(s) from cache")
log.info(f'Scraping from "{BASE_URL}"')
2025-12-18 03:04:11 -05:00
events = await get_events(cached_urls.keys())
2025-12-08 13:21:43 -05:00
log.info(f"Processing {len(events)} new URL(s)")
if events:
for i, ev in enumerate(events, start=1):
handler = partial(
process_event,
url=ev["link"],
url_num=i,
)
url = await network.safe_process(
handler,
url_num=i,
log=log,
)
if url:
sport, event, ts, link = (
ev["sport"],
ev["event"],
ev["event_ts"],
ev["link"],
)
tvg_id, logo = leagues.get_tvg_info(sport, event)
key = f"[{sport}] {event} ({TAG})"
entry = {
"url": url,
"logo": logo,
"base": BASE_URL,
"timestamp": ts,
"id": tvg_id or "Live.Event.us",
"link": link,
}
urls[key] = cached_urls[key] = entry
if new_count := len(cached_urls) - cached_count:
log.info(f"Collected and cached {new_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)