diff --git a/M3U8/fetch.py b/M3U8/fetch.py index 015caeb2..c2253c67 100644 --- a/M3U8/fetch.py +++ b/M3U8/fetch.py @@ -20,11 +20,10 @@ from scrapers import ( streamfree, streamhub, streamsgate, - tflix, - totalsportek, tvpass, watchfooty, webcast, + xstreameast, ) from scrapers.utils import get_logger, network @@ -69,7 +68,6 @@ 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(tflix.scrape(xtrnl_brwsr)), asyncio.create_task(webcast.scrape(hdl_brwsr)), asyncio.create_task(watchfooty.scrape(xtrnl_brwsr)), ] @@ -82,8 +80,8 @@ 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()), ] await asyncio.gather(*(pw_tasks + httpx_tasks)) @@ -111,11 +109,10 @@ async def main() -> None: | streamfree.urls | streamhub.urls | streamsgate.urls - | tflix.urls - | totalsportek.urls | tvpass.urls | watchfooty.urls | webcast.urls + | xstreameast.urls ) live_events: list[str] = [] diff --git a/M3U8/scrapers/tflix.py b/M3U8/scrapers/tflix.py deleted file mode 100644 index d883130c..00000000 --- a/M3U8/scrapers/tflix.py +++ /dev/null @@ -1,234 +0,0 @@ -import asyncio -from functools import partial -from urllib.parse import urljoin - -import feedparser -from playwright.async_api import Browser, Error, Page, TimeoutError - -from .utils import Cache, Time, get_logger, leagues, network - -log = get_logger(__name__) - -urls: dict[str, dict[str, str | float]] = {} - -TAG = "TFLIX" - -CACHE_FILE = Cache(TAG, exp=28_800) - -BASE_URL = "https://tv.tflix.app/" - -SPORT_ENDPOINTS = ["football", "nba", "nfl", "nhl"] - - -async def process_event( - url: str, - url_num: int, - page: Page, -) -> tuple[str | None, str | None]: - try: - await page.goto( - url, - wait_until="domcontentloaded", - timeout=15_000, - ) - - try: - iframe = await page.wait_for_selector( - "iframe.metaframe.rptss", - timeout=3_500, - ) - except TimeoutError: - log.warning(f"URL {url_num}) No iframe element.") - - return None, None - - if (old_src := await iframe.get_attribute("src")) and old_src.startswith( - "https://kloxmkhs.site/stream" - ): - new_src = old_src - - else: - try: - option = await page.wait_for_selector( - 'li.dooplay_player_option >> span.title:has-text("TFLIX HD - iOS")', - timeout=3_000, - ) - - await option.scroll_into_view_if_needed() - - await option.evaluate("el => el.click()") - - await page.wait_for_function( - """ - (oldSrc) => { - const iframe = document.querySelector('iframe.metaframe.rptss'); - return iframe && iframe.src && iframe.src !== oldSrc; - }; - """, - arg=old_src, - timeout=5_000, - ) - - iframe_2 = await page.wait_for_selector("iframe.metaframe.rptss") - - if not iframe_2 or not (new_src := await iframe_2.get_attribute("src")): - log.warning(f"URL {url_num}) No iframe source.") - - return None, None - except TimeoutError: - log.warning(f"URL {url_num}) No valid TFLIX source.") - - return None, None - - try: - await page.goto( - new_src, - wait_until="domcontentloaded", - timeout=10_000, - referer=url, - ) - except Error: - log.warning( - f"URL {url_num}) HTTP 403/404 error while redirecting to iframe source." - ) - - return None, None - - try: - play_btn = await page.wait_for_selector( - 'button[data-url][onclick*="startPlcb"]', - timeout=5_000, - ) - except TimeoutError: - log.warning(f"URL {url_num}) No play button found.") - - return None, None - - if not (data_url := await play_btn.get_attribute("data-url")): - log.warning(f"URL {url_num}) No PBID found.") - - return None, None - - log.info(f"URL {url_num}) Captured M3U8") - - return ( - f"https://kloxmkhs.site/stream/stream.m3u8?id={data_url}&format=.m3u8", - new_src, - ) - - except Exception as e: - log.warning(f"URL {url_num}) Exception while processing: {e}") - - return None, None - - -async def get_events(cached_keys: list[str]) -> list[dict[str, str]]: - tasks = [ - network.request(urljoin(BASE_URL, f"genre/{sport}/feed"), log=log) - for sport in SPORT_ENDPOINTS - ] - - results = await asyncio.gather(*tasks) - - events = [] - - if not (feeds := [feedparser.parse(html.content) for html in results if html]): - return events - - for feed in feeds: - title: str = feed["feed"]["title"] - - sport = title.split("Archives")[0].strip() - - for entry in feed.entries: - if not (link := entry.get("link")): - continue - - if not (title := entry.get("title")): - continue - - if f"[{sport}] {title} ({TAG})" in cached_keys: - continue - - events.append( - { - "sport": sport, - "event": title, - "link": link, - } - ) - - return events - - -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"]} - - valid_count = cached_count = len(cached_urls) - - urls.update(valid_urls) - - log.info(f"Loaded {cached_count} event(s) from cache") - - log.info(f'Scraping from "{BASE_URL}"') - - events = await get_events(cached_urls.keys()) - - log.info(f"Processing {len(events)} new URL(s)") - - if events: - now = Time.clean(Time.now()).timestamp() - - 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: - handler = partial( - process_event, - url=ev["link"], - url_num=i, - page=page, - ) - - url, iframe = await network.safe_process( - handler, - url_num=i, - semaphore=network.PW_S, - log=log, - timeout=20, - ) - - sport, event, link = ( - ev["sport"], - ev["event"], - ev["link"], - ) - - key = f"[{sport}] {event} ({TAG})" - - tvg_id, logo = leagues.get_tvg_info(sport, event) - - entry = { - "url": url, - "logo": logo, - "base": iframe, - "timestamp": now, - "id": tvg_id or "Live.Event.us", - "link": link, - } - - cached_urls[key] = entry - - if url: - valid_count += 1 - - urls[key] = entry - - if new_count := valid_count - cached_count: - log.info(f"Collected and cached {new_count} new event(s)") - - else: - log.info("No new events found") - - CACHE_FILE.write(cached_urls) diff --git a/M3U8/scrapers/totalsportek.py b/M3U8/scrapers/totalsportek.py deleted file mode 100644 index fecb29d0..00000000 --- a/M3U8/scrapers/totalsportek.py +++ /dev/null @@ -1,205 +0,0 @@ -import re -from functools import partial -from urllib.parse import urljoin, urlparse - -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 = "TOTALSPRTK" - -CACHE_FILE = Cache(TAG, exp=28_800) - -MIRRORS = [ - { - "base": "https://live.totalsportek777.com/", - "hex_decode": True, - }, - { - "base": "https://live2.totalsportek777.com/", - "hex_decode": False, - }, -] - - -def fix_txt(s: str) -> str: - s = " ".join(s.split()) - - 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]]: - events = [] - - if not (html_data := await network.request(url, log=log)): - return events - - soup = HTMLParser(html_data.content) - - sport = "Live Event" - - for node in soup.css("a"): - if not node.attributes.get("class"): - continue - - if (parent := node.parent) and "my-1" in parent.attributes.get("class", ""): - if span := node.css_first("span"): - sport = span.text(strip=True) - - sport = fix_txt(sport) - - if not (teams := [t.text(strip=True) for t in node.css(".col-7 .col-12")]): - continue - - if not (href := node.attributes.get("href")): - continue - - href = urlparse(href).path if href.startswith("http") else href - - if not (time_node := node.css_first(".col-3 span")): - continue - - if time_node.text(strip=True) != "MatchStarted": - continue - - event_name = fix_txt(" vs ".join(teams)) - - if f"[{sport}] {event_name} ({TAG})" in cached_keys: - continue - - events.append( - { - "sport": sport, - "event": event_name, - "href": href, - } - ) - - return events - - -async def scrape() -> None: - cached_urls = CACHE_FILE.load() - - valid_urls = {k: v for k, v in cached_urls.items() if v["url"]} - - valid_count = cached_count = len(valid_urls) - - urls.update(valid_urls) - - 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") - - CACHE_FILE.write(cached_urls) - - return - - events = await get_events(base_url, cached_urls.keys()) - - log.info(f"Processing {len(events)} new URL(s)") - - if events: - now = Time.clean(Time.now()) - - for i, ev in enumerate(events, start=1): - handler = partial( - process_event, - href=ev["href"], - url_num=i, - ) - - url, iframe = await network.safe_process( - handler, - url_num=i, - semaphore=network.HTTP_S, - log=log, - ) - - sport, event, href = ( - ev["sport"], - ev["event"], - ev["href"], - ) - - key = f"[{sport}] {event} ({TAG})" - - tvg_id, logo = leagues.get_tvg_info(sport, event) - - entry = { - "url": url, - "logo": logo, - "base": iframe, - "timestamp": now.timestamp(), - "id": tvg_id or "Live.Event.us", - "href": href, - } - - cached_urls[key] = entry - - if url: - valid_count += 1 - - urls[key] = entry - - if new_count := valid_count - cached_count: - log.info(f"Collected and cached {new_count} new event(s)") - - else: - log.info("No new events found") - - CACHE_FILE.write(cached_urls) diff --git a/M3U8/scrapers/watchfooty.py b/M3U8/scrapers/watchfooty.py index 1b8b0448..c92bdbdd 100644 --- a/M3U8/scrapers/watchfooty.py +++ b/M3U8/scrapers/watchfooty.py @@ -76,6 +76,8 @@ async def process_event( page: Page, ) -> tuple[str | None, str | None]: + nones = [None for _ in range(2)] + pattern = re.compile(r"\((\d+)\)") captured: list[str] = [] @@ -106,12 +108,12 @@ async def process_event( except TimeoutError: log.warning(f"URL {url_num}) Can't find stream links header.") - return None, None + return nones if not (match := pattern.search(text)) or int(match[1]) == 0: log.warning(f"URL {url_num}) No available stream links.") - return None, None + return nones try: first_available = await page.wait_for_selector( @@ -121,12 +123,12 @@ async def process_event( except TimeoutError: log.warning(f"URL {url_num}) No available stream links.") - return None, None + return nones if not (href := await first_available.get_attribute("href")): log.warning(f"URL {url_num}) No available stream links.") - return None, None + return nones embed = re.sub( pattern=r"^.*\/stream", @@ -147,7 +149,7 @@ async def process_event( except asyncio.TimeoutError: log.warning(f"URL {url_num}) Timed out waiting for M3U8.") - return None, None + return nones finally: if not wait_task.done(): @@ -165,12 +167,12 @@ async def process_event( log.warning(f"URL {url_num}) No M3U8 captured after waiting.") - return None, None + return nones except Exception as e: log.warning(f"URL {url_num}) Exception while processing: {e}") - return None, None + return nones finally: page.remove_listener("request", handler) diff --git a/M3U8/scrapers/xstreameast.py b/M3U8/scrapers/xstreameast.py new file mode 100644 index 00000000..b1c6278a --- /dev/null +++ b/M3U8/scrapers/xstreameast.py @@ -0,0 +1,188 @@ +import asyncio +import re +from functools import partial +from urllib.parse import urljoin + +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 = "XSTRMEST" + +CACHE_FILE = Cache(TAG, exp=10_800) + +BASE_URL = "https://xstreameast.com" + +SPORT_ENDPOINTS = [ + # "f1", + # "mlb", + "mma", + "nba", + "nfl", + "nhl", + "soccer", + "wwe", +] + + +async def process_event(url: str, url_num: int) -> tuple[str | None, str | None]: + valid_m3u8 = re.compile(r'var\s+(\w+)\s*=\s*"([^"]*)"', re.IGNORECASE) + + nones = [None for _ in range(2)] + + if not (html_data := await network.request(url, log=log)): + log.info(f"URL {url_num}) Failed to load url.") + return nones + + soup = HTMLParser(html_data.content) + + iframe = soup.css_first("iframe") + + if not iframe or not (iframe_src := iframe.attributes.get("src")): + log.warning(f"URL {url_num}) No iframe element found.") + return nones + + elif iframe_src == "about:blank": + log.warning(f"URL {url_num}) No iframe element found.") + return nones + + if not (iframe_src_data := await network.request(iframe_src, log=log)): + log.warning(f"URL {url_num}) Failed to load iframe source.") + return nones + + if not (match := valid_m3u8.search(iframe_src_data.text)): + log.warning(f"URL {url_num}) No Clappr source found.") + return nones + + log.info(f"URL {url_num}) Captured M3U8") + + return bytes.fromhex(match[2]).decode("utf-8"), iframe_src + + +async def get_events(cached_keys: list[str]) -> list[dict[str, str]]: + tasks = [ + network.request( + urljoin(BASE_URL, f"categories/{sport}/"), + log=log, + ) + for sport in SPORT_ENDPOINTS + ] + + results = await asyncio.gather(*tasks) + + events = [] + + if not (soups := [HTMLParser(html.content) for html in results if html]): + return events + + for soup in soups: + + sport = "Live Event" + + if sport_header := soup.css_first("h1.text-3xl"): + header = sport_header.text(strip=True) + + sport = header.split("Streams")[0].strip() + + for card in soup.css("article.game-card"): + if not (team_elem := card.css_first("h2.text-xl.font-semibold")): + continue + + if not (link_elem := card.css_first("a.stream-button")) or not ( + href := link_elem.attributes.get("href") + ): + continue + + if ( + not (live_badge := card.css_first("span.bg-green-600")) + or live_badge.text(strip=True) != "LIVE" + ): + continue + + event_name = team_elem.text(strip=True) + + if f"[{sport}] {event_name} ({TAG})" in cached_keys: + continue + + events.append( + { + "sport": sport, + "event": event_name, + "link": href, + } + ) + + return events + + +async def scrape() -> None: + cached_urls = CACHE_FILE.load() + + valid_urls = {k: v for k, v in cached_urls.items() if v["url"]} + + valid_count = cached_count = len(valid_urls) + + urls.update(valid_urls) + + log.info(f"Loaded {cached_count} event(s) from cache") + + log.info(f'Scraping from "{BASE_URL}"') + + events = await get_events(cached_urls.keys()) + + log.info(f"Processing {len(events)} new URL(s)") + + if events: + now = Time.clean(Time.now()) + + for i, ev in enumerate(events, start=1): + handler = partial( + process_event, + url=ev["link"], + url_num=i, + ) + + url, iframe = await network.safe_process( + handler, + url_num=i, + semaphore=network.HTTP_S, + log=log, + ) + + sport, event, link = ( + ev["sport"], + ev["event"], + ev["link"], + ) + + key = f"[{sport}] {event} ({TAG})" + + tvg_id, logo = leagues.get_tvg_info(sport, event) + + entry = { + "url": url, + "logo": logo, + "base": iframe, + "timestamp": now.timestamp(), + "id": tvg_id or "Live.Event.us", + "link": link, + } + + cached_urls[key] = entry + + if url: + valid_count += 1 + + urls[key] = entry + + if new_count := valid_count - cached_count: + log.info(f"Collected and cached {new_count} new event(s)") + + else: + log.info("No new events found") + + CACHE_FILE.write(cached_urls)