ttttdiva commited on
Commit
69a2710
·
verified ·
1 Parent(s): 14138da

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +17 -12
main.py CHANGED
@@ -1,32 +1,37 @@
1
  from fastapi import FastAPI
2
- from playwright.sync_api import sync_playwright
 
3
 
4
  app = FastAPI()
5
 
6
  @app.on_event("startup")
7
- def on_startup():
8
- # 起動時に自動的にMHTMLを取得
 
 
9
  url = "https://civitai.com/models/1055452/akashicpulse"
10
  output_path = "akashicpulse.mhtml"
11
 
12
  try:
13
- with sync_playwright() as p:
14
- browser = p.chromium.launch(headless=True)
15
- page = browser.new_page()
16
- page.goto(url, wait_until="networkidle")
17
 
18
- cdp = page.context.new_cdp_session(page)
19
- snapshot = cdp.send("Page.captureSnapshot", {"format": "mhtml"})
20
 
21
- browser.close()
22
 
 
23
  with open(output_path, "w", encoding="utf-8") as f:
24
  f.write(snapshot)
25
- print(f"[INFO] MHTML saved to: {output_path}")
 
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": "Startup event attempted to save MHTML to akashicpulse.mhtml."}
 
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."}