change base url for totalsportek.py (duplicate source)
This commit is contained in:
doms9 2026-01-29 19:38:47 -05:00
parent 8be3067419
commit 00000d9c8c
2 changed files with 46 additions and 106 deletions

View file

@ -71,6 +71,7 @@ async def main() -> None:
asyncio.create_task(streamcenter.scrape(xtrnl_brwsr)),
# asyncio.create_task(streamhub.scrape(xtrnl_brwsr)),
asyncio.create_task(streamsgate.scrape(xtrnl_brwsr)),
asyncio.create_task(totalsportek.scrape(hdl_brwsr)),
asyncio.create_task(webcast.scrape(hdl_brwsr)),
asyncio.create_task(watchfooty.scrape(xtrnl_brwsr)),
]
@ -83,7 +84,6 @@ async def main() -> None:
asyncio.create_task(shark.scrape()),
# asyncio.create_task(streambtw.scrape()),
asyncio.create_task(streamfree.scrape()),
asyncio.create_task(totalsportek.scrape()),
asyncio.create_task(tvpass.scrape()),
asyncio.create_task(xstreameast.scrape()),
]

View file

@ -1,7 +1,7 @@
import re
from functools import partial
from urllib.parse import urljoin, urlparse
from playwright.async_api import Browser
from selectolax.parser import HTMLParser
from .utils import Cache, Time, get_logger, leagues, network
@ -12,18 +12,9 @@ urls: dict[str, dict[str, str | float]] = {}
TAG = "TOTALSPRTK"
CACHE_FILE = Cache(f"{TAG.lower()}", exp=28_800)
CACHE_FILE = Cache(TAG, exp=28_800)
MIRRORS = [
{
"base": "https://live.totalsportek777.com/",
"hex_decode": True,
},
{
"base": "https://live2.totalsportek777.com/",
"hex_decode": False,
},
]
BASE_URL = "https://live3.totalsportek777.com/"
def fix_txt(s: str) -> str:
@ -32,59 +23,10 @@ def fix_txt(s: str) -> str:
return s.upper() if s.islower() else s
async def process_event(href: str, url_num: int) -> tuple[str | None, str | None]:
valid_m3u8 = re.compile(r'var\s+(\w+)\s*=\s*"([^"]*)"', re.IGNORECASE)
for x, mirror in enumerate(MIRRORS, start=1):
base: str = mirror["base"]
hex_decode: bool = mirror["hex_decode"]
url = urljoin(base, href)
if not (html_data := await network.request(url, log=log)):
log.info(f"M{x} | URL {url_num}) Failed to load url.")
return None, None
soup = HTMLParser(html_data.content)
iframe = soup.css_first("iframe")
if not iframe or not (iframe_src := iframe.attributes.get("src")):
log.warning(f"M{x} | URL {url_num}) No iframe element found.")
continue
if not (iframe_src_data := await network.request(iframe_src, log=log)):
log.warning(f"M{x} | URL {url_num}) Failed to load iframe source.")
continue
if not (match := valid_m3u8.search(iframe_src_data.text)):
log.warning(f"M{x} | URL {url_num}) No Clappr source found.")
continue
raw: str = match[2]
try:
m3u8_url = bytes.fromhex(raw).decode("utf-8") if hex_decode else raw
except Exception as e:
log.warning(f"M{x} | URL {url_num}) Decoding failed: {e}")
continue
if m3u8_url and iframe_src:
log.info(f"M{x} | URL {url_num}) Captured M3U8")
return m3u8_url, iframe_src
log.warning(f"M{x} | URL {url_num}) No M3U8 found")
return None, None
async def get_events(url: str, cached_keys: list[str]) -> list[dict[str, str]]:
async def get_events(cached_keys: list[str]) -> list[dict[str, str]]:
events = []
if not (html_data := await network.request(url, log=log)):
if not (html_data := await network.request(BASE_URL, log=log)):
return events
soup = HTMLParser(html_data.content)
@ -124,14 +66,14 @@ async def get_events(url: str, cached_keys: list[str]) -> list[dict[str, str]]:
{
"sport": sport,
"event": event_name,
"href": href,
"link": urljoin(BASE_URL, href),
}
)
return events
async def scrape() -> None:
async def scrape(browser: Browser) -> None:
cached_urls = CACHE_FILE.load()
valid_urls = {k: v for k, v in cached_urls.items() if v["url"]}
@ -142,40 +84,38 @@ async def scrape() -> None:
log.info(f"Loaded {cached_count} event(s) from cache")
if not (base_url := await network.get_base([mirr["base"] for mirr in MIRRORS])):
log.warning("No working TotalSportek mirrors")
log.info(f'Scraping from "{BASE_URL}"')
CACHE_FILE.write(cached_urls)
return
log.info(f'Scraping from "{base_url}"')
events = await get_events(base_url, cached_urls.keys())
events = await get_events(cached_urls.keys())
log.info(f"Processing {len(events)} new URL(s)")
if events:
now = Time.clean(Time.now())
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,
href=ev["href"],
network.process_event,
url=ev["link"],
url_num=i,
)
url, iframe = await network.safe_process(
handler,
url_num=i,
semaphore=network.HTTP_S,
page=page,
log=log,
)
sport, event, href = (
url = await network.safe_process(
handler,
url_num=i,
semaphore=network.PW_S,
log=log,
timeout=6,
)
sport, event, link = (
ev["sport"],
ev["event"],
ev["href"],
ev["link"],
)
key = f"[{sport}] {event} ({TAG})"
@ -185,10 +125,10 @@ async def scrape() -> None:
entry = {
"url": url,
"logo": logo,
"base": iframe,
"base": link,
"timestamp": now.timestamp(),
"id": tvg_id or "Live.Event.us",
"href": href,
"link": link,
}
cached_urls[key] = entry