Skip to content

Commit

Permalink
Fixes #21. Handle invalid SoundCloud urls
Browse files Browse the repository at this point in the history
  • Loading branch information
yetty committed Mar 24, 2014
1 parent c22c82a commit 16c765e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
5 changes: 5 additions & 0 deletions embed_video/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ def get_info(self):
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):
Expand Down
36 changes: 18 additions & 18 deletions embed_video/templatetags/embed_video_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import logging
import requests

from ..backends import detect_backend, VideoBackend
from ..backends import detect_backend, VideoBackend, \
VideoDoesntExistException, UnknownBackendException

register = Library()

Expand Down Expand Up @@ -72,33 +73,32 @@ def __init__(self, parser, token):
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) \
if hasattr(self.size, 'resolve') else self.size
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

Expand Down
9 changes: 7 additions & 2 deletions embed_video/tests/tests_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import requests

from ..backends import detect_backend, YoutubeBackend, VimeoBackend, \
SoundCloudBackend, UnknownBackendException, \
VideoDoesntExistException, UnknownIdException, VideoBackend
SoundCloudBackend, UnknownBackendException, VideoDoesntExistException, \
UnknownIdException, VideoBackend


class BackendTestMixin(object):
Expand Down Expand Up @@ -147,3 +147,8 @@ def test_get_embed_code(self):
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)
7 changes: 7 additions & 0 deletions embed_video/tests/tests_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,10 @@ def test_allow_spaces_in_size(self):
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(), '')

0 comments on commit 16c765e

Please sign in to comment.