77 lines
2.6 KiB
Python
Executable File
77 lines
2.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Wrapper for Obscura headless browser — simplifies common operations."""
|
|
|
|
import subprocess
|
|
import sys
|
|
import os
|
|
from pathlib import Path
|
|
|
|
OBSCURA_BIN = Path.home() / ".local" / "bin" / "obscura"
|
|
|
|
def fetch(url: str, dump: str = "markdown", timeout: int = 15, wait_until: str = None, output: str = None) -> str:
|
|
"""Fetch a single page and return markdown/text/html."""
|
|
cmd = [str(OBSCURA_BIN), "fetch", url, "--dump", dump, "--timeout", str(timeout)]
|
|
if wait_until:
|
|
cmd.extend(["--wait-until", wait_until])
|
|
if output:
|
|
cmd.extend(["--output", output])
|
|
|
|
result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout + 10)
|
|
if result.returncode != 0:
|
|
return f"ERROR: {result.stderr}"
|
|
return result.stdout
|
|
|
|
def eval_js(url: str, expression: str, timeout: int = 15) -> str:
|
|
"""Fetch a page and evaluate JavaScript."""
|
|
cmd = [str(OBSCURA_BIN), "fetch", url, "--eval", expression, "--timeout", str(timeout)]
|
|
result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout + 10)
|
|
if result.returncode != 0:
|
|
return f"ERROR: {result.stderr}"
|
|
# Filter out the "Fetching..." and "Page loaded..." lines
|
|
lines = result.stdout.strip().split("\n")
|
|
return "\n".join(line for line in lines if not line.startswith(("Fetching", "Page loaded")))
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print("Usage: obscura_wrapper.py fetch <url> [--dump markdown|text|html] [--timeout N] [--wait-until networkidle0]")
|
|
print(" obscura_wrapper.py eval <url> <js_expression>")
|
|
sys.exit(1)
|
|
|
|
action = sys.argv[1]
|
|
|
|
if action == "fetch":
|
|
url = sys.argv[2]
|
|
dump = "markdown"
|
|
timeout = 15
|
|
wait_until = None
|
|
|
|
# Parse optional flags
|
|
args = sys.argv[3:]
|
|
i = 0
|
|
while i < len(args):
|
|
if args[i] == "--dump" and i + 1 < len(args):
|
|
dump = args[i + 1]
|
|
i += 2
|
|
elif args[i] == "--timeout" and i + 1 < len(args):
|
|
timeout = int(args[i + 1])
|
|
i += 2
|
|
elif args[i] == "--wait-until" and i + 1 < len(args):
|
|
wait_until = args[i + 1]
|
|
i += 2
|
|
else:
|
|
i += 1
|
|
|
|
print(fetch(url, dump=dump, timeout=timeout, wait_until=wait_until))
|
|
|
|
elif action == "eval":
|
|
url = sys.argv[2]
|
|
expression = " ".join(sys.argv[3:])
|
|
print(eval_js(url, expression))
|
|
|
|
else:
|
|
print(f"Unknown action: {action}")
|
|
sys.exit(1)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|