Update app.py
Browse files
app.py
CHANGED
|
@@ -208,37 +208,6 @@ def safe_text(response):
|
|
| 208 |
# -------------------------------------------------------------------
|
| 209 |
@app.route("/capture", methods=["POST"])
|
| 210 |
def capture_endpoint():
|
| 211 |
-
"""
|
| 212 |
-
JSON body example:
|
| 213 |
-
{
|
| 214 |
-
"urls": ["https://x.com/USTreasury", "https://example.com"], // required, array of one or more
|
| 215 |
-
"width": 10850, // optional, default 1080
|
| 216 |
-
"height": 1920, // optional, default 1920
|
| 217 |
-
"fullPage": true // optional, default true
|
| 218 |
-
}
|
| 219 |
-
Response:
|
| 220 |
-
{
|
| 221 |
-
"results": [
|
| 222 |
-
{
|
| 223 |
-
"url": "...",
|
| 224 |
-
"output": {
|
| 225 |
-
"imageUrl": "...png",
|
| 226 |
-
"htmlUrl": "...html"
|
| 227 |
-
},
|
| 228 |
-
"meta": { "cached": false }
|
| 229 |
-
},
|
| 230 |
-
{ ... }
|
| 231 |
-
],
|
| 232 |
-
"errors": [
|
| 233 |
-
{ "url": "...", "error": "...", "status": 403, "details": "..." }
|
| 234 |
-
]
|
| 235 |
-
}
|
| 236 |
-
Behavior:
|
| 237 |
-
- Create workspace, then bot.
|
| 238 |
-
- Run capture for all URLs with the same bot/workspace.
|
| 239 |
-
- Delete bot first, then workspace, at the end of the request.
|
| 240 |
-
"""
|
| 241 |
-
# Parse and validate body
|
| 242 |
try:
|
| 243 |
body = request.get_json(force=True) or {}
|
| 244 |
except Exception:
|
|
@@ -248,12 +217,10 @@ def capture_endpoint():
|
|
| 248 |
if not urls or not isinstance(urls, list):
|
| 249 |
return jsonify({"error": "urls is required and must be a list"}), 400
|
| 250 |
|
| 251 |
-
# Optional params
|
| 252 |
width = body.get("width", 1080)
|
| 253 |
height = body.get("height", 1920)
|
| 254 |
full_page = body.get("fullPage", True)
|
| 255 |
|
| 256 |
-
# Normalize numeric params
|
| 257 |
try:
|
| 258 |
width = int(width)
|
| 259 |
if width <= 0:
|
|
@@ -268,66 +235,68 @@ def capture_endpoint():
|
|
| 268 |
height = 1920
|
| 269 |
full_page = bool(full_page)
|
| 270 |
|
| 271 |
-
workspace_id = None
|
| 272 |
-
bot_id = None
|
| 273 |
-
|
| 274 |
-
# Create (Workspace then Bot)
|
| 275 |
-
workspace_id = create_workspace()
|
| 276 |
-
if not workspace_id:
|
| 277 |
-
return jsonify({"error": "Failed to create workspace"}), 500
|
| 278 |
-
|
| 279 |
-
bot_id = create_bot(workspace_id)
|
| 280 |
-
if not bot_id:
|
| 281 |
-
# Cleanup: if bot creation fails, delete workspace
|
| 282 |
-
delete_workspace(workspace_id)
|
| 283 |
-
return jsonify({"error": "Failed to create bot"}), 500
|
| 284 |
-
|
| 285 |
results = []
|
| 286 |
errors = []
|
| 287 |
|
| 288 |
-
# Process multiple URLs using the same bot/workspace
|
| 289 |
for url in urls:
|
| 290 |
if not isinstance(url, str) or not url.strip():
|
| 291 |
errors.append({"url": url, "error": "Invalid URL"})
|
| 292 |
continue
|
| 293 |
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
"url": url,
|
| 311 |
-
"output": {
|
| 312 |
-
"imageUrl": output.get("imageUrl"),
|
| 313 |
-
"htmlUrl": output.get("htmlUrl")
|
| 314 |
-
},
|
| 315 |
-
"meta": meta
|
| 316 |
-
})
|
| 317 |
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
|
| 326 |
response_body = {"results": results}
|
| 327 |
if errors:
|
| 328 |
response_body["errors"] = errors
|
| 329 |
|
| 330 |
-
# If all failed, return 502; else 200
|
| 331 |
if results:
|
| 332 |
return jsonify(response_body), 200
|
| 333 |
else:
|
|
|
|
| 208 |
# -------------------------------------------------------------------
|
| 209 |
@app.route("/capture", methods=["POST"])
|
| 210 |
def capture_endpoint():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
try:
|
| 212 |
body = request.get_json(force=True) or {}
|
| 213 |
except Exception:
|
|
|
|
| 217 |
if not urls or not isinstance(urls, list):
|
| 218 |
return jsonify({"error": "urls is required and must be a list"}), 400
|
| 219 |
|
|
|
|
| 220 |
width = body.get("width", 1080)
|
| 221 |
height = body.get("height", 1920)
|
| 222 |
full_page = body.get("fullPage", True)
|
| 223 |
|
|
|
|
| 224 |
try:
|
| 225 |
width = int(width)
|
| 226 |
if width <= 0:
|
|
|
|
| 235 |
height = 1920
|
| 236 |
full_page = bool(full_page)
|
| 237 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
results = []
|
| 239 |
errors = []
|
| 240 |
|
|
|
|
| 241 |
for url in urls:
|
| 242 |
if not isinstance(url, str) or not url.strip():
|
| 243 |
errors.append({"url": url, "error": "Invalid URL"})
|
| 244 |
continue
|
| 245 |
|
| 246 |
+
workspace_id = None
|
| 247 |
+
bot_id = None
|
| 248 |
+
try:
|
| 249 |
+
# Create workspace and bot PER URL
|
| 250 |
+
workspace_id = create_workspace()
|
| 251 |
+
if not workspace_id:
|
| 252 |
+
errors.append({"url": url, "error": "Failed to create workspace"})
|
| 253 |
+
continue
|
| 254 |
|
| 255 |
+
bot_id = create_bot(workspace_id)
|
| 256 |
+
if not bot_id:
|
| 257 |
+
errors.append({"url": url, "error": "Failed to create bot"})
|
| 258 |
+
# Cleanup workspace since bot failed
|
| 259 |
+
delete_workspace(workspace_id)
|
| 260 |
+
workspace_id = None
|
| 261 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
+
# Capture with this bot/workspace
|
| 264 |
+
capture = capture_screenshot(
|
| 265 |
+
url=url.strip(),
|
| 266 |
+
width=width,
|
| 267 |
+
height=height,
|
| 268 |
+
full_page=full_page,
|
| 269 |
+
bot_id=bot_id,
|
| 270 |
+
workspace_id=workspace_id
|
| 271 |
+
)
|
| 272 |
+
|
| 273 |
+
if isinstance(capture, dict) and capture.get("error"):
|
| 274 |
+
errors.append({"url": url, **capture})
|
| 275 |
+
else:
|
| 276 |
+
output = capture.get("output", {})
|
| 277 |
+
meta = capture.get("meta", {})
|
| 278 |
+
results.append({
|
| 279 |
+
"url": url,
|
| 280 |
+
"output": {
|
| 281 |
+
"imageUrl": output.get("imageUrl"),
|
| 282 |
+
"htmlUrl": output.get("htmlUrl")
|
| 283 |
+
},
|
| 284 |
+
"meta": meta
|
| 285 |
+
})
|
| 286 |
+
|
| 287 |
+
finally:
|
| 288 |
+
# Cleanup per URL: delete bot first, then workspace
|
| 289 |
+
try:
|
| 290 |
+
if bot_id and workspace_id:
|
| 291 |
+
delete_bot(bot_id, workspace_id)
|
| 292 |
+
finally:
|
| 293 |
+
if workspace_id:
|
| 294 |
+
delete_workspace(workspace_id)
|
| 295 |
|
| 296 |
response_body = {"results": results}
|
| 297 |
if errors:
|
| 298 |
response_body["errors"] = errors
|
| 299 |
|
|
|
|
| 300 |
if results:
|
| 301 |
return jsonify(response_body), 200
|
| 302 |
else:
|