A Python library for The Trivia API
Project description
The Trivia API Python Library
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.
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"
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"
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
File details
Details for the file the_trivia_api_library-0.0.2.tar.gz.
File metadata
- Download URL: the_trivia_api_library-0.0.2.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61d45ff85a7343bee7638ce1186af36923c7fb60ea1a154077a14c96905eb3e9
|
|
| MD5 |
05b8e5262b2f201f83838c501b0f8599
|
|
| BLAKE2b-256 |
ab82f3235b79009cfd95e4f3221cbcb11cc5eae95250aaeb3c694ceac98c9717
|