def import_videos(self): if not self.should_import_videos(): return video_ids = google.get_uploaded_video_ids(self.channel_id) if not video_ids: return for video_id in video_ids: if video_id == self.last_import_video_id: break video_url = 'http://youtube.com/watch?v={}'.format(video_id) if self.type == ExternalAccount.TYPE_USER: try: Video.add(video_url, self.user) except Video.UrlAlreadyAdded: continue elif self.import_team: def add_to_team(video, video_url): TeamVideo.objects.create(video=video, team=self.import_team) try: Video.add(video_url, None, add_to_team) except Video.UrlAlreadyAdded: continue self.last_import_video_id = video_ids[0] self.save()
def test_get_uploaded_video_ids(self): mocker = test_utils.RequestsMocker() mocker.expect_request('get', 'https://www.googleapis.com/youtube/v3/channels', params={ 'part': 'contentDetails', 'id': 'test-channel-id', 'key': 'test-youtube-api-key', }, body=json.dumps({ 'items': [ { 'contentDetails': { 'relatedPlaylists': { 'uploads': 'test-playlist-id', }, }, }, ] })) mocker.expect_request( 'get', 'https://www.googleapis.com/youtube/v3/playlistItems', params={ 'part': 'snippet', 'playlistId': 'test-playlist-id', 'key': 'test-youtube-api-key', }, body=json.dumps({ 'items': [ { 'snippet': { 'resourceId': { 'kind': u'youtube#video', 'videoId': 'test-video-id1', } } }, { 'snippet': { 'resourceId': { 'kind': u'youtube#video', 'videoId': 'test-video-id2', } } }, { 'snippet': { 'resourceId': { 'kind': u'youtube#something-else', } } }, ] })) google.get_uploaded_video_ids.run_original_for_test() with mocker: video_ids = google.get_uploaded_video_ids('test-channel-id') assert_equal(video_ids, ['test-video-id1', 'test-video-id2'])
def test_get_uploaded_video_ids_multiple_pages(self): mocker = test_utils.RequestsMocker() mocker.expect_request('get', 'https://www.googleapis.com/youtube/v3/channels', params={ 'part': 'contentDetails', 'id': 'test-channel-id', 'key': 'test-youtube-api-key', }, body=json.dumps({ 'items': [ { 'contentDetails': { 'relatedPlaylists': { 'uploads': 'test-playlist-id', }, }, }, ] })) mocker.expect_request( 'get', 'https://www.googleapis.com/youtube/v3/playlistItems', params={ 'part': 'snippet', 'maxResults': 50, 'playlistId': 'test-playlist-id', 'key': 'test-youtube-api-key', }, body=json.dumps({ 'items': [ self.make_video_snippet('test-video-id{}'.format(i)) for i in range(50) ], 'nextPageToken': 'test-page-token', })) mocker.expect_request( 'get', 'https://www.googleapis.com/youtube/v3/playlistItems', params={ 'part': 'snippet', 'maxResults': 50, 'playlistId': 'test-playlist-id', 'key': 'test-youtube-api-key', 'pageToken': 'test-page-token', }, body=json.dumps({ 'items': [ self.make_video_snippet('test-video-id{}'.format(i)) for i in range(50, 75) ], })) google.get_uploaded_video_ids.run_original_for_test() with mocker: video_ids = google.get_uploaded_video_ids('test-channel-id') assert_equal(video_ids, ['test-video-id{}'.format(i) for i in range(75)])
def import_videos(self): if not self.should_import_videos(): return video_ids = google.get_uploaded_video_ids(self.channel_id) if not video_ids: return for video_id in video_ids: if video_id == self.last_import_video_id: break video_url = 'http://youtube.com/watch?v={}'.format(video_id) if self.type == ExternalAccount.TYPE_USER: Video.get_or_create_for_url(video_url, user=self.user) elif self.import_team: video, created = Video.get_or_create_for_url(video_url) TeamVideo.objects.create(video=video, team=self.import_team) self.last_import_video_id = video_ids[0] self.save()