caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

103 lines
3.6 KiB
Python

"""Orchestrates one app-listing preview: classify the URL, fetch the public
listing (reusing :mod:`app.domains.capture.sources` and ``.store`` — the same
modules the capture pipeline and the App Store audit already use), then write
a hook-script preview from the listing's own facts. Pure data in, pure dict
out — the router is the only place that touches HTTP status codes.
"""
from __future__ import annotations
from typing import Any, Callable
from app.domains.applisting.hookscript import HookPreview, generate_hook_preview
from app.domains.capture import store
from app.domains.capture.sources import KIND_AUTO, SourceError, classify
from app.domains.capture.store import StoreError
KIND_APP_STORE = "app_store"
KIND_PLAY_STORE = "play_store"
Getter = Callable[..., Any]
class ListingPreviewError(ValueError):
def __init__(self, status_code: int, message: str) -> None:
super().__init__(message)
self.status_code = status_code
self.message = message
def _listing_card(listing: dict[str, Any], *, source_kind: str) -> dict[str, Any]:
"""Everything the live listing card on `/from-app-store` renders."""
return {
"store": source_kind,
"url": listing.get("url") or "",
"title": listing.get("title") or "",
"developer": listing.get("developer") or "",
"description": listing.get("description") or "",
"icon_url": listing.get("icon_url") or "",
"screenshots": list(listing.get("screenshots") or [])[:8],
"rating": listing.get("rating"),
"rating_count": listing.get("rating_count"),
"price_label": listing.get("price_label") or "",
"genre": listing.get("genre") or "",
}
def build_preview(
url: str,
*,
requested_kind: str = KIND_AUTO,
getter: Getter | None = None,
chat: Any = None,
) -> dict[str, Any]:
"""Build the public, no-sign-in `/from-app-store` preview for a listing URL.
Raises :class:`ListingPreviewError` for anything user-presentable (bad
URL, store lookup failure). ``getter``/``chat`` are injection points for
tests; production callers leave them at their defaults.
"""
from app.domains.adstudio.fetch import safe_get
getter = getter or safe_get
try:
source = classify(url, requested_kind)
except SourceError as exc:
raise ListingPreviewError(422, str(exc)) from exc
if source.kind not in (KIND_APP_STORE, KIND_PLAY_STORE):
raise ListingPreviewError(422, "paste an App Store or Google Play app link to preview it")
try:
if source.kind == KIND_APP_STORE:
listing = store.fetch_app_store(source.ref, getter=getter)
else:
listing = store.fetch_play_store(source.ref, getter=getter)
except StoreError as exc:
raise ListingPreviewError(exc.status_code, exc.message) from exc
card = _listing_card(listing, source_kind=source.kind)
hook_kwargs: dict[str, Any] = {}
if chat is not None:
hook_kwargs["chat"] = chat
preview: HookPreview = generate_hook_preview(
card["title"], listing.get("description") or "", card["genre"], card["rating"], source.kind, **hook_kwargs,
)
return {
"input_url": url,
"listing": card,
"hook_preview": {
"hooks": [{"line": hook.line, "angle": hook.angle} for hook in preview.hooks],
"source": preview.source,
},
# The render itself (voiceover, avatar, captions, export) requires a free
# account; this preview never generates a video.
"render_requires_signup": True,
}
__all__ = ["KIND_APP_STORE", "KIND_PLAY_STORE", "ListingPreviewError", "build_preview"]