e
This commit is contained in:
parent
b110aee1e8
commit
00000d9ba6
11 changed files with 121 additions and 135 deletions
|
|
@ -1,3 +1,4 @@
|
|||
from functools import partial
|
||||
from pathlib import Path
|
||||
from urllib.parse import unquote, urljoin
|
||||
|
||||
|
|
@ -20,14 +21,48 @@ MIRRORS = [
|
|||
CACHE_FILE = Cache(Path(__file__).parent / "caches" / "fstv.json", exp=10_800)
|
||||
|
||||
|
||||
async def process_event(
|
||||
client: httpx.AsyncClient,
|
||||
url: str,
|
||||
url_num: int,
|
||||
) -> tuple[str, str]:
|
||||
|
||||
try:
|
||||
r = await client.get(url)
|
||||
r.raise_for_status()
|
||||
except Exception as e:
|
||||
log.error(f'URL {url_num}) Failed to fetch "{url}": {e}')
|
||||
|
||||
return "", ""
|
||||
|
||||
soup = HTMLParser(r.text)
|
||||
|
||||
if category_links := soup.css(".common-list-category .category-item a"):
|
||||
match_name = category_links[-1].text(strip=True)
|
||||
else:
|
||||
match_name = None
|
||||
|
||||
if not match_name or match_name.lower() == "vs":
|
||||
if og_title := soup.css_first("meta[property='og:title']"):
|
||||
match_name = (
|
||||
og_title.attributes.get("content", "").split(" start on")[0].strip()
|
||||
)
|
||||
|
||||
if not (ifr := soup.css_first("iframe")):
|
||||
log.info(f"URL {url_num}) No M3U8 found")
|
||||
return "", ""
|
||||
|
||||
if src := ifr.attributes.get("src"):
|
||||
log.info(f"URL {url_num}) Captured M3U8")
|
||||
return match_name or "", unquote(src).split("link=")[-1]
|
||||
|
||||
|
||||
async def get_events(
|
||||
client: httpx.AsyncClient,
|
||||
base_url: str,
|
||||
cached_hrefs: set[str],
|
||||
) -> list[dict[str, str]]:
|
||||
|
||||
log.info(f'Scraping from "{base_url}"')
|
||||
|
||||
try:
|
||||
r = await client.get(base_url)
|
||||
r.raise_for_status()
|
||||
|
|
@ -76,42 +111,6 @@ async def get_events(
|
|||
return events
|
||||
|
||||
|
||||
async def process_event(
|
||||
client: httpx.AsyncClient,
|
||||
url: str,
|
||||
url_num: int,
|
||||
) -> tuple[str, str]:
|
||||
|
||||
try:
|
||||
r = await client.get(url)
|
||||
r.raise_for_status()
|
||||
except Exception as e:
|
||||
log.error(f'URL {url_num}) Failed to fetch "{url}"\n{e}')
|
||||
|
||||
return "", ""
|
||||
|
||||
soup = HTMLParser(r.text)
|
||||
|
||||
if category_links := soup.css(".common-list-category .category-item a"):
|
||||
match_name = category_links[-1].text(strip=True)
|
||||
else:
|
||||
match_name = None
|
||||
|
||||
if not match_name or match_name.lower() == "vs":
|
||||
if og_title := soup.css_first("meta[property='og:title']"):
|
||||
match_name = (
|
||||
og_title.attributes.get("content", "").split(" start on")[0].strip()
|
||||
)
|
||||
|
||||
if not (ifr := soup.css_first("iframe")):
|
||||
log.info(f"URL {url_num}) No M3U8 found")
|
||||
return "", ""
|
||||
|
||||
if src := ifr.attributes.get("src", ""):
|
||||
log.info(f"URL {url_num}) Captured M3U8")
|
||||
return match_name or "", unquote(src).split("link=")[-1]
|
||||
|
||||
|
||||
async def scrape(client: httpx.AsyncClient) -> None:
|
||||
cached_urls = CACHE_FILE.load()
|
||||
cached_hrefs = {entry["href"] for entry in cached_urls.values()}
|
||||
|
|
@ -125,6 +124,8 @@ async def scrape(client: httpx.AsyncClient) -> None:
|
|||
CACHE_FILE.write(cached_urls)
|
||||
return
|
||||
|
||||
log.info(f'Scraping from "{base_url}"')
|
||||
|
||||
events = await get_events(
|
||||
client,
|
||||
base_url,
|
||||
|
|
@ -136,15 +137,9 @@ async def scrape(client: httpx.AsyncClient) -> None:
|
|||
now = Time.now().timestamp()
|
||||
|
||||
for i, ev in enumerate(events, start=1):
|
||||
match_name, url = await network.safe_process(
|
||||
lambda: process_event(
|
||||
client,
|
||||
ev["link"],
|
||||
url_num=i,
|
||||
),
|
||||
url_num=i,
|
||||
log=log,
|
||||
)
|
||||
handler = partial(process_event, client=client, url=ev["link"], url_num=i)
|
||||
|
||||
match_name, url = await network.safe_process(handler, url_num=i, log=log)
|
||||
|
||||
if url:
|
||||
sport = ev["sport"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue