nerdlandbot icon indicating copy to clipboard operation
nerdlandbot copied to clipboard

Getting latest youtube video task crashes after rate limit

Open Autom3 opened this issue 4 years ago • 0 comments

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:

  1. Setup the youtube api token
  2. Add a youtube channel to track to the bot
  3. Perform the api call manually until you're rate limited
  4. 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

  1. The task should handle this error so it doesn't crash, but continues gracefully
  2. 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>."
  }
}

Autom3 avatar Dec 15 '20 20:12 Autom3