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.
Access a demo of this library here.
You can install this library using pip:
pip install the_trivia_api_library
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"
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 a random set of questions from the Trivia API.
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.
A list of questions.
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 a list of categories from the Trivia API.
- None
A list of categories.
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 the total number of questions per tag from the Trivia API.
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.
A list of totals per tag.
from the_trivia_api_library import TriviaAPIClient
api_key = "YOUR_API_KEY"
client = TriviaAPIClient(api_key=api_key)
tags = client.get_totals_per_tag()
for tag in tags:
print(f"tag: {tag} -> total: {tags[tag]}")
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.
This library is licensed under the MIT License - see the LICENSE file for details.