Skip to main content

An aiohttp wrapper for OpenTDB

Project description

triviapy

triviapy is an async API wrapper for open trivia database this is still a WIP but currently can return questions from specific or all categories and select the number you want to return.

Setup

  1. Make sure you have python installed
  2. Download the repository
  3. Make sure you have aiohttp installed
  4. Place the triviapy folder in your project folder and have fun.

Quick Start

  1. A:First import Game from triviapy and construct a Game object:
from triviapy import Game


game = Game()

(optional) Set a game token, this will prevent repeat questions from occuring:

await game.gen_token()
  1. To see the categories avaliable you can do game.categories(), this returns the below array of category names and ID's, to use a category supply the number for the category you'd like questions from:
[('General Knowledge', 9), ('Entertainment: Books', 10), ('Entertainment: Film', 11), ('Entertainment: Music', 12), ('Entertainment: Musicals & Theatres', 13), ('Entertainment: Television', 14), ('Entertainment: Video Games', 15), ('Entertainment: Board Games', 16), ('Science & Nature', 17), ('Science: Computers', 18), ('Science: Mathematics', 19), ('Mythology', 20), ('Sports', 21), ('Geography', 22), ('History', 23), ('Politics', 24), ('Art', 25), ('Celebrities', 26), ('Animals', 27), ('Vehicles', 28), ('Entertainment: Comics', 29), ('Science: Gadgets', 30), ('Entertainment: Japanese Anime & Manga', 31), ('Entertainment: Cartoon & Animations', 32)]
  1. Create a round with question info, you can supply qty and category as ints to specify the number of questions and what category you want. It defaults to one question of any category and returns a Question object if only one if given or an array of them if multiple questions are requested.
    # This will give 10 questions from the music category.
round_example = game.round(quantity=10, category=12)

# This will give one question from a random category
round_2 = game.round()
  1. Question objects hold all the info needed about a question including: question, category, answers, correct answer, and difficulty:
    question = round_example[0]

    question_str = question.question # Returns the question as a str
    answers = question.answers # Returns a preshuffled array with the 4 multiple choice answer posibilities
    answer = question.answer # Returns the str of the answer, this is included in the answers array as well in the same str format.
    category = question.category # Returns the category of the question as a str
    difficulty = question.difficulty # Returns the question difficulty (Easy, Medium or Hard)
  1. Error Handling

Importing errors:

    from triviapy.errors import TokenError, QuestionError, InvalidTokenError, CategoryError

TokenError

This error will occur is game.token() is unable to set a token, this error may be caused by opentdb outages or other errors.

QuestionError

This error will occur if the number of questions exceeds that avaliable, when this occurs do await game.reset() to reuse questions.

InvalidTokenError

This error is raised due to an invalid or expired token, an api token will expire after 6 hours of no use. Do ``await game.gen_token``` to reset this.

CategoryError:

This error will occur if an invalid int is defined for a round category. To fix this double check what you are putting in.

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiotriviapy-1.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

aiotriviapy-1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file aiotriviapy-1.0.tar.gz.

File metadata

  • Download URL: aiotriviapy-1.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for aiotriviapy-1.0.tar.gz
Algorithm Hash digest
SHA256 3c56aa3db115311f1d7d2823e1427a3fdf9ec9956d4b62717b2aab2212251877
MD5 ddb9d4dd19b529d3cb10d5c9deb98567
BLAKE2b-256 70fe2feefb9a79da54df413ebcc7f1f1dd5fac6fa843b69bed715d4fded8fbfd

See more details on using hashes here.

File details

Details for the file aiotriviapy-1.0-py3-none-any.whl.

File metadata

  • Download URL: aiotriviapy-1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for aiotriviapy-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 716e0fbcd2e134a85af2af3727c1926bdc8d7e44f2efd39ba5ea6f7d6d10b4c4
MD5 18c7f7a999f608f9a89b1f20ef84f49c
BLAKE2b-256 e3676c0e909e04f7fa3d39b1738fb444a7c546e85bc132cda111e0494e2c5e52

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page