nerdlandbot
nerdlandbot copied to clipboard
Getting latest youtube video task crashes after rate limit
Describe the bug During the scheduler task for getting the latest youtube video for a particular channel, it can occur that youtube rate limits us because we have executed too many api calls recently. This is not handled correctly at the moment.
To Reproduce Steps to reproduce the behavior:
- Setup the youtube api token
- Add a youtube channel to track to the bot
- Perform the api call manually until you're rate limited
- See this error in the console:
Unhandled exception in internal background task 'check_and_post_latest_videos'.
Traceback (most recent call last):
File "/venv/lib/python3.8/site-packages/discord/ext/tasks/__init__.py", line 101, in _loop
await self.coro(*args, **kwargs)
File "/venv/lib/python3.8/site-packages/nerdlandbot/scheduler/Scheduler.py", line 19, in check_and_post_latest_videos
latest_video = await get_latest_video(channel_id)
File "/venv/lib/python3.8/site-packages/nerdlandbot/scheduler/Scheduler.py", line 34, in get_latest_video
response = r.json()["items"][0]
KeyError: 'items'
Expected behavior
- The task should handle this error so it doesn't crash, but continues gracefully
- Perform less API calls so we don't get rate limited
Additional context
Here are the contents of r.json()
when the error occurs:
{
"error": {
"code": 403,
"errors": [
{
"domain": "youtube.quota",
"message": "The request cannot be completed because you have exceeded your <a href=\"/youtube/v3/getting-started#quota\">quota</a>.",
"reason": "quotaExceeded"
}
],
"message": "The request cannot be completed because you have exceeded your <a href=\"/youtube/v3/getting-started#quota\">quota</a>."
}
}