pyopentdb
Python interface for the Open Trivia DB API
👶 Dependencies
🛠️ Installation
Install from PyPI using pip, you may need to use pip3 depending on your installation.
pip install pyopentdb
🚀 Quick Start
To get started create an instance of OpenTDBClient. Helper enums are provided to make querying easier. Using these enums is not required, you may also pass the standard identifiers as integers/strings. Please see the API documentation for more information.
A session token is automatically generated when the object is initialized. The client will also attempt to automatically recycle the token when it expires or runs out of new questions.
Calling .get_questions() will return a QuestionSet object of Question objects. QuestionSet is a thin wrapper around a list that provides some additional convenience methods.
from pyopentdb import OpenTDBClient, Category, QuestionType, Difficulty
client = OpenTDBClient()
questions = client.get_questions(
amount=5,
category=Category.SCIENCE_NATURE,
question_type=QuestionType.MULTIPLE,
difficulty=Difficulty.MEDIUM
)
>>> questions
[
Question(category=<Category.SCIENCE_NATURE: CategoryItem(id=17, name='Science & Nature')>, question_type=<QuestionType.MULTIPLE: 'multiple'>, difficulty=<Difficulty.MEDIUM: 'medium'>, question='All the following metal elements are liquids at or near room temperature EXCEPT:', choices=['Mercury', 'Caesium', 'Gallium', 'Beryllium'], answer='Beryllium', answer_index=3),
Question(category=<Category.SCIENCE_NATURE: CategoryItem(id=17, name='Science & Nature')>, question_type=<QuestionType.MULTIPLE: 'multiple'>, difficulty=<Difficulty.MEDIUM: 'medium'>, question='The medical condition osteoporosis affects which part of the body?', choices=['Skin', 'Heart', 'Brain', 'Bones'], answer='Bones', answer_index=3),
Question(category=<Category.SCIENCE_NATURE: CategoryItem(id=17, name='Science & Nature')>, question_type=<QuestionType.MULTIPLE: 'multiple'>, difficulty=<Difficulty.MEDIUM: 'medium'>, question='About how old is Earth?', choices=['4.5 Billion Years', '3.5 Billion Years', '5.5 Billion Years', '2.5 Billion Years'], answer='4.5 Billion Years', answer_index=0),
Question(category=<Category.SCIENCE_NATURE: CategoryItem(id=17, name='Science & Nature')>, question_type=<QuestionType.MULTIPLE: 'multiple'>, difficulty=<Difficulty.MEDIUM: 'medium'>, question='What do you study if you are studying entomology?', choices=['the Brain', 'Fish', 'Humans', 'Insects'], answer='Insects', answer_index=3),
Question(category=<Category.SCIENCE_NATURE: CategoryItem(id=17, name='Science & Nature')>, question_type=<QuestionType.MULTIPLE: 'multiple'>, difficulty=<Difficulty.MEDIUM: 'medium'>, question='Which of the following men does not have a chemical element named after him?', choices=['Albert Einstein', 'Enrico Fermi', 'Sir Isaac Newton', 'Niels Bohr'], answer='Sir Isaac Newton', answer_index=2)
]
Question choices are shuffled on each call. The answer_index field is provided to easily check the correct answer in the choice list.
Both Question and QuestionSet provide the .to_serializable method, which returns either an easily serializable output or a JSON formatted string.
⚙️ API Reference
OpenTDBClient
Client that handles HTTP requests and session tokens
Methods
get_questions
Get a set of questions based on query params
-
Parameters:
- amount: int = 10 : Number of items to return between 1 and 50
- category: (Category, int) = None : Category enum or ID, None for all
- difficulty: (Difficulty, str) = None : Difficulty enum or name, None for all
- question_type: (QuestionType, str) = None : Question type enum or name, None for all
- retry: int = 5 : Number of times to retry call after 4xx or 5xx status code or nonzero response code
-
Returns:
- QuestionSet
Question
Represents a single question returned by the API
Parameters
- category: Category : Category enum
- question_type: QuestionType : Question type enum
- difficulty: Difficulty : Difficulty enum
- question: str : Question string
- choices: List[str] : List of shuffled provided choices
- answer: str : Answer string
- answer_index: int : Index of answer in choices list
Methods
to_serializable
Generate an easily serializable object or JSON formatted string
-
Parameters:
- as_json: bool = False : Return a JSON formatted string
-
Returns:
- (dict, str)
QuestionSet
Represents an array of questions returned by the API, behaves like a list
Parameters
- items: List[Question] : Array of Question objects
Methods
to_serializable
Generate an easily serializable object or JSON formatted string
-
Parameters:
- as_json: bool = False : Return a JSON formatted string
-
Returns:
- (List[dict], str)
⚖️ License
Metadata
Release files for pyopentdb 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pyopentdb-0.0.4.tar.gz | 9.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyopentdb-0.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.3 kB
Release files / pyopentdb-0.0.4.tar.gz
| Download URL | pyopentdb-0.0.4.tar.gz |
|---|---|
| Size | 9.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0fde38d4aa025090b2326c9235746c904d92798235fec6150134a7080ccd1605
|
|
BLAKE2b-256 checksum How to use checksums |
6ed4e23100197c0653bc4b3d130d1e880e29eba00879745bb9f445000f965354
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.6
|
Release files / pyopentdb-0.0.4-py3-none-any.whl
| Download URL | pyopentdb-0.0.4-py3-none-any.whl |
|---|---|
| Size | 12.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a2033040695c4abaecf395173e57710fa87158e0d29ecad4a94321ae2ca421a9
|
|
BLAKE2b-256 checksum How to use checksums |
47838aa407289d7a6d0f82c2d0e8d9afa5190f8b16d8a577520b389dad190fe6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.6
|
Security for pyopentdb 0.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.