Commit
·
c9d5bf0
1
Parent(s):
df4a46f
Building new logic
Browse files- screenshot.py +20 -9
screenshot.py
CHANGED
|
@@ -4,9 +4,14 @@ from playwright.async_api import async_playwright
|
|
| 4 |
import asyncio
|
| 5 |
import base64
|
| 6 |
import time
|
| 7 |
-
from typing import Optional
|
|
|
|
|
|
|
|
|
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
|
|
|
| 10 |
|
| 11 |
class AnalysisResult(BaseModel):
|
| 12 |
url: str
|
|
@@ -17,22 +22,21 @@ class AnalysisResult(BaseModel):
|
|
| 17 |
seo_flags: List[str]
|
| 18 |
accessibility_flags: List[str]
|
| 19 |
screenshot_base64: str
|
| 20 |
-
|
| 21 |
|
| 22 |
@app.get("/analyze", response_model=AnalysisResult)
|
| 23 |
async def analyze_website(url: str):
|
| 24 |
try:
|
| 25 |
async with async_playwright() as p:
|
| 26 |
browser = await p.chromium.launch(headless=True)
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
# Start timing
|
| 30 |
start_time = time.time()
|
| 31 |
-
response = await page.goto(url, timeout=
|
| 32 |
-
load_time = round(time.time() - start_time, 2)
|
| 33 |
-
|
| 34 |
-
# Wait for content
|
| 35 |
await page.wait_for_load_state("networkidle")
|
|
|
|
| 36 |
|
| 37 |
# Screenshot
|
| 38 |
screenshot = await page.screenshot(full_page=True)
|
|
@@ -63,6 +67,8 @@ async def analyze_website(url: str):
|
|
| 63 |
accessibility_flags.append("Image without alt attribute")
|
| 64 |
break
|
| 65 |
|
|
|
|
|
|
|
| 66 |
await browser.close()
|
| 67 |
|
| 68 |
return AnalysisResult(
|
|
@@ -73,7 +79,12 @@ async def analyze_website(url: str):
|
|
| 73 |
og_image=og_image,
|
| 74 |
seo_flags=seo_flags,
|
| 75 |
accessibility_flags=accessibility_flags,
|
| 76 |
-
screenshot_base64=screenshot_base64
|
|
|
|
| 77 |
)
|
| 78 |
except Exception as e:
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import asyncio
|
| 5 |
import base64
|
| 6 |
import time
|
| 7 |
+
from typing import Optional, List
|
| 8 |
+
import uvicorn
|
| 9 |
+
import logging
|
| 10 |
+
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
+
logging.basicConfig(level=logging.INFO)
|
| 14 |
+
logger = logging.getLogger("analyzer")
|
| 15 |
|
| 16 |
class AnalysisResult(BaseModel):
|
| 17 |
url: str
|
|
|
|
| 22 |
seo_flags: List[str]
|
| 23 |
accessibility_flags: List[str]
|
| 24 |
screenshot_base64: str
|
| 25 |
+
status_code: Optional[int] = None
|
| 26 |
|
| 27 |
@app.get("/analyze", response_model=AnalysisResult)
|
| 28 |
async def analyze_website(url: str):
|
| 29 |
try:
|
| 30 |
async with async_playwright() as p:
|
| 31 |
browser = await p.chromium.launch(headless=True)
|
| 32 |
+
context = await browser.new_context()
|
| 33 |
+
page = await context.new_page()
|
| 34 |
|
| 35 |
# Start timing
|
| 36 |
start_time = time.time()
|
| 37 |
+
response = await page.goto(url, timeout=60000, wait_until='domcontentloaded')
|
|
|
|
|
|
|
|
|
|
| 38 |
await page.wait_for_load_state("networkidle")
|
| 39 |
+
load_time = round(time.time() - start_time, 2)
|
| 40 |
|
| 41 |
# Screenshot
|
| 42 |
screenshot = await page.screenshot(full_page=True)
|
|
|
|
| 67 |
accessibility_flags.append("Image without alt attribute")
|
| 68 |
break
|
| 69 |
|
| 70 |
+
status_code = response.status if response else None
|
| 71 |
+
|
| 72 |
await browser.close()
|
| 73 |
|
| 74 |
return AnalysisResult(
|
|
|
|
| 79 |
og_image=og_image,
|
| 80 |
seo_flags=seo_flags,
|
| 81 |
accessibility_flags=accessibility_flags,
|
| 82 |
+
screenshot_base64=screenshot_base64,
|
| 83 |
+
status_code=status_code
|
| 84 |
)
|
| 85 |
except Exception as e:
|
| 86 |
+
logger.error(f"Analysis failed for {url}: {str(e)}")
|
| 87 |
+
raise HTTPException(status_code=500, detail=f"Error analyzing {url}: {str(e)}")
|
| 88 |
+
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|