Package: the-trivia-api-library
The Trivia API Python LibraryA Python library for interacting with the Trivia API. This library allows you to easily request multiple-choice trivia questions and access various features of the Trivia API.DemoAccess a demo of this libraryhere.InstallationYou can install this library using pip:pipinstallthe_trivia_api_libraryExample usageHere's an example of how to use this library to interact with the Trivia API:fromthe_trivia_api_libraryimportTriviaAPIClient,EnumCategory,EnumDifficulty# Initialize the Trivia API clientapi_key="YOUR_API_KEY"client=TriviaAPIClient(api_key=api_key)# Get a random set of questionsquestions=client.get_random_question(limit=5,categories=[EnumCategory.SCIENCE.value],difficulties=[EnumDifficulty.EASY.value],tags=["math","physics"])forquestioninquestions:print(f"Question:{question['question']['text']}")print(f"Correct Answer:{question['correctAnswer']}")print(f"Incorrect Answers:{', '.join(question['incorrectAnswers'])}")print()Methodsget_random_questionGet a random set of questions from the Trivia API.Parameterslimit- The number of questions to return. Defaults to 1.categories- A list of categories to filter the questions by. Defaults to None.difficulties- A list of difficulties to filter the questions by. Defaults to None.region- A list of regions to filter the questions by. Defaults to None.tags- A list of tags to filter the questions by. Defaults to None.types- A list of types to filter the questions by. Defaults to None.session- A session object to use for the request. Defaults to None.preview- A boolean indicating whether to return the questions in preview mode. Defaults to False.ReturnsA list of questions.Examplefromthe_trivia_api_libraryimportTriviaAPIClient,EnumCategory,EnumDifficulty# Initialize the Trivia API clientapi_key="YOUR_API_KEY"client=TriviaAPIClient(api_key=api_key)# Get a random set of questionsquestions=client.get_random_question(limit=5,categories=[EnumCategory.SCIENCE.value],difficulties=[EnumDifficulty.EASY.value],tags=["math","physics"])forquestioninquestions:print(f"Question:{question['question']['text']}")print(f"Correct Answer:{question['correctAnswer']}")print(f"Incorrect Answers:{', '.join(question['incorrectAnswers'])}")print()get_categoriesGet a list of categories from the Trivia API.ParametersNoneReturnsA list of categories.Examplefromthe_trivia_api_libraryimportTriviaAPIClientapi_key="YOUR_API_KEY"client=TriviaAPIClient(api_key=api_key)tags=client.get_all_tags()fortagintags:print(tag)get_totals_per_tagGet the total number of questions per tag from the Trivia API.Parameterscategories- A list of categories to filter the questions by. Defaults to None.difficulties- A list of difficulties to filter the questions by. Defaults to None.region- A list of regions to filter the questions by. Defaults to None.tags- A list of tags to filter the questions by. Defaults to None.types- A list of types to filter the questions by. Defaults to None.ReturnsA list of totals per tag.Examplefromthe_trivia_api_libraryimportTriviaAPIClientapi_key="YOUR_API_KEY"client=TriviaAPIClient(api_key=api_key)tags=client.get_totals_per_tag()fortagintags:print(f"tag:{tag}-> total:{tags[tag]}")Initialize the Trivia API clientContributingContributions are welcome! If you have suggestions, bug reports, or want to contribute to the library, please follow the guidelines in theCONTRIBUTING.mdfile.LicenseThis library is licensed under the MIT License - see theLICENSEfile for details.