Asynchronous API Wrapper for the OpenTDB Api. (https://opentdb.com/)
Project description
aiotrivia
Async Wrapper for the OpenTDB API
Installation
$ pip install git+https://github.com/niztg/aiotrivia
Example Usage
import asyncio
import aiotrivia
client = aiotrivia.TriviaClient()
async def main():
data = await client.get_specific_question(category=20)
for i in data:
print('%s | %s' % (i.question, i.responses))
await client.close() # after you're done with everything
asyncio.get_event_loop().run_until_complete(main())
Returns:
Which figure from Greek mythology traveled to the underworld to return his wife Eurydice to the land of the living? | ['Daedalus', 'Hercules', 'Perseus', 'Orpheus']
discord.py command usage
from aiotrivia import TriviaClient, AiotriviaException
from discord.ext import commands
import asyncio
import random
class TriviaCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.trivia = TriviaClient()
@commands.command()
async def trivia(self, ctx, difficulty):
try:
question = await self.trivia.get_random_question(difficulty)
except AiotriviaException as error:
return await ctx.send(f"{error.__class__.__name__}: {error}")
answers = question.responses
random.shuffle(answers)
final_answers = '\n'.join([f"{index}. {value}" for index, value in enumerate(answers, 1)])
message = await ctx.send(f"**{question.question}**\n{final_answers}\n{question.type.capitalize()} Question about {question.category}")
answer = answers.index(question.answer)+1
await self.trivia.close() # cleaning up
try:
while True:
msg = await self.client.wait_for('message', timeout=15, check=lambda m: m.id != message.id)
if str(answer) in msg.content:
return await ctx.send(f"{answer} was correct! ({question.answer})")
except asyncio.TimeoutError:
await ctx.send(f"The correct answer was {question.answer}")
def setup(bot):
bot.add_cog(TriviaCog(bot))
For more info, read the documentation
Or join the discord server
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
aiotrivia-2.0.1.tar.gz
(4.6 kB
view details)
Built Distribution
File details
Details for the file aiotrivia-2.0.1.tar.gz
.
File metadata
- Download URL: aiotrivia-2.0.1.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3rc1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef057c2765b0c6167edbacd284e919fd8c5827c46f142ffc8eb177282cc22a49 |
|
MD5 | c55a324dff637bdef10afe77b3e7f141 |
|
BLAKE2b-256 | d27ff41a91f67bd700444dc3767fec27fa78f50f44fad2ddb4a165e6c789cb92 |
File details
Details for the file aiotrivia-2.0.1-py3-none-any.whl
.
File metadata
- Download URL: aiotrivia-2.0.1-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3rc1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1fce48405eed4eabdf5ce7076d9c7cdac9cf8fc527656ca14eb9c687658cc71d |
|
MD5 | d241422723cfae572cfda4fd5a27fca0 |
|
BLAKE2b-256 | 741c831284bf4e9048f95645ebaf21eb7108e2631e66a94c0d5ba68fbb58d91f |