OpenTDBpy
An API wrapper for interacting with the OpenTDB API.
Introduction
In order to being working with this API, you must create a client.
import opentdbpy
client = opentdbpy.Client()
Functionality
There are two main components when working with the client.
client.get_categories()
client.get_category()
or
client.get_questions()
When you make a call with get_categories()
, the entries are cached for each time it is used.
The same applies when using get_category()
.
Note: The cache is cleared every hour.
Using get_categories()
will return a list of Category
objects.
Using get_category()
can take 2 possible arguments. Which are the id and name.
You can pass either or, and even both. This will return a Category
object containing useful information about the category. If there are no results, the return value will be None.
When you wish to get questions from the api, use get_questions()
.
There are four possible arguments you can pass. These include: amount, category, type, and use_token.
By default, using this function will not use your session token. Setting use_token to true will use it.
This will require you to either pass in a previous session token, or generate a new one with get_token()
.
The amount takes an integer, which will return that number of questions. The category takes either an integer or a string, you can pass in the category id or the name. The type takes a string, which determines the question type.
Tokens
In OpenTDB, tokens are used to filter out questions you have already pulled.
If you wish to use a token, you can either manually create one and pass it into Client
. Or, you can use client.get_token()
to create a new one.
If your client has a session token and you wish to reset it's data, use reset_token()
.
If you want to change your session token to a manual one on runtime, you can use set_token()
. If you don't want to manually create a session token, run get_token()
to pull a new one automatically.