Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wistia support to django-embed-video #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions embed_video/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,51 @@ def get_thumbnail_url(self):
return self.info.get('thumbnail_large')


class WistiaBackend(VideoBackend):
"""
Backend for Wistia URLs.
"""
domain = None
is_secure = True

re_detect = re.compile(r'https://(?P<domain>[a-z]+).wistia.com/medias/*', re.I)
re_code = re.compile(r'''wistia\.com/(medias/(.*/)?|deliveries/)(?P<code>[a-z0-9;:@?&%=+/\$_.-]+)''', re.I)

pattern_url = '{protocol}://fast.wistia.net/embed/iframe/{code}'
pattern_info = '{protocol}://fast.wistia.net/oembed?url={protocol}%3A%2F%2F{domain}.wistia.com%2Fmedias%2F{code}&embedType=async'

@cached_property
def width(self):
"""
:rtype: str
"""
return self.info.get('width')

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

def get_info(self):
try:
response = requests.get(
self.pattern_info.format(domain=self.domain, code=self.code, protocol=self.protocol), timeout=EMBED_VIDEO_TIMEOUT
)
return json.loads(response.text)
except ValueError:
raise VideoDoesntExistException()

def get_thumbnail_url(self):
"""
Returns thumbnail URL folded from :py:data:`pattern_thumbnail_url` and
parsed code.
:rtype: str
"""
return self.info.get('thumbnail_url')


class SoundCloudBackend(VideoBackend):
"""
Backend for SoundCloud URLs.
Expand Down
1 change: 1 addition & 0 deletions embed_video/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
EMBED_VIDEO_BACKENDS = getattr(settings, 'EMBED_VIDEO_BACKENDS', (
'embed_video.backends.YoutubeBackend',
'embed_video.backends.VimeoBackend',
'embed_video.backends.WistiaBackend',
'embed_video.backends.SoundCloudBackend',
))
""" :type: tuple[str] """
Expand Down
31 changes: 31 additions & 0 deletions embed_video/tests/backends/tests_wistia.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import requests
from mock import patch
from unittest import TestCase

from . import BackendTestMixin
from embed_video.backends import WistiaBackend, VideoDoesntExistException


class WistiaBackendTestCase(BackendTestMixin, TestCase):

urls = (
('https://support.wistia.com/medias/26sk4lmiix', '26sk4lmiix'), # This comes from the wistia docs
('https://home.wistia.com/medias/342jss6yh5', '342jss6yh5'),
)

instance = WistiaBackend

def test_wistia_get_info_exception(self):
with self.assertRaises(VideoDoesntExistException):
backend = WistiaBackend('https://support.wistia.com/123')
backend.get_info()

def test_get_thumbnail_url(self):
backend = WistiaBackend('https://home.wistia.com/medias/342jss6yh5')
self.assertEqual(backend.get_thumbnail_url(),
'https://embed-ssl.wistia.com/deliveries/7fc8174a908ad1d349196405671e0fbdaa1e84eb.jpg?image_crop_resized=960x540')

@patch('embed_video.backends.EMBED_VIDEO_TIMEOUT', 0.000001)
def test_timeout_in_get_info(self):
backend = WistiaBackend('http://support.wistia.com/72304002')
self.assertRaises(requests.Timeout, backend.get_info)
1 change: 1 addition & 0 deletions embed_video/tests/django_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'embed_video.backends.YoutubeBackend',
'embed_video.backends.VimeoBackend',
'embed_video.backends.SoundCloudBackend',
'embed_video.backends.WistiaBackend',
'embed_video.tests.backends.tests_custom_backend.CustomBackend',
)

Expand Down