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 ¶
Comic ¶
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:
-
update_raw_image–Update the raw image of the comic.
-
__repr__–Return the representation of the comic.
Client ¶
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:
-
api_url(str, default:'https://xkcd.com') –The URL of the xkcd API, by default "https://xkcd.com"
-
explanation_wiki_url(str, default:'https://www.explainxkcd.com/wiki/index.php/') –The URL of the xkcd explanation wiki, by default "https://www.explainxkcd.com/wiki/index.php/"
Methods:
-
latest_comic_url–Get the URL for the latest comic.
-
comic_id_url–Get the URL for a specific comic ID.
-
get_latest_comic–Get the latest xkcd comic.
-
get_comic–Get a specific xkcd comic.
-
get_random_comic–Get a random xkcd comic.
-
__repr__–Return the representation of the client.
Functions¶
comic_id_url ¶
_parse_response ¶
_fetch_comic ¶
get_latest_comic ¶
get_comic ¶
get_random_comic ¶
_request_comic ¶
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:
-
TuxAPIConnectionError–If connection to xkcd API fails.
-
TuxAPIRequestError–If the API request fails.
-
TuxAPIResourceNotFoundError–If the comic is not found.
_request_raw_image staticmethod ¶
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:
-
TuxAPIConnectionError–If connection to xkcd API fails.
-
TuxAPIRequestError–If the API request fails.
-
TuxAPIResourceNotFoundError–If the image is not found or URL is not provided.