e
This commit is contained in:
parent
cb9d5637fc
commit
00000d905f
7 changed files with 224 additions and 93 deletions
|
|
@ -4,12 +4,21 @@ import json
|
|||
import ssl
|
||||
import xml.etree.ElementTree as ET
|
||||
from datetime import datetime, timedelta
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
from playwright.async_api import Request, async_playwright
|
||||
from playwright.async_api import async_playwright
|
||||
|
||||
from .utils import LOGOS, TZ, get_logger, now, safe_process_event
|
||||
from .utils import (
|
||||
LOGOS,
|
||||
TZ,
|
||||
capture_req,
|
||||
get_logger,
|
||||
load_ts_cache,
|
||||
now,
|
||||
safe_process_event,
|
||||
)
|
||||
|
||||
log = get_logger(__name__)
|
||||
|
||||
|
|
@ -62,21 +71,6 @@ async def get_cert(client: httpx.AsyncClient) -> ssl.SSLContext:
|
|||
return ssl.create_default_context(cafile=CERT_FILE)
|
||||
|
||||
|
||||
def load_cache() -> dict[str, dict[str, str | float]]:
|
||||
try:
|
||||
data: dict[str, dict[str, str | float]] = json.loads(
|
||||
CACHE_FILE.read_text(encoding="utf-8")
|
||||
)
|
||||
|
||||
return {
|
||||
k: v
|
||||
for k, v in data.items()
|
||||
if now.timestamp() - data[k].get("timestamp", 0) < 14400 # 4 hours
|
||||
}
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
return {}
|
||||
|
||||
|
||||
async def fetch_xml_stream(url: str, ssl_ctx: ssl.SSLContext) -> io.BytesIO:
|
||||
buffer = io.BytesIO()
|
||||
|
||||
|
|
@ -104,44 +98,38 @@ async def process_event(url: str, url_num: int) -> str | None:
|
|||
context = await browser.new_context(
|
||||
ignore_https_errors=True # website doesn't send valid certs
|
||||
)
|
||||
ev_page = await context.new_page()
|
||||
|
||||
page = await context.new_page()
|
||||
|
||||
captured: list[str] = []
|
||||
|
||||
got_one = asyncio.Event()
|
||||
|
||||
def capture_req(req: Request) -> None:
|
||||
if (
|
||||
".m3u8" in req.url
|
||||
and "amazonaws" not in req.url
|
||||
and "knitcdn" not in req.url
|
||||
):
|
||||
captured.append(req.url)
|
||||
got_one.set()
|
||||
handler = partial(capture_req, captured=captured, got_one=got_one)
|
||||
|
||||
popup = None
|
||||
|
||||
try:
|
||||
await ev_page.goto(
|
||||
await page.goto(
|
||||
url,
|
||||
wait_until="domcontentloaded",
|
||||
timeout=10_000,
|
||||
)
|
||||
|
||||
btn = await ev_page.query_selector(".lnkhdr > tbody > tr > td:nth-child(2)")
|
||||
btn = await page.query_selector(".lnkhdr > tbody > tr > td:nth-child(2)")
|
||||
|
||||
if btn:
|
||||
try:
|
||||
await btn.click()
|
||||
|
||||
await ev_page.wait_for_timeout(500)
|
||||
await page.wait_for_timeout(500)
|
||||
except Exception as e:
|
||||
log.debug(f"URL {url_num}) Failed to click Browser Links tab: {e}")
|
||||
return
|
||||
else:
|
||||
log.warning(f"URL {url_num}) Browser Links tab not found")
|
||||
|
||||
link_img = await ev_page.query_selector(
|
||||
link_img = await page.query_selector(
|
||||
"tr:nth-child(2) > td:nth-child(1) td:nth-child(6) img"
|
||||
)
|
||||
|
||||
|
|
@ -149,10 +137,10 @@ async def process_event(url: str, url_num: int) -> str | None:
|
|||
log.warning(f"URL {url_num}) No browser link to click.")
|
||||
return
|
||||
|
||||
ev_page.on("request", capture_req)
|
||||
page.on("request", handler)
|
||||
|
||||
try:
|
||||
async with ev_page.expect_popup(timeout=5_000) as popup_info:
|
||||
async with page.expect_popup(timeout=5_000) as popup_info:
|
||||
try:
|
||||
await link_img.click()
|
||||
except Exception as e:
|
||||
|
|
@ -162,22 +150,20 @@ async def process_event(url: str, url_num: int) -> str | None:
|
|||
|
||||
popup = await popup_info.value
|
||||
|
||||
popup.on("request", capture_req)
|
||||
popup.on("request", handler)
|
||||
except Exception:
|
||||
|
||||
try:
|
||||
await link_img.click()
|
||||
except Exception as e:
|
||||
log.debug(f"URL {url_num}) Fallback click failed: {e}")
|
||||
return
|
||||
|
||||
wait_task = asyncio.create_task(got_one.wait())
|
||||
|
||||
try:
|
||||
await asyncio.wait_for(wait_task, timeout=1.5e1)
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
log.warning(f"URL {url_num}) Timed out waiting for m3u8.")
|
||||
log.warning(f"URL {url_num}) Timed out waiting for M3U8.")
|
||||
return
|
||||
|
||||
finally:
|
||||
|
|
@ -189,32 +175,32 @@ async def process_event(url: str, url_num: int) -> str | None:
|
|||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
ev_page.remove_listener("request", capture_req)
|
||||
page.remove_listener("request", handler)
|
||||
|
||||
if popup:
|
||||
popup.remove_listener("request", capture_req)
|
||||
popup.remove_listener("request", handler)
|
||||
|
||||
await popup.close()
|
||||
|
||||
await ev_page.close()
|
||||
await page.close()
|
||||
|
||||
if captured:
|
||||
log.info(f"URL {url_num}) Captured M3U8")
|
||||
|
||||
return captured[-1]
|
||||
|
||||
log.warning(f"URL {url_num}) No m3u8 captured in popup or inline playback.")
|
||||
log.warning(f"URL {url_num}) No M3U8 captured in popup or inline playback.")
|
||||
return
|
||||
except Exception:
|
||||
try:
|
||||
ev_page.remove_listener("request", capture_req)
|
||||
page.remove_listener("request", handler)
|
||||
|
||||
if popup:
|
||||
popup.remove_listener("request", capture_req)
|
||||
popup.remove_listener("request", handler)
|
||||
|
||||
await popup.close()
|
||||
|
||||
await ev_page.close()
|
||||
await page.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
|
@ -283,7 +269,7 @@ async def main(client: httpx.AsyncClient) -> None:
|
|||
|
||||
cert = await get_cert(client)
|
||||
|
||||
cached_urls = load_cache()
|
||||
cached_urls = load_ts_cache(CACHE_FILE, 14400)
|
||||
cached_count = len(cached_urls)
|
||||
|
||||
log.info(f"Collected {cached_count} event(s) from cache")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue