So, I don't seem to find any good tutorial on using the new asyncio module in python (async, await, etc.). Also, from all the tutorials I've watched, the concept is poorly described and I don't seem to be able to wrap my head around the idea of coroutines. I mean, the idea behind the concept is not that hard, but there's not a single place where I can learn exactly what the coroutines can and cannot do, and how to use them.
For example, I have written a small class called YouTubeAPI for a Discord BOT that I'm building at the moment. The Discord.py library uses asyncio for all its functions, but my class doesn't. My class (YouTubeAPI) is made for the sole purpose of retrieving data from the YouTube Data API V3 about latest videos posted by a user. I'm actually trying to build a BOT that keeps me up-to-date on all the videos someone is posting.
But for the BOT to be working I need to make an update()
function that gets all the videos regularly so that I can get the latest video. The problem is that the update function needs to be wrapped in a while True
loop (or something similar), so that I can keep the list up-to-date. If I build an infinite loop then I'm gonna run into a problem with the BOT (making the BOT crash and unusable).
So, I thought maybe I could learn the new asyncio module and solve the problem that way. Sadly I find nothing.
Here's some code with all the API keys removed, so that you can see my problem easier:
from Api_Test import YoutubeAPI
import discord
import asyncio
YoutubeName = 'Vsauce'
GOOGLE_API = 'API KEY'
print('Collecting YouTube Data.')
api = YoutubeAPI(GOOGLE_API, YoutubeName) # create object that will get all info for the name 'Vsauce'
print('YouTube Data collected succesfully.')
print('Starting bot.')
def getLastVideo():
return api.videosData[0] # api.videosData looks like: [[title, link],[title, link],[title, link],]
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
await client.send_message('Now testing: Last {} videos!'.format(YoutubeName))
#While Loop that keeps the api.videosData up-to-date and runs "await client.send_message('new video: title + ink')" if new video found in the list
client.run('Discord BOT token')
I am extremely sorry if this post sounds vaguely explained, but I have totally no idea on how to use asyncio or something similar and I find myself in a place where I find almost no documentation on this new concept.