Client#

class trivio.client.Client(use_token: bool = False)#

Represents a TrivIO client to connect to the opentdb.com Trivia API.

There’s one option that can be passed to the Client.

Parameters:

use_token (Optional[bool]) – Determines whether the client should use a session token or not. Session Tokens are unique keys that will help keep track of the questions the API has already retrieved. By appending a Session Token to an API Call, the API will never give you the same question twice. Over the lifespan of a Session Token, there will eventually reach a point where you have exhausted all the possible questions in the database. At this point the api will automatically create a new token for you.

async close()#

This function is a coroutine.

Closes the Client and the connection to the api.

async request(_type: Type, amount: int, category: Category = None, difficulty: Difficulty = None) Response#

This function is a coroutine.

Makes a request to the trivia api and returns questions according to the given query

Parameters:
  • _type (trivio.enums.Type) – Determines whether returned questions should be multiple choice or yes/no questions.

  • amount (int) – The number of questions that should be returned the number of returned questions must be between 1 and 50.

  • category (Optional[trivio.enums.Category]) – The category of the returned questions. When leaving empty there will be questions from a random category.

  • difficulty (Optional[trivio.enums.Difficulty]) – The difficulty of the returned questions. When leaving empty there will be questions from all difficulties.

Returns:

The requested questions in a list.

Return type:

trivio.models.Response

Models#

class trivio.models.Question(data: Dict[str, Union[str, List[str]]])#

This object represents a single question from an api call.

Parameters:
  • category (str) – The category of the question.

  • type (trivio.enums.Type) – The type of the question

  • difficulty (trivio.enums.Difficulty) – The difficulty of this question.

  • correct_answer (str) – The correct answer of the question.

  • wrong_answers (List[str]) – The wrong answers to the question.

property all_answers: List[str]#

Returns all answers for the question (correct and wrong).

Returns:

All the possible answers for the question.

Return type:

List[str]

class trivio.models.Response(data: Dict[str, Union[int, List[Dict[str, Union[str, List[str]]]]]])#

This object represents a response from the api.

Parameters:
  • response_code (bool) – The response code to indicate whether the results returned successfully.

  • results (List[trivio.enums.Question]) – The list of questions given by the result.

  • token (str) – The token returned by the request. This will mostly be None as this is only used for generating tokens. As the library handles the tokens this probably has no use case.

Enums#

class trivio.enums.Category(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)#

The Different Categories, to get questions from.

ALL = 0#
ANIMALS = 27#
ANIME_MANGA = 31#
ART = 25#
BOARD_GAMES = 16#
BOOKS = 10#
CARTOON_ANIMATIONS = 32#
CELEBRITIES = 26#
COMICS = 29#
COMPUTERS = 18#
FILM = 11#
GADGETS = 30#
GENERAL_KNOWLEDGE = 9#
GEOGRAPHY = 22#
HISTORY = 23#
MATHEMATICS = 19#
MUSIC = 12#
MUSICALS_THEATRES = 13#
MYTHOLOGY = 20#
POLITICS = 24#
SCIENCE_NATURE = 17#
SPORTS = 21#
TELEVISION = 14#
VEHICLES = 28#
VIDEO_GAMES = 15#
class trivio.enums.Difficulty(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)#

The Different Difficulties, to get questions from.

EASY = 'easy'#
HARD = 'hard'#
MEDIUM = 'medium'#
class trivio.enums.Type(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)#

The Different Types of questions, to get questions from.

MULTIPLE_CHOICE = 'multiple'#
REQUEST = 'request'#
TRUE_FALSE = 'boolean'#

Utils#

class trivio.utils.Utils#

Class with helping functions

build_list(item) dict#

Decodes a given query

Exceptions#

exception trivio.exceptions.BaseError(message: str)#

“The Base class for all Exceptions

exception trivio.exceptions.InvalidParameter#

“Raised when there is an invalid Parameter in the given query

exception trivio.exceptions.NoResultFound#

Raised when the opentdb database hasn’t enough questions for the given query.

exception trivio.exceptions.TokenEmpty#

Raised when the token has returned all possible questions for the specified query. Resetting the token is necessary.

Note

This can also mean that there aren’t enough results in the opentdb database. For some reason the API also raises does not raise NoResultFound in this case.