josephrw commited on
Commit
e47382e
Β·
verified Β·
1 Parent(s): fde6aba

Add scrapeUrl route to endpoint Space

Browse files
Files changed (1) hide show
  1. app.py +27 -0
app.py CHANGED
@@ -573,6 +573,32 @@ async def alias_gate_status():
573
  # ═══════════════════════════════════════════════════════════════
574
  # STATIC ARTIFACT SERVING (must be last β€” catches /artifact/* GETs)
575
  # ═══════════════════════════════════════════════════════════════
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
576
  app.mount("/artifact", StaticFiles(directory=str(ARTIFACTS_DIR)), name="artifacts")
577
 
578
 
@@ -598,6 +624,7 @@ async def root():
598
  "GET /workspace/tree",
599
  "POST /artifact/compile",
600
  "POST /url/issue",
 
601
  "POST /learn",
602
  "POST /recall",
603
  "POST /tool/register",
 
573
  # ═══════════════════════════════════════════════════════════════
574
  # STATIC ARTIFACT SERVING (must be last β€” catches /artifact/* GETs)
575
  # ═══════════════════════════════════════════════════════════════
576
+ class ScrapeUrlRequest(BaseModel):
577
+ workspace: str = "default"
578
+ url: str
579
+ timeout_seconds: int = Field(default=10, ge=1, le=30)
580
+
581
+
582
+ @app.post("/scrapeUrl")
583
+ async def scrape_url(req: ScrapeUrlRequest):
584
+ ws_base = _ws_path(req.workspace)
585
+ script = ws_base / f"_scrape_{uuid.uuid4().hex[:8]}.py"
586
+ script.write_text(f"""
587
+ import urllib.request, sys
588
+ try:
589
+ r = urllib.request.urlopen("{req.url}", timeout={req.timeout_seconds})
590
+ data = r.read().decode("utf-8", errors="replace")[:50000]
591
+ print(data)
592
+ except Exception as e:
593
+ print(f"ERROR: {{e}}", file=sys.stderr)
594
+ sys.exit(1)
595
+ """)
596
+ result = _safe_run(["python3", str(script)], ws_base, req.timeout_seconds)
597
+ script.unlink(missing_ok=True)
598
+ receipt = _receipt("scrape_url", req.url[:80], result, req.workspace)
599
+ return {"url": req.url, **result, "receipt": receipt}
600
+
601
+
602
  app.mount("/artifact", StaticFiles(directory=str(ARTIFACTS_DIR)), name="artifacts")
603
 
604
 
 
624
  "GET /workspace/tree",
625
  "POST /artifact/compile",
626
  "POST /url/issue",
627
+ "POST /scrapeUrl",
628
  "POST /learn",
629
  "POST /recall",
630
  "POST /tool/register",