Skip to content

xkcd

xkcd Comics API Wrapper for Tux Bot.

This module provides integration with the xkcd webcomic API, allowing the bot to fetch and display xkcd comics with full metadata and image processing capabilities.

Classes:

  • HttpError

    Exception raised for HTTP-related errors in xkcd API calls.

  • Comic

    A class representing an xkcd comic.

  • Client

    xkcd API client for fetching and managing comics.

Classes

HttpError

Python
HttpError(status_code: int, reason: str)

Bases: Exception

Exception raised for HTTP-related errors in xkcd API calls.

Initialize the HttpError.

Parameters:

  • status_code (int) –

    The status code of the error.

  • reason (str) –

    The reason of the error.

Functions

Comic

Python
Comic(
    xkcd_dict: dict[str, Any],
    raw_image: bytes | None = None,
    comic_url: str | None = None,
    explanation_url: str | None = None,
)

A class representing an xkcd comic.

Initialize a Comic instance from xkcd API data.

Parameters:

  • xkcd_dict (dict[str, Any]) –

    Dictionary containing xkcd comic metadata from the API.

  • raw_image (bytes, default: None ) –

    Raw image data for the comic.

  • comic_url (str, default: None ) –

    Direct URL to the comic page.

  • explanation_url (str, default: None ) –

    URL to the comic explanation.

Methods:

Functions

_determine_date staticmethod
Python
_determine_date(xkcd_dict: dict[str, Any]) -> date | None

Determine the date of the comic.

Parameters:

  • xkcd_dict (dict[str, Any]) –

    The dictionary containing the comic data.

Returns:

  • date | None

    The date of the comic.

_determine_image_extension
Python
_determine_image_extension() -> str | None

Determine the image extension of the comic.

Returns:

  • str | None

    The extension of the image.

update_raw_image
Python
update_raw_image(raw_image: bytes) -> None

Update the raw image of the comic.

Parameters:

  • raw_image (bytes) –

    The raw image data.

__repr__
Python
__repr__() -> str

Return the representation of the comic.

Returns:

  • str

    The representation of the comic.

Client

Python
Client(
    api_url: str = "https://xkcd.com",
    explanation_wiki_url: str = "https://www.explainxkcd.com/wiki/index.php/",
)

xkcd API client for fetching and managing comics.

Initialize the Client.

Parameters:

Methods:

Functions

latest_comic_url
Python
latest_comic_url() -> str

Get the URL for the latest comic.

Returns:

  • str

    The URL for the latest comic.

comic_id_url
Python
comic_id_url(comic_id: int) -> str

Get the URL for a specific comic ID.

Parameters:

  • comic_id (int) –

    The ID of the comic.

Returns:

  • str

    The URL for the specific comic ID.

_parse_response
Python
_parse_response(response_text: str) -> Comic

Parse the response text into a Comic object.

Parameters:

  • response_text (str) –

    The response text to parse.

Returns:

  • Comic

    The parsed comic object.

_fetch_comic
Python
_fetch_comic(comic_id: int, raw_comic_image: bool) -> Comic

Fetch a comic from the xkcd API.

Parameters:

  • comic_id (int) –

    The ID of the comic to fetch.

  • raw_comic_image (bool) –

    Whether to fetch the raw image data.

Returns:

  • Comic

    The fetched comic.

get_latest_comic
Python
get_latest_comic(raw_comic_image: bool = False) -> Comic

Get the latest xkcd comic.

Parameters:

  • raw_comic_image (bool, default: False ) –

    Whether to fetch the raw image data, by default False

Returns:

  • Comic

    The latest xkcd comic.

get_comic
Python
get_comic(comic_id: int, raw_comic_image: bool = False) -> Comic

Get a specific xkcd comic.

Parameters:

  • comic_id (int) –

    The ID of the comic to fetch.

  • raw_comic_image (bool, default: False ) –

    Whether to fetch the raw image data, by default False

Returns:

  • Comic

    The fetched xkcd comic.

get_random_comic
Python
get_random_comic(raw_comic_image: bool = False) -> Comic

Get a random xkcd comic.

Parameters:

  • raw_comic_image (bool, default: False ) –

    Whether to fetch the raw image data, by default False

Returns:

  • Comic

    The random xkcd comic.

_request_comic
Python
_request_comic(comic_id: int) -> str

Request the comic data from the xkcd API.

Parameters:

  • comic_id (int) –

    The ID of the comic to fetch.

Returns:

  • str

    The response text.

Raises:

_request_raw_image staticmethod
Python
_request_raw_image(raw_image_url: str | None) -> bytes

Request the raw image data from the xkcd API.

Parameters:

  • raw_image_url (str | None) –

    The URL of the raw image data.

Returns:

  • bytes

    The raw image data.

Raises:

__repr__
Python
__repr__() -> str

Return the representation of the client.

Returns:

  • str

    The representation of the client.