def test_youtube_subs_response(self): import os from videos.types.youtube import YoutubeVideoType import urllib def urlopen_mockup(url, *args, **kwargs): path = os.path.join(os.path.dirname(__file__), 'fixtures/youtube_subs_response.json') return open(path) _urlopen = urllib.urlopen urllib.urlopen = urlopen_mockup vt = YoutubeVideoType('http://www.youtube.com/watch?v=GcjgWov7mTM') video, create = Video.get_or_create_for_url('http://www.youtube.com/watch?v=GcjgWov7mTM', vt) vt._get_subtitles_from_youtube(video) video = Video.objects.get(pk=video.pk) version = video.version(language_code='en') self.assertFalse(version is None) self.assertTrue(len(version.subtitles())) self.assertEqual(version.subtitles()[0].text, 'I think what is probably the most misunderstood\nconcept in all of science and as we all know') subs = version.subtitles() subs.sort(key=lambda s: s.start_time) for i in range(1, len(subs)): self.assertTrue(subs[i].sub_order > subs[i - 1].sub_order) urllib.urlopen = _urlopen
def test_youtube_subs_response(self): import os from videos.types.youtube import YoutubeVideoType import urllib def urlopen_mockup(url, *args, **kwargs): path = os.path.join(os.path.dirname(__file__), 'fixtures/youtube_subs_response.json') return open(path) _urlopen = urllib.urlopen urllib.urlopen = urlopen_mockup vt = YoutubeVideoType('http://www.youtube.com/watch?v=GcjgWov7mTM') video, create = Video.get_or_create_for_url('http://www.youtube.com/watch?v=GcjgWov7mTM', vt) res = vt._get_subtitles_from_youtube(video) if res is None: # api might be down or error return video = Video.objects.get(pk=video.pk) version = video.version(language_code='en') self.assertFalse(version is None) self.assertTrue(len(version.subtitles())) self.assertEqual(version.subtitles()[0].text, 'I think what is probably the most misunderstood\nconcept in all of science and as we all know') subs = version.subtitles() subs.sort(key=lambda s: s.start_time) for i in range(1, len(subs)): self.assertTrue(subs[i].sub_order > subs[i - 1].sub_order) urllib.urlopen = _urlopen
def test_add_youtube_video(self): vt = YoutubeVideoType('http://www.youtube.com/watch?v=_ShmidkrcY0') vt_2 = YoutubeVideoType('http://www.youtube.com/watch?v=_ShmidkrcY0we') vt_3 = YoutubeVideoType('http://www.youtube.com/watch?v=_ShmidkrcY0ewgwe') user = UserFactory() video, video_url = Video.add(vt, user) with assert_raises(Video.UrlAlreadyAdded): video_2, video_url_2 = Video.add(vt_2, user) with assert_raises(Video.UrlAlreadyAdded): video_3, video_url_3 = Video.add(vt_3, user)
def test_video_id(self, mock_get_video_info): video_info = google.VideoInfo('test-channel-id', 'title', 'description', 100, 'http://example.com/thumb.png') mock_get_video_info.return_value = video_info vt = YoutubeVideoType('http://www.youtube.com/watch?v=_ShmidkrcY0') self.assertEqual(vt.video_id, '_ShmidkrcY0') vt = YoutubeVideoType('http://www.youtube.com/watch?v=_ShmidkrcY0123') self.assertEqual(vt.video_id, '_ShmidkrcY0') vt = YoutubeVideoType('http://www.youtube.com/watch?v=_Shmid') self.assertEqual(vt.video_id, '_Shmid')
def test_set_values(self, mock_get_video_info): video_info = google.VideoInfo('test-channel-id', 'title', 'description', 100, 'http://example.com/thumb.png') mock_get_video_info.return_value = video_info vt = YoutubeVideoType('http://www.youtube.com/watch?v=_ShmidkrcY0') video = Video() vt.set_values(video) self.assertEqual(video.title, video_info.title) self.assertEqual(video.description, video_info.description) self.assertEqual(video.duration, video_info.duration) self.assertEqual(video.thumbnail, video_info.thumbnail_url)
def test_get_video_info_exception(self, mock_get_video_info): video_info = google.VideoInfo('test-channel-id', 'title', 'description', 100, 'http://example.com/thumb.png') mock_get_video_info.side_effect = google.APIError() vt = YoutubeVideoType('http://www.youtube.com/watch?v=_ShmidkrcY0') video = Video() vt.set_values(video, None, None, None) self.assertEqual(vt.video_id, '_ShmidkrcY0') self.assertEqual(video.description, '') self.assertEqual(video.duration, None) self.assertEqual(video.thumbnail, '')
def test_get_video_info_exception(self, mock_get_video_info): video_info = google.VideoInfo('test-channel-id', 'title', 'description', 100, 'http://example.com/thumb.png') mock_get_video_info.side_effect = google.APIError() vt = YoutubeVideoType('http://www.youtube.com/watch?v=_ShmidkrcY0') video = Video() vt.set_values(video) self.assertEqual(vt.video_id, '_ShmidkrcY0') self.assertEqual(video.description, '') self.assertEqual(video.duration, None) self.assertEqual(video.thumbnail, '') # since get_video_info failed, we don't know the channel id of our # video URL. We should use a dummy value to make it easier to fix the # issue in the future self.assertEqual(vt.owner_username(), None)
def _import_video(self, video_url, videoid, title, description, thumbnail, videosrt): videoid_match = VIDEOID_RE.search(videoid) videoid = videoid_match.group(1) video_type = YoutubeVideoType( 'http://www.youtube.com/watch?v={0}'.format(videoid)) try: video_url_obj = VideoUrl.objects.get( url=video_type.convert_to_video_url()) video = video_url_obj.video except ObjectDoesNotExist: video_url_obj = None video = Video() video.youtube_videoid = videoid video.title = title video.description = description if video_type.entry.media.duration: video.duration = int(video_type.entry.media.duration.seconds) video.thumbnail = thumbnail video.save() Action.create_video_handler(video) if videosrt: self._import_srt(video, videosrt) else: SubtitleLanguage(video=video, language='en', is_original=True, is_forked=True).save() if not video_url_obj: video_url_obj = VideoUrl(videoid=videoid, url=video_type.convert_to_video_url(), type=video_type.abbreviation, original=True, primary=True, video=video) video_url_obj.save() self._save_alternate_url(video, video_url)
def _import_video(self, video_url, videoid, title, description, thumbnail, videosrt): videoid_match = VIDEOID_RE.search(videoid) videoid = videoid_match.group(1) video_type = YoutubeVideoType( 'http://www.youtube.com/watch?v={0}'.format(videoid)) try: video_url_obj = VideoUrl.objects.get( url=video_type.convert_to_video_url()) video = video_url_obj.video except ObjectDoesNotExist: video_url_obj = None video = Video() video.youtube_videoid = videoid video.title = title video.description = description if video_type.entry.media.duration: video.duration = int(video_type.entry.media.duration.seconds) video.thumbnail = thumbnail video.save() Action.create_video_handler(video) if videosrt: self._import_srt(video, videosrt) else: SubtitleLanguage(video=video, language='en', is_original=True, is_forked=True).save() if not video_url_obj: video_url_obj = VideoUrl( videoid=videoid, url=video_type.convert_to_video_url(), type=video_type.abbreviation, original=True, primary=True, video=video) video_url_obj.save() self._save_alternate_url(video, video_url)