Skip to main content

A Python library for The Trivia API

Project description

The Trivia API Python Library

PyPI version PyPI downloads Python Versions License


A Python library for interacting with the Trivia API. This library allows you to easily request multiple-choice trivia questions and access various features of the Trivia API.

Demo

Access a demo of this library here.

example demo

Installation

You can install this library using pip:

pip install the_trivia_api_library

Example usage

Here's an example of how to use this library to interact with the Trivia API:

from the_trivia_api_library import TriviaAPIClient, EnumCategory, EnumDifficulty

# Initialize the Trivia API client
api_key = "YOUR_API_KEY" # it's not necessary
client = TriviaAPIClient(api_key=api_key)

# Get a random set of questions
questions = client.get_random_question(
    limit=5,
    categories=[EnumCategory.SCIENCE.value],
    difficulties=[EnumDifficulty.EASY.value],
    tags=["math", "physics"]
)

for question in questions:
    print(f"Question: {question['question']['text']}")
    print(f"Correct Answer: {question['correctAnswer']}")
    print(f"Incorrect Answers: {', '.join(question['incorrectAnswers'])}")
    print()

Methods

get_random_question

Get a random set of questions from the Trivia API.

Parameters

  • limit - The number of questions to return. Defaults to 1.
  • categories - A list of categories to filter the questions by. Defaults to None.
  • difficulties - A list of difficulties to filter the questions by. Defaults to None.
  • region - A list of regions to filter the questions by. Defaults to None.
  • tags - A list of tags to filter the questions by. Defaults to None.
  • types - A list of types to filter the questions by. Defaults to None.
  • session - A session object to use for the request. Defaults to None.
  • preview - A boolean indicating whether to return the questions in preview mode. Defaults to False.

Returns

A list of questions.

Example

from the_trivia_api_library import TriviaAPIClient, EnumCategory, EnumDifficulty

# Initialize the Trivia API client
api_key = "YOUR_API_KEY"
client = TriviaAPIClient(api_key=api_key)

# Get a random set of questions
questions = client.get_random_question(
    limit=5,
    categories=[EnumCategory.SCIENCE.value],
    difficulties=[EnumDifficulty.EASY.value],
    tags=["math", "physics"]
)

for question in questions:
    print(f"Question: {question['question']['text']}")
    print(f"Correct Answer: {question['correctAnswer']}")
    print(f"Incorrect Answers: {', '.join(question['incorrectAnswers'])}")
    print()

get_categories

Get a list of categories from the Trivia API.

Parameters

  • None

Returns

A list of categories.

Example

from the_trivia_api_library import TriviaAPIClient

api_key = "YOUR_API_KEY"
client = TriviaAPIClient(api_key=api_key)

tags = client.get_all_tags()

for tag in tags:
    print(tag)

get_totals_per_tag

Get the total number of questions per tag from the Trivia API.

Parameters

  • categories - A list of categories to filter the questions by. Defaults to None.
  • difficulties - A list of difficulties to filter the questions by. Defaults to None.
  • region - A list of regions to filter the questions by. Defaults to None.
  • tags - A list of tags to filter the questions by. Defaults to None.
  • types - A list of types to filter the questions by. Defaults to None.

Returns

A list of totals per tag.

Example

from the_trivia_api_library import TriviaAPIClient

api_key = "YOUR_API_KEY" #it's not necessary
client = TriviaAPIClient(api_key=api_key)

tags = client.get_totals_per_tag()

for tag in tags:
    print(f"tag: {tag} -> total: {tags[tag]}")

Initialize the Trivia API client

Contributing

Contributions are welcome! If you have suggestions, bug reports, or want to contribute to the library, please follow the guidelines in the CONTRIBUTING.md file.

License

This library is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

the_trivia_api_library-0.0.5.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

the_trivia_api_library-0.0.5-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file the_trivia_api_library-0.0.5.tar.gz.

File metadata

  • Download URL: the_trivia_api_library-0.0.5.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for the_trivia_api_library-0.0.5.tar.gz
Algorithm Hash digest
SHA256 fd76639b03b0bd0a81eb50b1dad4663bd122b3b14992e6e4745c0c263f68cbc9
MD5 21b19fd02261b154b90e08447a3dd6a6
BLAKE2b-256 a1afd6249b6a064e102687fc9a68ef58123a60e0a459c5c197686ca4d59ce255

See more details on using hashes here.

Provenance

The following attestation bundles were made for the_trivia_api_library-0.0.5.tar.gz:

Publisher: python-publish.yml on Carlos-Henreis/the-trivia-api

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file the_trivia_api_library-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for the_trivia_api_library-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 34ebef93b0a0356175a3caf27d7375d0e9d7176631a52bfca80d3987e550f54c
MD5 d16cd38fa6e588cd02d80deec369bdad
BLAKE2b-256 72348b374dfbd53521b913805cdbbecf964d7dea60226b3698ad846465c47466

See more details on using hashes here.

Provenance

The following attestation bundles were made for the_trivia_api_library-0.0.5-py3-none-any.whl:

Publisher: python-publish.yml on Carlos-Henreis/the-trivia-api

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page