def retrieveThumbImage(video): """ Try to call YouTube service to retrieve video thumbnail and save it in the video """ video_behavior = IVideo(video) if not video_behavior: return video_id = video_behavior.get_youtube_id_from_url() if not video_id: return url = "https://i.ytimg.com/vi/%s/hqdefault.jpg" % video_id error_msg = _( 'yt_image_download_error_label', 'Unable to download thumbnail image automatically from youtube. Try later.' ) try: res = requests.get(url, stream=True, timeout=10) except Timeout: logger.error( 'Unable to retrieve thumbnail image for "%s": timeout.' % video_id) api.portal.show_message( message=error_msg, request=video.REQUEST, type="warning") return except Exception as e: logger.error('Unable to retrieve thumbnail from "%s".' % url) logger.exception(e) api.portal.show_message( message=error_msg, request=video.REQUEST, type="warning") return if not res.ok: if res.status_code == 404: logger.error( 'Unable to retrieve thumbnail from "%s". Not found.' % url) error_msg = _( 'yt_image_download_notfound_error_label', "Unable to download thumbnail image automatically from youtube. Probably it isn't available yet. Please retry in a few minutes." ) api.portal.show_message( message=error_msg, request=video.REQUEST, type="warning") else: logger.error('Unable to retrieve thumbnail from "%s". Error: %s' % (url, res.status_code)) api.portal.show_message( message=error_msg, request=video.REQUEST, type="warning") return video.image = NamedBlobImage(res.raw.data, filename=u'%s.jpg' % video_id) api.portal.show_message( message=_( 'yt_image_download_success_label', 'Thumbnail image correctly saved from youtube.'), request=video.REQUEST)
def test_extract_yt_video_id(self): behavior1 = IVideo(self.video1) behavior2 = IVideo(self.video2) behavior3 = IVideo(self.video3) self.assertEqual(behavior1.get_youtube_id_from_url(), self.video_id) self.assertEqual(behavior2.get_youtube_id_from_url(), self.video_id) self.assertEqual(behavior3.get_youtube_id_from_url(), self.video_id)
def retrieveThumbImage(video): """ Try to call YouTube service to retrieve video thumbnail and save it in the video """ video_behavior = IVideo(video) if not video_behavior: return video_id = video_behavior.get_youtube_id_from_url() if not video_id: return url = "https://i.ytimg.com/vi/%s/hqdefault.jpg" % video_id error_msg = _( 'yt_image_download_error_label', 'Unable to download thumbnail image automatically from youtube. Try later.' ) try: res = requests.get(url, stream=True, timeout=10) except Timeout: logger.error('Unable to retrieve thumbnail image for "%s": timeout.' % video_id) api.portal.show_message(message=error_msg, request=video.REQUEST, type="warning") return except Exception as e: logger.error('Unable to retrieve thumbnail from "%s".' % url) logger.exception(e) api.portal.show_message(message=error_msg, request=video.REQUEST, type="warning") return if not res.ok: if res.status_code == 404: logger.error('Unable to retrieve thumbnail from "%s". Not found.' % url) error_msg = _( 'yt_image_download_notfound_error_label', "Unable to download thumbnail image automatically from youtube. Probably it isn't available yet. Please retry in a few minutes." ) api.portal.show_message(message=error_msg, request=video.REQUEST, type="warning") else: logger.error('Unable to retrieve thumbnail from "%s". Error: %s' % (url, res.status_code)) api.portal.show_message(message=error_msg, request=video.REQUEST, type="warning") return video.image = NamedBlobImage(res.raw.data, filename=u'%s.jpg' % video_id) api.portal.show_message(message=_( 'yt_image_download_success_label', 'Thumbnail image correctly saved from youtube.'), request=video.REQUEST)
def get_embed_url(self): """ Try to guess video id from a various case of possible youtube urls and returns the correct url for embed. For example: - 'https://youtu.be/VIDEO_ID' - 'https://www.youtube.com/watch?v=VIDEO_ID' - 'https://www.youtube.com/embed/2Lb2BiUC898' """ video_behavior = IVideo(self.context) if not video_behavior: return "" video_id = video_behavior.get_youtube_id_from_url() if not video_id: return "" return "https://www.youtube.com/embed/" + video_id