Skip to main content

A Python library for The Trivia API

Project description

The Trivia API Python Library

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"
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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

the_trivia_api_library-0.0.3-py3-none-any.whl (51.7 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for the_trivia_api_library-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e4dfb1c26eab8ad22e5105a27876e311511c93052685e0c1a59e33cd3e776536
MD5 0fd71b98ec7622e65bbae183fd4d2794
BLAKE2b-256 3ad21f9b23c959fe632d3c96f78af325f8b6bb72251b847b83996465d74d905c

See more details on using hashes here.

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