- #!/usr/bin/env python3
- import sys
- from invisible_playwright import InvisiblePlaywright
- INIT_SCRIPT = """
- () => {
- window.__m3u8FetchState = { count: 0, secondUrl: null };
- const originalFetch = window.fetch.bind(window);
- window.fetch = function (input, init) {
- try {
- const rawUrl =
- typeof input === 'string'
- ? input
- : input instanceof URL
- ? input.toString()
- : input && input.url
- ? input.url
- : String(input);
- const resolved = new URL(rawUrl, window.location.href);
- const pathOnly = resolved.pathname; // ignore query string / hash
- if (pathOnly.toLowerCase().endsWith('.m3u8')) {
- window.__m3u8FetchState.count += 1;
- if (window.__m3u8FetchState.count === 2 && !window.__m3u8FetchState.secondUrl) {
- window.__m3u8FetchState.secondUrl = resolved.href;
- }
- }
- } catch (e) {
- // If URL parsing fails for some exotic input, just let the
- // fetch proceed untouched rather than breaking the page.
- }
- return originalFetch(input, init);
- };
- }
- """
- def main() -> int:
- if len(sys.argv) < 2:
- print("Usage: python second_m3u8_fetch.py <url>", file=sys.stderr)
- return 1
- url = sys.argv[1]
- with InvisiblePlaywright() as browser:
- page = browser.new_page()
- # Installed before any page JS runs.
- page.add_init_script(INIT_SCRIPT)
- try:
- page.goto(url, wait_until="domcontentloaded")
- except Exception as err:
- print(f"Navigation failed: {err}", file=sys.stderr)
- return 3
- print("Waiting for the second fetch()-ed .m3u8 URL (no timeout)...", file=sys.stderr)
- # timeout=0 means wait indefinitely. If the page only ever fetches
- # one .m3u8 file, this call simply never returns — by design.
- handle = page.wait_for_function(
- "() => window.__m3u8FetchState.secondUrl", timeout=0
- )
- second_m3u8_url = handle.json_value()
- print(second_m3u8_url)
- return 0
- if __name__ == "__main__":
- sys.exit(main())
Untitled
Posted by Anonymous on Thu 13th Aug 2026 03:29
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
kpaste.net RSS