Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -294,6 +294,91 @@ def cors_proxy_404(e):
|
|
| 294 |
|
| 295 |
return Response(resp.content, resp.status_code, headers)
|
| 296 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
if __name__ == "__main__":
|
| 298 |
if os.environ.get("WERKZEUG_RUN_MAIN") != "true":
|
| 299 |
start_processes()
|
|
|
|
| 294 |
|
| 295 |
return Response(resp.content, resp.status_code, headers)
|
| 296 |
|
| 297 |
+
#------------------
|
| 298 |
+
def generate_random_user():
|
| 299 |
+
return f"user_{random.randint(1000,9999)}"
|
| 300 |
+
|
| 301 |
+
@app.route("/scratch/<project_id>", methods=["GET"])
|
| 302 |
+
def scratch_embed(project_id):
|
| 303 |
+
# URLパラメータからオプションを取得
|
| 304 |
+
options = {
|
| 305 |
+
"turbo": request.args.get("turbo", "false") == "true",
|
| 306 |
+
"interpolation": request.args.get("completion", "false") == "true", # ここは後で見直し
|
| 307 |
+
"highQualityPen": request.args.get("pen_smooth", "true") == "true",
|
| 308 |
+
"maxClones": int(request.args.get("max_clones", "300")),
|
| 309 |
+
"stageWidth": int(request.args.get("stage_width", "480")),
|
| 310 |
+
"stageHeight": int(request.args.get("stage_height", "360")),
|
| 311 |
+
"resizeMode": request.args.get("stretch", "preserve-ratio"),
|
| 312 |
+
"autoplay": request.args.get("autostart", "true") == "true",
|
| 313 |
+
"username": request.args.get("username", generate_random_user()),
|
| 314 |
+
"closeWhenStopped": request.args.get("close_on_stop", "false") == "true",
|
| 315 |
+
"custom": {
|
| 316 |
+
"css": request.args.get("custom_css", ""),
|
| 317 |
+
"js": request.args.get("custom_js", "")
|
| 318 |
+
},
|
| 319 |
+
"appearance": {
|
| 320 |
+
"background": request.args.get("background_color", "#000000"),
|
| 321 |
+
"foreground": request.args.get("primary_color", "#ffffff"),
|
| 322 |
+
"accent": request.args.get("accent_color"),
|
| 323 |
+
},
|
| 324 |
+
"loadingScreen": {
|
| 325 |
+
"progressBar": request.args.get("show_progress_bar", "true") == "true",
|
| 326 |
+
"text": request.args.get("loading_text", ""),
|
| 327 |
+
"image": request.args.get("loading_image"),
|
| 328 |
+
},
|
| 329 |
+
"controls": {
|
| 330 |
+
"greenFlag": {"enabled": request.args.get("show_green_flag", "true") == "true"},
|
| 331 |
+
"stopAll": {"enabled": request.args.get("show_stop_button", "true") == "true"},
|
| 332 |
+
"pause": {"enabled": request.args.get("show_pause_button", "false") == "true"},
|
| 333 |
+
"fullscreen": {"enabled": request.args.get("show_fullscreen_button", "false") == "true"},
|
| 334 |
+
},
|
| 335 |
+
"monitors": {
|
| 336 |
+
"editableLists": request.args.get("editable_lists", "true") == "true",
|
| 337 |
+
"variableColor": request.args.get("variable_color"),
|
| 338 |
+
"listColor": request.args.get("list_color"),
|
| 339 |
+
},
|
| 340 |
+
"compiler": {
|
| 341 |
+
"enabled": request.args.get("enable_compiler", "false") == "true",
|
| 342 |
+
"warpTimer": request.args.get("warp_timer", "false") == "true",
|
| 343 |
+
},
|
| 344 |
+
"app": {
|
| 345 |
+
"icon": request.args.get("icon"),
|
| 346 |
+
"windowTitle": request.args.get("title", "Scratch Project"),
|
| 347 |
+
},
|
| 348 |
+
"chunks": {
|
| 349 |
+
"gamepad": request.args.get("enable_gamepad", "false") == "true",
|
| 350 |
+
"pointerlock": request.args.get("lock_mouse", "false") == "true",
|
| 351 |
+
},
|
| 352 |
+
"cloudVariables": {
|
| 353 |
+
"mode": request.args.get("cloud_mode", "ws"),
|
| 354 |
+
"cloudHost": request.args.get("cloud_host", "wss://clouddata.turbowarp.org"),
|
| 355 |
+
"custom": {},
|
| 356 |
+
"specialCloudBehaviors": request.args.get("htmlifier_cloud", "false") == "true",
|
| 357 |
+
"unsafeCloudBehaviors": request.args.get("unsafe_cloud", "false") == "true",
|
| 358 |
+
},
|
| 359 |
+
"extensions": [],
|
| 360 |
+
"bakeExtensions": request.args.get("cache_extensions", "false") == "true",
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
# 一時ファイルにオプションを保存
|
| 364 |
+
with tempfile.NamedTemporaryFile("w+", delete=False) as tmp:
|
| 365 |
+
json.dump(options, tmp)
|
| 366 |
+
tmp.flush()
|
| 367 |
+
tmp_path = tmp.name
|
| 368 |
+
|
| 369 |
+
# Node.jsスクリプトを呼び出す
|
| 370 |
+
try:
|
| 371 |
+
result = subprocess.run(
|
| 372 |
+
["node", "generate_html.js", project_id, tmp_path],
|
| 373 |
+
capture_output=True,
|
| 374 |
+
text=True,
|
| 375 |
+
check=True
|
| 376 |
+
)
|
| 377 |
+
html_content = result.stdout
|
| 378 |
+
return Response(html_content, mimetype="text/html")
|
| 379 |
+
except subprocess.CalledProcessError as e:
|
| 380 |
+
return jsonify({"error": e.stderr}), 500
|
| 381 |
+
|
| 382 |
if __name__ == "__main__":
|
| 383 |
if os.environ.get("WERKZEUG_RUN_MAIN") != "true":
|
| 384 |
start_processes()
|