LondonScrapers/functions/.functions.utils.playwright.py
2026-10-30 11:26:56 -04:00

77 lines
2.1 KiB
Python

#!/usr/bin/env python3
import os
import sys
from playwright.sync_api import sync_playwright
def fail(msg):
print(f"FAILED! {msg}")
sys.exit(1)
if len(sys.argv) != 3:
fail("Usage: <url> <outfile>")
url = sys.argv[1]
outfile = sys.argv[2]
# eScribe doesn't use consistent cases between municipalities.
# I don't know, maybe python doesn't care? I don't know python lol, this entire thing was blogs and stack overflow glued together.
url_lowercase = url.lower()
try:
with sync_playwright() as p:
# Firefox headless is okay I guess. I tried chrome but 403.
browser = p.firefox.launch(
headless=True
)
context = browser.new_context(
accept_downloads=True
)
page = context.new_page()
# The PDFs
if "filestream.ashx" in url_lowercase:
page.set_content(
f'<a id="download" href="{url}">Too bad eScribe, your files are mine. Click here!</a>'
)
with page.expect_download(
timeout=60000
) as d:
page.locator("#download").click()
download = d.value
os.makedirs(
os.path.dirname(outfile) or ".",
exist_ok=True
)
download.save_as(outfile)
browser.close()
sys.exit(0)
# The HTMLs
if "meeting.aspx" in url_lowercase:
response = page.goto(
url,
timeout=50000
)
if response is None:
fail("No response")
body = response.body()
os.makedirs(
os.path.dirname(outfile) or ".",
exist_ok=True
)
with open(outfile, "wb") as fp:
fp.write(body)
browser.close()
sys.exit(0)
browser.close()
# Not filestream or meeting. I do not know what this could be.
fail(
f"New or unsupported eScribe URL: {url}"
)
except Exception as e:
fail(
f"{type(e).__name__}: {e}"
)