Put app/services/asset_service.py
Browse files
app/services/asset_service.py
CHANGED
|
@@ -4,6 +4,8 @@ Each extractor produces a list of absolute asset URLs. Results are
|
|
| 4 |
collected into five categories: image, video, audio, svg_icon, font.
|
| 5 |
"""
|
| 6 |
|
|
|
|
|
|
|
| 7 |
from bs4 import BeautifulSoup
|
| 8 |
|
| 9 |
from app.schemas import AssetExtractResponse, AssetGroup
|
|
@@ -11,6 +13,8 @@ from app.schemas import AssetExtractResponse, AssetGroup
|
|
| 11 |
from .extractors import EXTRACTORS
|
| 12 |
from .fetcher import FetchError, fetch_html
|
| 13 |
|
|
|
|
|
|
|
| 14 |
|
| 15 |
class AssetExtractError(Exception):
|
| 16 |
"""Raised when asset extraction fails."""
|
|
@@ -31,17 +35,24 @@ async def extract_assets(
|
|
| 31 |
) -> AssetExtractResponse:
|
| 32 |
"""Fetch a page and return its assets grouped by category."""
|
| 33 |
url = normalize_url(url)
|
|
|
|
|
|
|
| 34 |
|
| 35 |
try:
|
| 36 |
final_url, html, title = await fetch_html(url)
|
| 37 |
except FetchError as exc:
|
|
|
|
| 38 |
raise AssetExtractError(str(exc)) from exc
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
soup = BeautifulSoup(html, "html.parser")
|
| 41 |
|
| 42 |
# Determine which categories to run. Default to all.
|
| 43 |
selected = groups if groups else list(EXTRACTORS.keys())
|
| 44 |
selected = [g for g in selected if g in EXTRACTORS]
|
|
|
|
| 45 |
|
| 46 |
grouped: dict[str, list[str]] = {
|
| 47 |
"image": [],
|
|
@@ -55,11 +66,16 @@ async def extract_assets(
|
|
| 55 |
label, extractor = EXTRACTORS[key]
|
| 56 |
try:
|
| 57 |
urls = extractor(soup, final_url, include_data_uris)
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
urls = []
|
| 60 |
grouped[label] = _dedupe(urls)
|
|
|
|
| 61 |
|
| 62 |
total = sum(len(v) for v in grouped.values())
|
|
|
|
|
|
|
| 63 |
return AssetExtractResponse(
|
| 64 |
url=final_url,
|
| 65 |
title=title,
|
|
|
|
| 4 |
collected into five categories: image, video, audio, svg_icon, font.
|
| 5 |
"""
|
| 6 |
|
| 7 |
+
import logging
|
| 8 |
+
|
| 9 |
from bs4 import BeautifulSoup
|
| 10 |
|
| 11 |
from app.schemas import AssetExtractResponse, AssetGroup
|
|
|
|
| 13 |
from .extractors import EXTRACTORS
|
| 14 |
from .fetcher import FetchError, fetch_html
|
| 15 |
|
| 16 |
+
logger = logging.getLogger(__name__)
|
| 17 |
+
|
| 18 |
|
| 19 |
class AssetExtractError(Exception):
|
| 20 |
"""Raised when asset extraction fails."""
|
|
|
|
| 35 |
) -> AssetExtractResponse:
|
| 36 |
"""Fetch a page and return its assets grouped by category."""
|
| 37 |
url = normalize_url(url)
|
| 38 |
+
logger.info("extract_assets start url=%r groups=%r include_data_uris=%s",
|
| 39 |
+
url, groups, include_data_uris)
|
| 40 |
|
| 41 |
try:
|
| 42 |
final_url, html, title = await fetch_html(url)
|
| 43 |
except FetchError as exc:
|
| 44 |
+
logger.warning("extract_assets fetch failed url=%r error=%r", url, str(exc))
|
| 45 |
raise AssetExtractError(str(exc)) from exc
|
| 46 |
|
| 47 |
+
logger.info("extract_assets fetched final_url=%r bytes=%d title=%r",
|
| 48 |
+
final_url, len(html), title)
|
| 49 |
+
|
| 50 |
soup = BeautifulSoup(html, "html.parser")
|
| 51 |
|
| 52 |
# Determine which categories to run. Default to all.
|
| 53 |
selected = groups if groups else list(EXTRACTORS.keys())
|
| 54 |
selected = [g for g in selected if g in EXTRACTORS]
|
| 55 |
+
logger.info("extract_assets selected_groups=%r", selected)
|
| 56 |
|
| 57 |
grouped: dict[str, list[str]] = {
|
| 58 |
"image": [],
|
|
|
|
| 66 |
label, extractor = EXTRACTORS[key]
|
| 67 |
try:
|
| 68 |
urls = extractor(soup, final_url, include_data_uris)
|
| 69 |
+
logger.info("extract_assets group=%s raw_count=%d", key, len(urls))
|
| 70 |
+
except Exception as exc: # noqa: BLE001 - isolate a failing extractor
|
| 71 |
+
logger.exception("extract_assets group=%s failed: %s", key, exc)
|
| 72 |
urls = []
|
| 73 |
grouped[label] = _dedupe(urls)
|
| 74 |
+
logger.info("extract_assets group=%s deduped_count=%d", key, len(grouped[label]))
|
| 75 |
|
| 76 |
total = sum(len(v) for v in grouped.values())
|
| 77 |
+
logger.info("extract_assets done url=%r total=%d counts=%r",
|
| 78 |
+
final_url, total, {k: len(v) for k, v in grouped.items()})
|
| 79 |
return AssetExtractResponse(
|
| 80 |
url=final_url,
|
| 81 |
title=title,
|