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

View file

@ -1,7 +1,7 @@
import re
from functools import partial from functools import partial
from urllib.parse import urljoin, urlparse from urllib.parse import urljoin, urlparse
from playwright.async_api import Browser
from selectolax.parser import HTMLParser from selectolax.parser import HTMLParser
from .utils import Cache, Time, get_logger, leagues, network from .utils import Cache, Time, get_logger, leagues, network
@ -12,18 +12,9 @@ urls: dict[str, dict[str, str | float]] = {}
TAG = "TOTALSPRTK" TAG = "TOTALSPRTK"
CACHE_FILE = Cache(f"{TAG.lower()}", exp=28_800) CACHE_FILE = Cache(TAG, exp=28_800)
MIRRORS = [ BASE_URL = "https://live3.totalsportek777.com/"
{
"base": "https://live.totalsportek777.com/",
"hex_decode": True,
},
{
"base": "https://live2.totalsportek777.com/",
"hex_decode": False,
},
]
def fix_txt(s: str) -> str: def fix_txt(s: str) -> str:
@ -32,59 +23,10 @@ def fix_txt(s: str) -> str:
return s.upper() if s.islower() else s return s.upper() if s.islower() else s
async def process_event(href: str, url_num: int) -> tuple[str | None, str | None]: async def get_events(cached_keys: list[str]) -> list[dict[str, str]]:
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]]:
events = [] events = []
if not (html_data := await network.request(url, log=log)): if not (html_data := await network.request(BASE_URL, log=log)):
return events return events
soup = HTMLParser(html_data.content) 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, "sport": sport,
"event": event_name, "event": event_name,
"href": href, "link": urljoin(BASE_URL, href),
} }
) )
return events return events
async def scrape() -> None: async def scrape(browser: Browser) -> None:
cached_urls = CACHE_FILE.load() cached_urls = CACHE_FILE.load()
valid_urls = {k: v for k, v in cached_urls.items() if v["url"]} valid_urls = {k: v for k, v in cached_urls.items() if v["url"]}
@ -142,61 +84,59 @@ async def scrape() -> None:
log.info(f"Loaded {cached_count} event(s) from cache") 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.info(f'Scraping from "{BASE_URL}"')
log.warning("No working TotalSportek mirrors")
CACHE_FILE.write(cached_urls) events = await get_events(cached_urls.keys())
return
log.info(f'Scraping from "{base_url}"')
events = await get_events(base_url, cached_urls.keys())
log.info(f"Processing {len(events)} new URL(s)") log.info(f"Processing {len(events)} new URL(s)")
if events: if events:
now = Time.clean(Time.now()) now = Time.clean(Time.now())
for i, ev in enumerate(events, start=1): async with network.event_context(browser) as context:
handler = partial( for i, ev in enumerate(events, start=1):
process_event, async with network.event_page(context) as page:
href=ev["href"], handler = partial(
url_num=i, network.process_event,
) url=ev["link"],
url_num=i,
page=page,
log=log,
)
url, iframe = await network.safe_process( url = await network.safe_process(
handler, handler,
url_num=i, url_num=i,
semaphore=network.HTTP_S, semaphore=network.PW_S,
log=log, log=log,
) timeout=6,
)
sport, event, href = ( sport, event, link = (
ev["sport"], ev["sport"],
ev["event"], ev["event"],
ev["href"], ev["link"],
) )
key = f"[{sport}] {event} ({TAG})" key = f"[{sport}] {event} ({TAG})"
tvg_id, logo = leagues.get_tvg_info(sport, event) tvg_id, logo = leagues.get_tvg_info(sport, event)
entry = { entry = {
"url": url, "url": url,
"logo": logo, "logo": logo,
"base": iframe, "base": link,
"timestamp": now.timestamp(), "timestamp": now.timestamp(),
"id": tvg_id or "Live.Event.us", "id": tvg_id or "Live.Event.us",
"href": href, "link": link,
} }
cached_urls[key] = entry cached_urls[key] = entry
if url: if url:
valid_count += 1 valid_count += 1
urls[key] = entry urls[key] = entry
if new_count := valid_count - cached_count: if new_count := valid_count - cached_count:
log.info(f"Collected and cached {new_count} new event(s)") log.info(f"Collected and cached {new_count} new event(s)")