pryscraper/routers/share.py
cryptorugmunch 2f1eec2f78
Some checks failed
CI / lint (pull_request) Successful in 33s
CI / typecheck (pull_request) Failing after 1m42s
CI / test (pull_request) Failing after 2m33s
CI / security (pull_request) Failing after 39s
CI / gitleaks (pull_request) Successful in 36s
CI / commitlint (pull_request) Failing after 10s
style: ruff format
2026-07-03 18:12:36 +02:00

59 lines
2.2 KiB
Python

"""Pry — Share router (remaining api.py routes).
Auto-extracted from api.py during the router-split refactor.
"""
# SPDX-License-Identifier: MIT
# Copyright (c) 2026 Rug Munch Media LLC
from __future__ import annotations
import html
import logging
import uuid
from datetime import UTC, datetime
from typing import Any
from fastapi import APIRouter, Body
from fastapi.responses import HTMLResponse
from deps import scraper
logger = logging.getLogger(__name__)
router = APIRouter(tags=["Share"])
@router.post("/v1/share", tags=["Share"], summary="Share scraped content via link")
async def share_scrape(data: dict[str, Any] = Body(...)) -> dict[str, Any]:
url = data.get("url", "")
result = await scraper.scrape(url, {"bypass_cloudflare": True})
sid = uuid.uuid4().hex[:8]
_shares[sid] = {
"url": url,
"title": result.get("title", url),
"content": result.get("content", ""),
"method": result.get("method"),
"ts": datetime.now(UTC).isoformat(),
}
return {"success": True, "data": {"share_id": sid, "url": f"/share/{sid}"}}
@router.get("/share/{share_id}", tags=["Share"], summary="View shared content")
async def view_share(share_id: str) -> HTMLResponse:
d = _shares.get(share_id)
if not d:
return HTMLResponse("<h1>Not found</h1><p>Share expired.</p>", 404)
st, sc, su = html.escape(d["title"]), html.escape(d["content"][:100000]), html.escape(d["url"])
return HTMLResponse(
f'<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>{st} — Pry Share</title>'
f'<meta name="viewport" content="width=device-width,initial-scale=1">'
f"<style>body{{font-family:-apple-system,system-ui,sans-serif;background:#09090b;color:#e4e4e7;padding:2rem;max-width:800px;margin:0 auto}}"
f"h1{{font-size:1.5rem;color:#f59e0b}}.meta{{color:#52525b;font-size:.85rem;margin-bottom:2rem}}"
f"pre{{background:#18181b;border:1px solid #27272a;border-radius:8px;padding:1.5rem;white-space:pre-wrap}}</style></head><body>"
f'<h1>🔧 {st}</h1><p class="meta"><a href="{su}">{su}</a> · {d["method"]} · {d["ts"][:10]}</p>'
f"<pre>{sc}</pre></body></html>"
)
_shares: dict[str, dict[str, Any]] = {}