from fastapi import FastAPI from playwright.async_api import async_playwright import os app = FastAPI() @app.on_event("startup") async def on_startup(): """ アプリ起動時に、外部ページを読み込んで MHTML を保存。 """ url = "https://civitai.com/models/1055452/akashicpulse" output_path = "akashicpulse.mhtml" try: async with async_playwright() as p: browser = await p.chromium.launch(headless=True) page = await browser.new_page() await page.goto(url, wait_until="networkidle") cdp_session = await page.context.new_cdp_session(page) snapshot = await cdp_session.send("Page.captureSnapshot", {"format": "mhtml"}) await browser.close() # MHTMLをファイルに書き出し with open(output_path, "w", encoding="utf-8") as f: f.write(snapshot) print(f"[INFO] MHTML saved to: {os.path.abspath(output_path)}") except Exception as e: print(f"[ERROR] Failed to save MHTML: {e}") @app.get("/") def read_root(): return {"message": "Check logs for MHTML creation results."}