Skip to main content

Python wrapper for the Open Trivia Database

Project description

python-open-trivia-database-wrapper

A Python wrapper for the Open Trivia Database API.

To intall the package, run the following command:

pip install python-open-trivia-database-wrapper

Usage

from opentdb.opentdb import openTriviaDB, Parameters

trivia = openTriviaDB()
response = trivia.get_questions(category=Parameters.Category.GENERAL_KNOWLEDGE, number_of_questions=1)

The Parameters class is a helper class that contains enums for the different parameters that can be passed to the get_questions method . The get_questions method can take the following parameters:

category: Parameters.Category
difficulty: Parameters.Difficulty
question_type: Parameters.QuestionType
number_of_questions: int

Example of all the parameters being used:

response = get_questions(category=Parameters.Category.GENERAL_KNOWLEDGE,
                         difficulty=Parameters.Difficulty.EASY,
                         question_type=Parameters.QuestionType.MULTIPLE,
                         number_of_questions=15)

If no parameters are passed in, then a set of 10 random questions will be returned

Response

Example json response from API:

{
    "response_code": 0,
    "results": [
        {
            "category": "General Knowledge",
            "type": "multiple",
            "difficulty": "easy",
            "question": "What is the most common surname Wales?",
            "correct_answer": "Jones",
            "incorrect_answers": [
                "Williams",
                "Davies",
                "Evans"
            ]
        }
    ]
}

In code, the response is modelled by a Result object, which has the following attributes:

response_code: int
results: List[Question]

The Question object has the following attributes:

type: str
difficulty: str
category: str
question: str
correct_answer: str
incorrect_answers: List[str]

So when looping over the list of results, you can access the attributes like so:

for result in response.results:
    print(result.question)
    print(result.correct_answer)
    print(result.incorrect_answers)

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.

open_trivia_database_wrapper-0.3.2-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

Details for the file open_trivia_database_wrapper-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for open_trivia_database_wrapper-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 62c610e04113897c3ddef1bef9a057a547aec1f896ea94bb028cefa079dc13d3
MD5 a42dae313fe50016d63b076200e06122
BLAKE2b-256 8b11df94755cce44e94c2801a984ae920327e9cc8e160bb138f710ff1a228027

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