Skip to content

Kronopt/xkcd-wrapper

Repository files navigation

xkcd-wrapper

python versions build status code style: black coverage vulnerabilities docs status license

pypi pypi downloads

A Python wrapper for the xkcd webcomic API.

Retrieves xkcd comic data and metadata as python objects.

Asynchronous (async) and synchronous implementations.

Installation

At the command line, with pip,

synchronous implementation:

$ pip install xkcd-wrapper[sync]

async implementation:

$ pip install xkcd-wrapper[async]

Usage

synchronous:

>>> import xkcd_wrapper

>>> client = xkcd_wrapper.Client()
>>> specific_comic = client.get(100)        # Comic object with comic 100 data
>>> latest_comic = client.get_latest()      # Comic object containing data of the latest xkcd comic
>>> random_comic = client.get_random()      # Comic object of a random comic

>>> specific_comic
xkcd_wrapper.Comic(100)
>>> specific_comic.image_url
'https://imgs.xkcd.com/comics/family_circus.jpg'

async:

>>> import xkcd_wrapper, asyncio
>>> async_client = xkcd_wrapper.AsyncClient()

>>> async def async_call():
...     responses = await asyncio.gather(
...         async_client.get(100),          # Comic object with comic 100 data
...         async_client.get_latest(),      # Comic object containing data of the latest xkcd comic
...         async_client.get_random()       # Comic object of a random comic
...     )
...     print(
...         responses[0],                   # async_client.get(100) output
...         responses[0].image_url,
...         sep='\n'
...     )

>>> asyncio.run(async_call())
xkcd_wrapper.Comic(100)
'https://imgs.xkcd.com/comics/family_circus.jpg'

Documentation

Check the documentation for more details: https://xkcd-wrapper.readthedocs.io/en/latest