Skip to content

Commit

Permalink
Merge pull request #53 from DiogoMarques29/master
Browse files Browse the repository at this point in the history
Get better resolutions from youtube with fallback for default resolution
  • Loading branch information
yetty committed Jan 19, 2016
2 parents bd87759 + cb39c24 commit a9d92e1
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -2,7 +2,7 @@ sudo: false
language: python
python:
- "2.7"
- "3.3"
- "3.4"
cache: pip
env:
- DJANGO_VERSION=1.5.2
Expand Down
22 changes: 21 additions & 1 deletion embed_video/backends.py
Expand Up @@ -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()
Expand All @@ -322,6 +328,20 @@ def get_code(self):

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):
"""
Expand Down
18 changes: 10 additions & 8 deletions embed_video/fields.py
@@ -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
Expand All @@ -19,14 +20,15 @@ def formfield(self, **kwargs):
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):
Expand Down
6 changes: 6 additions & 0 deletions embed_video/tests/backends/tests_youtube.py
Expand Up @@ -40,3 +40,9 @@ def test_thumbnail(self):
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)
2 changes: 2 additions & 0 deletions embed_video/tests/django_settings.py
Expand Up @@ -9,6 +9,8 @@
STATIC_URL = MEDIA_URL = '/static/'

INSTALLED_APPS = (
'django.contrib.contenttypes',
'django.contrib.auth',
'embed_video',
)

Expand Down
17 changes: 11 additions & 6 deletions embed_video/tests/tests_fields.py
Expand Up @@ -2,6 +2,7 @@
from unittest import TestCase

from django.forms import ValidationError
from django import VERSION

from ..fields import EmbedVideoField, EmbedVideoFormField
from ..backends import UnknownBackendException, UnknownIdException, \
Expand All @@ -16,12 +17,16 @@ def test_formfield_form_class(self):
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):
Expand Down

0 comments on commit a9d92e1

Please sign in to comment.