mirror of
https://github.com/Hopiu/django-embed-video.git
synced 2026-03-16 21:30:23 +00:00
Prevent API calls on when backend intance is created
This commit is contained in:
parent
a2538a034e
commit
a4684c62db
2 changed files with 26 additions and 16 deletions
|
|
@ -7,6 +7,7 @@ try:
|
|||
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
|
||||
|
|
@ -109,7 +110,14 @@ class VideoBackend(object):
|
|||
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):
|
||||
|
|
@ -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)
|
||||
|
||||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -90,8 +90,9 @@ class BackendsTestCase(TestCase):
|
|||
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 """
|
||||
|
|
|
|||
Loading…
Reference in a new issue