def test_get_video_invalid_body(self): mocker = test_utils.RequestsMocker() mocker.expect_request( 'get', 'https://www.googleapis.com/youtube/v3/videos', params={ 'part': 'snippet,contentDetails', 'id': 'test-video-id', 'key': settings.YOUTUBE_API_KEY, }, body="Invalid body") google.get_video_info.run_original_for_test() with mocker: with assert_raises(google.APIError): google.get_video_info('test-video-id')
def test_get_video_info(self): mocker = test_utils.RequestsMocker() mocker.expect_request('get', 'https://www.googleapis.com/youtube/v3/videos', params={ 'part': 'snippet,contentDetails', 'id': 'test-video-id', 'key': settings.YOUTUBE_API_KEY, }, body=json.dumps({ 'items': [{ 'snippet': { 'title': 'test-title', 'channelId': 'test-channel-id', 'description': 'test-description', 'thumbnails': { 'high': { 'url': 'test-thumbnail-url', } } }, 'contentDetails': { 'duration': 'PT10M10S', } }] })) google.get_video_info.run_original_for_test() with mocker: video_info = google.get_video_info('test-video-id') self.assertEqual(video_info.channel_id, 'test-channel-id') self.assertEqual(video_info.title, 'test-title') self.assertEqual(video_info.description, 'test-description') self.assertEqual(video_info.duration, 610) self.assertEqual(video_info.thumbnail_url, 'test-thumbnail-url')
def test_get_video_info_no_items(self): mocker = test_utils.RequestsMocker() mocker.expect_request( 'get', 'https://www.googleapis.com/youtube/v3/videos', params={ 'part': 'snippet,contentDetails', 'id': 'test-video-id', 'key': settings.GOOGLE_API_KEY, }, body=json.dumps({ 'items': [ ] }) ) google.get_video_info.run_original_for_test() with mocker: with assert_raises(google.APIError): google.get_video_info('test-video-id')
def get_video_info(self, video, user, team, video_url): incomplete = False if not hasattr(self, '_video_info'): if team: accounts = externalsites.models.YouTubeAccount.objects.for_team_or_synced_with_team( team) elif user: accounts = externalsites.models.YouTubeAccount.objects.for_owner( user) else: accounts = externalsites.models.YouTubeAccount.objects.none() try: self._video_info = google.get_video_info( self.video_id, accounts[:YoutubeVideoType.MAX_ACCOUNTS_TO_TRY]) except google.APIError: if len(accounts) > YoutubeVideoType.MAX_ACCOUNTS_TO_TRY: accounts_pks = map( lambda x: x.pk, accounts[YoutubeVideoType.MAX_ACCOUNTS_TO_TRY:]) get_set_values_background.enqueue_in( 2, self.video_id, accounts_pks, video.pk, video_url.pk) incomplete = True self._video_info = None else: raise return self._video_info, incomplete
def find_youtube_account(video_id, possible_accounts): try: video_info = google.get_video_info(video_id, possible_accounts) for account in possible_accounts: if account.channel_id == video_info.channel_id: return account return None except google.APIError as e: logger.warn("find_youtube_account() error for video id {}: {}".format(video_id, e)) return None
def get_set_values_background(video_id, accounts_pks, video_pk, video_url_pk): from django.db import models from videos.models import Video, VideoUrl from externalsites.models import YouTubeAccount try: video = Video.objects.get(id=video_pk) video_url = VideoUrl.objects.get(id=video_url_pk) accounts = list(YouTubeAccount.objects.filter(id__in=accounts_pks)) video_info = google.get_video_info(video_id, accounts) YoutubeVideoType.complete_set_values(video, video_url, video_info) except models.ObjectDoesNotExist, e: return
def add_credit_to_video_url(video_url, account): """Add credit to a video on an external site This method checks if we have an account linked to the site and if so, we add credit to the description. It will only add credit once per video URL. """ creditedvideourl, created = models.CreditedVideoUrl.objects.get_or_create( video_url=video_url) if not created: return access_token = google.get_new_access_token(account.oauth_refresh_token) video_id = video_url.videoid credit_text = calc_credit_text(video_url.video) current_description = google.get_video_info(video_id).description if credit_text not in current_description: new_description = '%s\n\n%s' % (current_description, credit_text) google.update_video_description(video_id, access_token, new_description)
def get_video_info(self): if not hasattr(self, '_video_info'): self._video_info = google.get_video_info(self.video_id) return self._video_info
def find_youtube_account(video_id, possible_accounts): video_info = google.get_video_info(video_id, possible_accounts) for account in possible_accounts: if account.channel_id == video_info.channel_id: return account return None