Skip to content

Commit

Permalink
Merge branch 'master' of github.com:yetty/django-embed-video
Browse files Browse the repository at this point in the history
  • Loading branch information
yetty committed Sep 30, 2013
2 parents cb8fba4 + c2699d5 commit c133f25
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
37 changes: 23 additions & 14 deletions embed_video/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
except:
import urllib.parse as urlparse # py3

from django.utils.functional import cached_property

from .utils import import_by_path
from .settings import EMBED_VIDEO_BACKENDS, EMBED_VIDEO_CACHE
Expand Down Expand Up @@ -109,7 +110,14 @@ def init(self, url):
self.backend = self.__class__.__name__
self.code = self.get_code()
self.url = self.get_url()
self.thumbnail = self.get_thumbnail_url()

@cached_property
def thumbnail(self):
return self.get_thumbnail_url()

@cached_property
def info(self):
return self.get_info()

@classmethod
def is_valid(klass, url):
Expand Down Expand Up @@ -208,7 +216,6 @@ class VimeoBackend(VideoBackend):
def init(self, url):
self._url = url
self.code = self.get_code()
self.info = self.get_info()

super(VimeoBackend, self).init(url)

Expand All @@ -233,29 +240,31 @@ class SoundCloudBackend(VideoBackend):
re_code = re.compile(r'src=".*%2F(?P<code>\d+)&show_artwork.*"', re.I)
re_url = re.compile(r'src="(?P<url>.*?)"', re.I)

def init(self, url):
@cached_property
def width(self):
return self.info.get('width')

@cached_property
def height(self):
return self.info.get('height')

def get_info(self):
params = {
'format': 'json',
'url': url,
'url': self._url,
}

r = requests.get(self.base_url, data=params)
self.response = json.loads(r.text)

self.width = self.response.get('width')
self.height = self.response.get('height')

super(SoundCloudBackend, self).init(url)
return json.loads(r.text)

def get_thumbnail_url(self):
return self.response.get('thumbnail_url')
return self.info.get('thumbnail_url')

def get_url(self):
match = self.re_url.search(self.response.get('html'))
match = self.re_url.search(self.info.get('html'))
return match.group('url')

def get_code(self):
match = self.re_code.search(self.response.get('html'))
match = self.re_code.search(self.info.get('html'))
return match.group('code')

def get_embed_code(self, width, height):
Expand Down
5 changes: 3 additions & 2 deletions embed_video/tests/tests_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ def test_code_soundcloud(self):
self.assertEqual(code, url[1])

def test_vimeo_get_info_exception(self):
self.assertRaises(VideoDoesntExistException, VimeoBackend,
'http://vimeo.com/123')
with self.assertRaises(VideoDoesntExistException):
backend = VimeoBackend('http://vimeo.com/123')
backend.get_info()

def test_youtube_keyerror(self):
""" Test for issue #7 """
Expand Down

0 comments on commit c133f25

Please sign in to comment.