Package: opentdb-py
opentdb-pyPython wrapper for the open-trivia-database APIInstallationpy -m pip install -U opentdb-py  
  
# latest (unstable)  
py -m pip install -U git+https://github.com/Marseel-E/opentdb-pyQuickstartimportasynciofromtriviaimportClient,EmptyTokenasyncdefmain()->None:session_token=awaitClient.get_session_token()trivia_client=Client(session_token)try:data=awaittrivia_client.get_questions(amount=1)exceptEmptyToken:awaittrivia_client.reset_session_token()else:data=awaittrivia_client.get_questions(amount=1)print(data)awaittrivia_client.close_session()if__name__=='__main__:asyncio.run(main()){"category":"Entertainment: Video Games","type":"boolean","difficulty":"medium","question":"In the Resident Evil series, Leon S. Kennedy is a member of STARS.","correct_answer":"False","incorrect_answers":["True"]}DocumentationclassClientSends a POST call to the API and gets the desired data.Parameterssession_token (str) - The session token.Methodsasyncget_session_tokenasyncreset_session_tokenasyncclose_sessionasyncget_questionsasyncget_categoriesasyncget_category_questions_countasyncget_global_questions_countasyncClient.get_session_tokenawaitClient.get_session_token()This function is acoroutine.Fetches a session token from the API.ReturnsSession Token.Return TypestrasyncClient.reset_session_tokenawaitClient(...).reset_session_token()This function is acoroutine.Resets the session token.asyncClient.close_sessionawaitClient(...).close_session()This function is acoroutine.Closes the client session.asyncClient.get_questionsawaitClient(...).questions(amount=10,category=Category.undefined,difficulty=Difficulty.undefined,question_type=QuestionType.both,encoding=ResponseEncoding.default)This function is acoroutine.Fetches the requests amount of questions from the API with the appropriate parameters.Parametersamount (int) - The amount of questions to return.category (Category) - The category of questions.difficulty (Difficulty) - The difficulty of the question (undefined=any, easy, medium, hard).question_type (QuestionType) - The type of question (both, multiple choice, true/false).encoding (ResponseEncoding) - The encoding of the API response.ReturnsA list of questions.Return TypeQuestionDataasyncClient.get_categoriesawaitClient(...).categories()This function is acoroutine.Fetches a list of all categories the API has.ReturnsA list of categories.Return TypeCategoriesListasyncClient.get_category_questions_countawaitClient(...).category_questions_count(category=Category.general_knowledge)This function is acoroutine.Fetches statistics about a specific category.Parameterscategory (Category) - The category to fetch data from.ReturnsStatistics about the category.Return TypeCategoryQuestionsCountasyncClient.get_global_questions_countawaitClient(...).global_questions_count()This function is acoroutine.Fetches statistics about all the categories.ReturnsGlobal statisticsReturn TypeGlobalQuestionsCountexceptionNoResults<NoResults>: [Code 1] Could not return results. The API doesn't have enough questions for your query. (Ex. Asking for 50 Questions in a Category that only has 20.)This exception is raised when aresponse_code1 is returned.The API doesn't have enough questions for the given query.exceptionInvalidParameter<InvalidParameter>: [Code 2] Contains an invalid parameter. Arguements passed in aren't valid. (Ex. Amount = Five)This exception is raised when aresponse_code2 is returned.One or more of the query parameters are invalid.exceptionTokenNotFound<TokenNotFound>: [Code 3] Session Token does not exist.This exception is raised when aresponse_code3 is returned.The session token was not specified.exceptionTokenEmpty<TokenEmpty>: [Code 4] Session Token has returned all possible questions for the specified query. Resseting the Token is necassery.This exception is raised when aresponse_code4 is returned.The session token is about to expire.(session tokens last 6 hours only)typeQuestionDataclassQuestionData(TypedDict):category:strtype:strdifficulty:strquestion:strcorrect_answer:strincorrect_answers:List[str]typeQuestionResponseclassQuestionResponse(TypedDict):response_code:intresults:List[QuestionData]typeCategoryDataclassCategoryData(TypedDict):id:intname:strtypeCategoriesListclassCategoriesList(TypedDict):trivia_categories:List[CategoryData]typeCategoryQuestionsCountclassCategoryQuestionsCount(TypedDict):category_id:intcategory_questions_count:List[_CategoryQuestionsCount]typeGlobalQuestionsCountclassGlobalQuestionsCount(TypedDict):overall:_GlobalQuestionsCountcategories:Dict[str,_GlobalQuestionsCount]enumResponseEncodingclassResponseEncoding(Enum):default:None=Noneurl:str="url3986"base64:str="base64"enumDifficultyclassQuestionDifficulty(Enum):undefined:None=Noneeasy:str="easy"medium:str="medium"hard:str="hard"enumQuestionTypeclassQuestionType(Enum):both:None=Nonemultiple_choice:str="multiple"true_false:str="boolean"enumCategoryclassQuestionCategory(Enum):undefined:None=Nonegeneral_knowledge:int=9entertainment_books:int=10entertainment_film:int=11entertainment_music:int=12entertainment_music_and_theatres:int=13entertainment_television:int=14entertainment_video_games:int=15entertainment_board_games:int=16science_and_nature:int=17science_computers:int=18science_mathematics:int=19mythology:int=20sports:int=21geography:int=22history:int=23politics:int=24art:int=25celebrities:int=26animals:int=27vehicles:int=28entertainment_comics:int=29science_gadgets:int=30entertainment_japanese_anime_and_manga:int=31entertainment_cartoons_and_animations:int=32:scroll:License