mirror of
https://github.com/Hopiu/django-embed-video.git
synced 2026-05-04 19:14:42 +00:00
Merge pull request #53 from DiogoMarques29/master
Get better resolutions from youtube with fallback for default resolution
This commit is contained in:
commit
a9d92e190c
6 changed files with 51 additions and 16 deletions
|
|
@ -2,7 +2,7 @@ sudo: false
|
|||
language: python
|
||||
python:
|
||||
- "2.7"
|
||||
- "3.3"
|
||||
- "3.4"
|
||||
cache: pip
|
||||
env:
|
||||
- DJANGO_VERSION=1.5.2
|
||||
|
|
|
|||
|
|
@ -303,8 +303,14 @@ class YoutubeBackend(VideoBackend):
|
|||
)
|
||||
|
||||
pattern_url = '{protocol}://www.youtube.com/embed/{code}'
|
||||
pattern_thumbnail_url = '{protocol}://img.youtube.com/vi/{code}/hqdefault.jpg'
|
||||
pattern_thumbnail_url = '{protocol}://img.youtube.com/vi/{code}/{resolution}'
|
||||
default_query = EMBED_VIDEO_YOUTUBE_DEFAULT_QUERY
|
||||
resolutions = [
|
||||
'maxresdefault.jpg',
|
||||
'sddefault.jpg',
|
||||
'hqdefault.jpg',
|
||||
'mqdefault.jpg',
|
||||
]
|
||||
|
||||
def get_code(self):
|
||||
code = super(YoutubeBackend, self).get_code()
|
||||
|
|
@ -322,6 +328,20 @@ class YoutubeBackend(VideoBackend):
|
|||
|
||||
return code
|
||||
|
||||
def get_thumbnail_url(self):
|
||||
"""
|
||||
Returns thumbnail URL folded from :py:data:`pattern_thumbnail_url` and
|
||||
parsed code.
|
||||
|
||||
:rtype: str
|
||||
"""
|
||||
for resolution in self.resolutions:
|
||||
temp_thumbnail_url = self.pattern_thumbnail_url.format(
|
||||
code=self.code, protocol=self.protocol, resolution=resolution)
|
||||
if int(requests.head(temp_thumbnail_url).status_code) < 400:
|
||||
return temp_thumbnail_url
|
||||
return None
|
||||
|
||||
|
||||
class VimeoBackend(VideoBackend):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
from django.db import models
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django import VERSION
|
||||
|
||||
from .backends import detect_backend, UnknownIdException, \
|
||||
UnknownBackendException
|
||||
|
|
@ -19,14 +20,15 @@ class EmbedVideoField(models.URLField):
|
|||
defaults.update(kwargs)
|
||||
return super(EmbedVideoField, self).formfield(**defaults)
|
||||
|
||||
def south_field_triple(self):
|
||||
from south.modelsinspector import introspector
|
||||
cls_name = '%s.%s' % (
|
||||
self.__class__.__module__,
|
||||
self.__class__.__name__
|
||||
)
|
||||
args, kwargs = introspector(self)
|
||||
return (cls_name, args, kwargs)
|
||||
if VERSION < (1, 9):
|
||||
def south_field_triple(self):
|
||||
from south.modelsinspector import introspector
|
||||
cls_name = '%s.%s' % (
|
||||
self.__class__.__module__,
|
||||
self.__class__.__name__
|
||||
)
|
||||
args, kwargs = introspector(self)
|
||||
return (cls_name, args, kwargs)
|
||||
|
||||
|
||||
class EmbedVideoFormField(forms.URLField):
|
||||
|
|
|
|||
|
|
@ -40,3 +40,9 @@ class YoutubeBackendTestCase(BackendTestMixin, TestCase):
|
|||
for url in self.urls:
|
||||
backend = self.instance(url[0])
|
||||
self.assertIn(url[1], backend.thumbnail)
|
||||
|
||||
def test_get_better_resolution_youtube(self):
|
||||
backend = self.instance('https://www.youtube.com/watch?v=1Zo0-sWD7xE')
|
||||
self.assertIn(
|
||||
'img.youtube.com/vi/1Zo0-sWD7xE/maxresdefault.jpg',
|
||||
backend.thumbnail)
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ STATIC_ROOT = MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'static')
|
|||
STATIC_URL = MEDIA_URL = '/static/'
|
||||
|
||||
INSTALLED_APPS = (
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.auth',
|
||||
'embed_video',
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ from mock import patch
|
|||
from unittest import TestCase
|
||||
|
||||
from django.forms import ValidationError
|
||||
from django import VERSION
|
||||
|
||||
from ..fields import EmbedVideoField, EmbedVideoFormField
|
||||
from ..backends import UnknownBackendException, UnknownIdException, \
|
||||
|
|
@ -16,12 +17,16 @@ class EmbedVideoFieldTestCase(TestCase):
|
|||
self.assertIsInstance(self.field.formfield(),
|
||||
EmbedVideoFormField)
|
||||
|
||||
def test_south(self):
|
||||
self.assertEqual(self.field.south_field_triple(),
|
||||
(
|
||||
'embed_video.fields.EmbedVideoField',
|
||||
[], {'max_length': '200'}
|
||||
))
|
||||
if VERSION < (1, 9):
|
||||
def test_south(self):
|
||||
self.assertEqual(
|
||||
self.field.south_field_triple(),
|
||||
(
|
||||
'embed_video.fields.EmbedVideoField',
|
||||
[],
|
||||
{'max_length': '200'}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class EmbedVideoFormFieldTestCase(TestCase):
|
||||
|
|
|
|||
Loading…
Reference in a new issue