Package: pyopentdb
pyopentdbPython interface for theOpen Trivia DB API👶 DependenciesPython 3.6 or higher🛠️ InstallationInstall from PyPI usingpip, you may need to usepip3depending on your installation.pipinstallpyopentdb🚀 Quick StartTo get started create an instance ofOpenTDBClient. 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 theAPI documentationfor 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 aQuestionSetobject ofQuestionobjects.QuestionSetis a thin wrapper around a list that provides some additional convenience methods.frompyopentdbimportOpenTDBClient,Category,QuestionType,Difficultyclient=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. Theanswer_indexfield is provided to easily check the correct answer in the choice list.BothQuestionandQuestionSetprovide the.to_serializablemethod, which returns either an easily serializable output or a JSON formatted string.⚙️ API ReferenceOpenTDBClientClient that handles HTTP requests and session tokensMethodsget_questionsGet a set of questions based on query paramsParameters:amount: int = 10 : Number of items to return between 1 and 50category: (Category, int) = None : Category enum or ID, None for alldifficulty: (Difficulty, str) = None : Difficulty enum or name, None for allquestion_type: (QuestionType, str) = None : Question type enum or name, None for allretry: int = 5 : Number of times to retry call after 4xx or 5xx status code or nonzero response codeReturns:QuestionSetQuestionRepresents a single question returned by the APIParameterscategory: Category : Category enumquestion_type: QuestionType : Question type enumdifficulty: Difficulty : Difficulty enumquestion: str : Question stringchoices: List[str] : List of shuffled provided choicesanswer: str : Answer stringanswer_index: int : Index of answer in choices listMethodsto_serializableGenerate an easily serializable object or JSON formatted stringParameters:as_json: bool = False : Return a JSON formatted stringReturns:(dict, str)QuestionSetRepresents an array of questions returned by the API, behaves like a listParametersitems: List[Question] : Array of Question objectsMethodsto_serializableGenerate an easily serializable object or JSON formatted stringParameters:as_json: bool = False : Return a JSON formatted stringReturns:(List[dict], str)⚖️ LicenseMIT © 2020 Andrew Mickael