Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.8 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    build_endpoint_schemas,
    detect,
)
from products.warehouse_sources.sources.tvmaze._config import TVMazeSourceConfig
from products.warehouse_sources.sources.tvmaze.settings import ENDPOINTS, INCREMENTAL_FIELDS
from products.warehouse_sources.sources.tvmaze.tvmaze import TVMazeResumeConfig, check_connection, tvmaze_source


@SourceRegistry.register
class TVMazeSource(ResumableSource[TVMazeSourceConfig, TVMazeResumeConfig]):
    codebase_detection = detect(env=("^(TV_MAZE|TVMAZE)_",))

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs
    # TVmaze exposes no version token (no /vN/ path, header, or dated release), so the
    # framework's unversioned default applies.
    api_docs_url = "https://www.tvmaze.com/api"

    @property
    def source_type(self) -> ExternalDataSourceType:
        return ExternalDataSourceType.TVMAZE

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        # The public API needs no credentials; a 401/403 means TVmaze is refusing the
        # caller (e.g. an IP-level block), which a retry cannot fix.
        return {
            "401 Client Error": "TVmaze rejected the request. Try again later.",
            "403 Client Error": "TVmaze rejected the request. Try again later.",
        }

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        from products.warehouse_sources.sources.tvmaze.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_schemas(
        self,
        config: TVMazeSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        return build_endpoint_schemas(ENDPOINTS, INCREMENTAL_FIELDS, names)

    def validate_credentials(
        self,
        config: TVMazeSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        # No credentials to validate — just confirm the public API is reachable.
        return check_connection()

    def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[TVMazeResumeConfig]:
        return ResumableSourceManager[TVMazeResumeConfig](inputs, TVMazeResumeConfig)

    def source_for_pipeline(
        self,
        config: TVMazeSourceConfig,
        resumable_source_manager: ResumableSourceManager[TVMazeResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return tvmaze_source(
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            resumable_source_manager=resumable_source_manager,
        )

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.TVMAZE,
            category=DataWarehouseSourceCategory.ANALYTICS,
            label="TVmaze",
            caption="Import TV show and people data from the free public TVmaze API. No API key is required. TVmaze data is licensed CC BY-SA, which requires attribution and share-alike.",
            docsUrl="https://posthog.com/docs/cdp/sources/tvmaze",
            iconPath="/static/services/tvmaze.png",
            keywords=["tv", "television", "tv shows"],
            fields=cast(list[FieldType], []),
            releaseStatus=ReleaseStatus.BETA,
        )