"""Pixiv — one quirk.

post_url: gallery-dl's `url` is the image URL on `i.pximg.net`. The
post permalink follows /artworks/<id>. external_post_id (= `id`) was
already correct, so no override there.
"""

from .base import GD_DEFAULTS, PlatformInfo, str_id_value


def derive_post_url(data: dict) -> str | None:
    pid = str_id_value(data.get("id"))
    if pid:
        return f"https://www.pixiv.net/artworks/{pid}"
    return None


INFO = PlatformInfo(
    key="pixiv",
    name="Pixiv",
    description="Download artwork from Pixiv artists",
    auth_type="token",
    requires_auth=True,
    url_pattern=r"^https?://(www\.)?pixiv\.net/",
    url_examples=[
        "https://www.pixiv.net/users/12345678",
        "https://www.pixiv.net/en/users/12345678",
    ],
    default_config={**GD_DEFAULTS, "content_types": ["all"]},
    notes="Requires OAuth refresh token. Run `gallery-dl oauth:pixiv` to obtain one.",
    derive_post_url=derive_post_url,
)
