mirror of
https://github.com/Hopiu/django-embed-video.git
synced 2026-04-30 17:34:45 +00:00
Fixes #21. Handle invalid SoundCloud urls
This commit is contained in:
parent
c22c82a86a
commit
b549271d1a
5 changed files with 39 additions and 20 deletions
|
|
@ -5,6 +5,8 @@ Release 0.9 (dev)
|
|||
|
||||
- Allow relative sizes in template tag (`#19 <https://github.com/yetty/django-embed-video/pull/19>`_)
|
||||
|
||||
- Fix handling invalid urls of SoundCloud. (`#21 <https://github.com/yetty/django-embed-video/issues/21>`_)
|
||||
|
||||
|
||||
Release 0.8 (Feb. 22, 2014)
|
||||
---------------------------
|
||||
|
|
|
|||
|
|
@ -268,6 +268,11 @@ class SoundCloudBackend(VideoBackend):
|
|||
r = requests.get(self.base_url, data=params,
|
||||
timeout=EMBED_VIDEO_TIMEOUT)
|
||||
|
||||
if r.status_code != 200:
|
||||
raise VideoDoesntExistException(
|
||||
'SoundCloud returned status code `{0}`.'.format(r.status_code)
|
||||
)
|
||||
|
||||
return json.loads(r.text)
|
||||
|
||||
def get_thumbnail_url(self):
|
||||
|
|
|
|||
|
|
@ -4,7 +4,8 @@ import re
|
|||
import logging
|
||||
import requests
|
||||
|
||||
from ..backends import detect_backend, VideoBackend
|
||||
from ..backends import detect_backend, VideoBackend, \
|
||||
VideoDoesntExistException, UnknownBackendException
|
||||
|
||||
register = Library()
|
||||
|
||||
|
|
@ -72,15 +73,19 @@ class VideoNode(Node):
|
|||
def render(self, context):
|
||||
url = self.url.resolve(context)
|
||||
|
||||
# Fixes #18. If no video url is provided it should return an empty
|
||||
# string instead raising UnknownBackendException.
|
||||
if not url:
|
||||
return ''
|
||||
try:
|
||||
if self.size:
|
||||
return self.__render_embed(url, context)
|
||||
else:
|
||||
return self.__render_block(url, context)
|
||||
except requests.Timeout:
|
||||
logger.exception('Timeout reached during rendering embed video (`{0}`)'.format(url))
|
||||
except UnknownBackendException:
|
||||
logger.exception('Backend wasn\'t recognised (`{0}`)'.format(url))
|
||||
except VideoDoesntExistException:
|
||||
logger.exception('Attempt to render not existing video (`{0}`)'.format(url))
|
||||
|
||||
if self.size:
|
||||
return self.__render_embed(url, context)
|
||||
else:
|
||||
return self.__render_block(url, context)
|
||||
return ''
|
||||
|
||||
def __render_embed(self, url, context):
|
||||
size = self.size.resolve(context) \
|
||||
|
|
@ -88,17 +93,12 @@ class VideoNode(Node):
|
|||
return self.embed(url, size, context=context)
|
||||
|
||||
def __render_block(self, url, context):
|
||||
output = ''
|
||||
as_var = self.bits[-1]
|
||||
|
||||
try:
|
||||
context.push()
|
||||
context[as_var] = self.get_backend(url, context=context)
|
||||
output = self.nodelist_file.render(context)
|
||||
context.pop()
|
||||
except requests.Timeout:
|
||||
logger.exception('Timeout reached during rendering embed '
|
||||
'video (`{0}`)'.format(url))
|
||||
context.push()
|
||||
context[as_var] = self.get_backend(url, context=context)
|
||||
output = self.nodelist_file.render(context)
|
||||
context.pop()
|
||||
|
||||
return output
|
||||
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ from mock import patch
|
|||
import requests
|
||||
|
||||
from ..backends import detect_backend, YoutubeBackend, VimeoBackend, \
|
||||
SoundCloudBackend, UnknownBackendException, \
|
||||
VideoDoesntExistException, UnknownIdException, VideoBackend
|
||||
SoundCloudBackend, UnknownBackendException, VideoDoesntExistException, \
|
||||
UnknownIdException, VideoBackend
|
||||
|
||||
|
||||
class BackendTestMixin(object):
|
||||
|
|
@ -147,3 +147,8 @@ class SoundCloudBackendTestCase(BackendTestMixin, TestCase):
|
|||
def test_timeout_in_get_info(self):
|
||||
backend = SoundCloudBackend('https://soundcloud.com/community/soundcloud-case-study-wildlife')
|
||||
self.assertRaises(requests.Timeout, backend.get_info)
|
||||
|
||||
def test_invalid_url(self):
|
||||
""" Check if bug #21 is fixed. """
|
||||
backend = SoundCloudBackend('https://soundcloud.com/xyz/foo')
|
||||
self.assertRaises(VideoDoesntExistException, backend.get_info)
|
||||
|
|
|
|||
|
|
@ -205,3 +205,10 @@ class EmbedVideoNodeTestCase(TestCase):
|
|||
rendered = '<iframe width="80%" height="300" src="http://player.vimeo.com/video/72304002"' \
|
||||
'\n frameborder="0" allowfullscreen></iframe>'
|
||||
self.assertEqual(template.render(self._grc()).strip(), rendered)
|
||||
|
||||
def test_soundcloud_invalid_url(self):
|
||||
template = Template("""
|
||||
{% load embed_video_tags %}
|
||||
{% video "https://soundcloud.com/xyz/foo" %}
|
||||
""")
|
||||
self.assertEqual(template.render(self._grc()).strip(), '')
|
||||
|
|
|
|||
Loading…
Reference in a new issue