from fastapi import FastAPI from playwright.sync_api import sync_playwright app = FastAPI() @app.on_event("startup") def on_startup(): # 起動時に自動的にMHTMLを取得 url = "https://civitai.com/models/1055452/akashicpulse" output_path = "akashicpulse.mhtml" try: with sync_playwright() as p: browser = p.chromium.launch(headless=True) page = browser.new_page() page.goto(url, wait_until="networkidle") cdp = page.context.new_cdp_session(page) snapshot = cdp.send("Page.captureSnapshot", {"format": "mhtml"}) browser.close() with open(output_path, "w", encoding="utf-8") as f: f.write(snapshot) print(f"[INFO] MHTML saved to: {output_path}") except Exception as e: print(f"[ERROR] Failed to save MHTML: {e}") @app.get("/") def read_root(): return {"message": "Startup event attempted to save MHTML to akashicpulse.mhtml."}