Update main.py
Browse files
main.py
CHANGED
@@ -1,32 +1,37 @@
|
|
1 |
from fastapi import FastAPI
|
2 |
-
from playwright.
|
|
|
3 |
|
4 |
app = FastAPI()
|
5 |
|
6 |
@app.on_event("startup")
|
7 |
-
def on_startup():
|
8 |
-
|
|
|
|
|
9 |
url = "https://civitai.com/models/1055452/akashicpulse"
|
10 |
output_path = "akashicpulse.mhtml"
|
11 |
|
12 |
try:
|
13 |
-
with
|
14 |
-
browser = p.chromium.launch(headless=True)
|
15 |
-
page = browser.new_page()
|
16 |
-
page.goto(url, wait_until="networkidle")
|
17 |
|
18 |
-
|
19 |
-
snapshot =
|
20 |
|
21 |
-
browser.close()
|
22 |
|
|
|
23 |
with open(output_path, "w", encoding="utf-8") as f:
|
24 |
f.write(snapshot)
|
25 |
-
|
|
|
26 |
|
27 |
except Exception as e:
|
28 |
print(f"[ERROR] Failed to save MHTML: {e}")
|
29 |
|
30 |
@app.get("/")
|
31 |
def read_root():
|
32 |
-
return {"message": "
|
|
|
1 |
from fastapi import FastAPI
|
2 |
+
from playwright.async_api import async_playwright
|
3 |
+
import os
|
4 |
|
5 |
app = FastAPI()
|
6 |
|
7 |
@app.on_event("startup")
|
8 |
+
async def on_startup():
|
9 |
+
"""
|
10 |
+
アプリ起動時に、外部ページを読み込んで MHTML を保存。
|
11 |
+
"""
|
12 |
url = "https://civitai.com/models/1055452/akashicpulse"
|
13 |
output_path = "akashicpulse.mhtml"
|
14 |
|
15 |
try:
|
16 |
+
async with async_playwright() as p:
|
17 |
+
browser = await p.chromium.launch(headless=True)
|
18 |
+
page = await browser.new_page()
|
19 |
+
await page.goto(url, wait_until="networkidle")
|
20 |
|
21 |
+
cdp_session = await page.context.new_cdp_session(page)
|
22 |
+
snapshot = await cdp_session.send("Page.captureSnapshot", {"format": "mhtml"})
|
23 |
|
24 |
+
await browser.close()
|
25 |
|
26 |
+
# MHTMLをファイルに書き出し
|
27 |
with open(output_path, "w", encoding="utf-8") as f:
|
28 |
f.write(snapshot)
|
29 |
+
|
30 |
+
print(f"[INFO] MHTML saved to: {os.path.abspath(output_path)}")
|
31 |
|
32 |
except Exception as e:
|
33 |
print(f"[ERROR] Failed to save MHTML: {e}")
|
34 |
|
35 |
@app.get("/")
|
36 |
def read_root():
|
37 |
+
return {"message": "Check logs for MHTML creation results."}
|