Skip to content

Commit

Permalink
Fix for handling weird responses in the category data from youtube.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhedegaard committed Mar 17, 2020
1 parent 7cbfc2b commit e435c93
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
19 changes: 19 additions & 0 deletions youtube/tests/test_youtubeapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ def test(self, requests_patch):
self.assertTrue(resp_mock.raise_for_status.called)
self.assertTrue(resp_mock.json.called)

@mock.patch('youtube.youtubeapi.requests')
def test__empty_data(self, requests_patch):
resp_mock = mock.Mock()
resp_mock.json.return_value = {}
requests_patch.get.return_value = resp_mock

# Empty list, since the JSON response is an empty object.
self.assertEqual(fetch_videocategories([1, 2, 3]), [])

requests_patch.get.assert_called_with(
'https://www.googleapis.com/youtube/v3/videoCategories',
params={
'part': 'snippet',
'id': '1,2,3',
'key': settings.YOUTUBE_API_KEY,
})
self.assertTrue(resp_mock.raise_for_status.called)
self.assertTrue(resp_mock.json.called)


class FetchChannelInfoTestCase(TestCase):

Expand Down
7 changes: 6 additions & 1 deletion youtube/youtubeapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ def fetch_videocategories(categoryids):
'key': settings.YOUTUBE_API_KEY,
})
resp.raise_for_status()
return resp.json()['items']
data = resp.json()
if 'items' in data:
return data['items']
else:
# Happens for specific ids, at specific times.
return []


def fetch_channel_info(channelid, parts=('snippet', 'contentDetails')):
Expand Down

0 comments on commit e435c93

Please sign in to comment.