iptv/M3U8/scrapers/fstv.py

171 lines
4.6 KiB
Python
Raw Normal View History

2025-09-13 04:42:55 -04:00
from pathlib import Path
from urllib.parse import unquote, urljoin
2025-08-17 10:05:09 -04:00
import httpx
2025-08-27 10:26:56 -04:00
from selectolax.parser import HTMLParser
2025-08-30 16:45:19 -04:00
2025-10-01 11:57:49 -04:00
from .utils import Cache, Time, get_logger, leagues, network
2025-08-30 16:45:19 -04:00
log = get_logger(__name__)
2025-08-17 10:05:09 -04:00
2025-08-28 19:43:35 -04:00
urls: dict[str, dict[str, str]] = {}
2025-08-17 10:05:09 -04:00
2025-09-02 18:06:35 -04:00
MIRRORS = [
2025-08-23 15:15:47 -04:00
"https://fstv.zip",
2025-09-13 04:42:55 -04:00
"https://fstv.space",
"https://fstv.online",
2025-08-23 15:15:47 -04:00
"https://fstv.us",
2025-08-27 10:26:56 -04:00
]
2025-08-17 10:05:09 -04:00
2025-10-01 11:57:49 -04:00
CACHE_FILE = Cache(Path(__file__).parent / "caches" / "fstv.json", exp=10_800)
2025-08-17 10:05:09 -04:00
2025-09-13 04:42:55 -04:00
async def get_events(
client: httpx.AsyncClient,
base_url: str,
cached_hrefs: set[str],
) -> list[dict[str, str]]:
2025-08-30 16:45:19 -04:00
log.info(f'Scraping from "{base_url}"')
2025-08-17 10:05:09 -04:00
try:
2025-08-27 10:26:56 -04:00
r = await client.get(base_url)
2025-08-17 10:05:09 -04:00
r.raise_for_status()
except Exception as e:
2025-10-01 11:57:49 -04:00
log.error(f'Failed to fetch "{base_url}": {e}')
2025-08-17 10:05:09 -04:00
2025-08-17 17:01:52 -04:00
return []
2025-08-17 10:05:09 -04:00
2025-08-27 10:26:56 -04:00
soup = HTMLParser(r.text)
2025-08-17 10:05:09 -04:00
2025-09-13 04:42:55 -04:00
events = []
2025-08-17 10:05:09 -04:00
2025-08-27 10:26:56 -04:00
for wrpr in soup.css("div.fixtures-live-wrapper"):
2025-09-13 13:32:32 -04:00
for league_block in wrpr.css(".match-table-item > .league-info-wrapper"):
if not (
league_name_el := league_block.css_first(".league-info a.league-name")
):
continue
2025-08-17 10:05:09 -04:00
2025-09-13 13:32:32 -04:00
full_text = league_name_el.text(strip=True)
2025-08-17 10:05:09 -04:00
2025-09-13 13:32:32 -04:00
if "]" in full_text:
event_name = full_text.split("]", 1)[1].strip()
else:
event_name = full_text
2025-08-17 10:05:09 -04:00
2025-09-13 13:32:32 -04:00
parent_item = league_block.parent
2025-08-17 10:05:09 -04:00
2025-09-13 13:32:32 -04:00
for game in parent_item.css(".common-table-row a[href*='/match/']"):
if not (href := game.attributes.get("href")):
continue
2025-09-13 04:42:55 -04:00
if cached_hrefs & {href}:
continue
2025-09-14 18:13:44 -04:00
cached_hrefs.add(href)
2025-09-13 04:42:55 -04:00
events.append(
{
"sport": event_name,
2025-09-13 13:32:32 -04:00
"link": urljoin(base_url, href),
2025-09-13 04:42:55 -04:00
"href": href,
}
2025-08-27 10:26:56 -04:00
)
2025-08-17 10:05:09 -04:00
2025-09-13 04:42:55 -04:00
return events
2025-08-17 10:05:09 -04:00
2025-09-13 04:42:55 -04:00
async def process_event(
client: httpx.AsyncClient,
url: str,
url_num: int,
) -> tuple[str, str]:
2025-08-17 10:05:09 -04:00
try:
2025-08-27 10:26:56 -04:00
r = await client.get(url)
2025-08-17 10:05:09 -04:00
r.raise_for_status()
except Exception as e:
2025-09-13 04:42:55 -04:00
log.error(f'URL {url_num}) Failed to fetch "{url}"\n{e}')
2025-08-17 10:05:09 -04:00
2025-09-13 04:42:55 -04:00
return "", ""
2025-08-17 10:05:09 -04:00
2025-08-27 10:26:56 -04:00
soup = HTMLParser(r.text)
2025-08-17 10:05:09 -04:00
2025-08-27 10:26:56 -04:00
if category_links := soup.css(".common-list-category .category-item a"):
match_name = category_links[-1].text(strip=True)
2025-08-17 10:05:09 -04:00
else:
match_name = None
if not match_name or match_name.lower() == "vs":
2025-08-27 10:26:56 -04:00
if og_title := soup.css_first("meta[property='og:title']"):
match_name = (
og_title.attributes.get("content", "").split(" start on")[0].strip()
)
2025-08-17 10:05:09 -04:00
2025-09-13 13:32:32 -04:00
if not (ifr := soup.css_first("iframe")):
log.info(f"URL {url_num}) No M3U8 found")
return "", ""
2025-08-17 10:05:09 -04:00
2025-09-13 13:32:32 -04:00
if src := ifr.attributes.get("src", ""):
log.info(f"URL {url_num}) Captured M3U8")
2025-10-01 11:57:49 -04:00
return match_name or "", unquote(src).split("link=")[-1]
2025-08-17 10:05:09 -04:00
2025-08-17 17:01:52 -04:00
2025-09-20 23:26:18 -04:00
async def scrape(client: httpx.AsyncClient) -> None:
2025-10-01 11:57:49 -04:00
cached_urls = CACHE_FILE.load()
2025-09-13 04:42:55 -04:00
cached_hrefs = {entry["href"] for entry in cached_urls.values()}
cached_count = len(cached_urls)
urls.update(cached_urls)
2025-10-01 11:57:49 -04:00
log.info(f"Loaded {cached_count} event(s) from cache")
2025-09-13 04:42:55 -04:00
2025-10-01 11:57:49 -04:00
if not (base_url := await network.get_base(MIRRORS)):
2025-08-30 16:45:19 -04:00
log.warning("No working FSTV mirrors")
2025-10-01 11:57:49 -04:00
CACHE_FILE.write(cached_urls)
2025-08-27 10:26:56 -04:00
return
2025-08-17 10:05:09 -04:00
2025-09-13 04:42:55 -04:00
events = await get_events(
client,
base_url,
cached_hrefs,
)
log.info(f"Processing {len(events)} new URL(s)")
for i, ev in enumerate(events, start=1):
2025-10-01 11:57:49 -04:00
match_name, url = await network.safe_process(
2025-09-13 04:42:55 -04:00
lambda: process_event(
client,
ev["link"],
url_num=i,
),
url_num=i,
log=log,
)
if url:
2025-09-19 02:05:40 -04:00
sport = ev["sport"]
2025-08-17 10:05:09 -04:00
key = (
2025-09-19 02:05:40 -04:00
f"[{sport}] {match_name} (FSTV)" if match_name else f"[{sport}] (FSTV)"
2025-08-17 10:05:09 -04:00
)
2025-09-24 12:30:55 -04:00
tvg_id, logo = leagues.info(sport)
2025-09-21 10:28:15 -04:00
2025-09-13 04:42:55 -04:00
entry = {
"url": url,
2025-09-21 10:28:15 -04:00
"logo": logo,
2025-09-13 04:42:55 -04:00
"base": base_url,
2025-10-01 11:57:49 -04:00
"timestamp": Time.now().timestamp(),
"id": tvg_id or "Live.Event.us",
2025-09-13 04:42:55 -04:00
"href": ev["href"],
2025-08-28 19:43:35 -04:00
}
2025-08-17 10:05:09 -04:00
2025-09-13 04:42:55 -04:00
urls[key] = cached_urls[key] = entry
2025-09-03 00:00:22 -04:00
2025-09-13 04:42:55 -04:00
if new_count := len(cached_urls) - cached_count:
log.info(f"Collected and cached {new_count} new event(s)")
else:
log.info("No new events found")
2025-09-03 00:00:22 -04:00
2025-10-01 11:57:49 -04:00
CACHE_FILE.write(cached_urls)