IssueID #53: Fixed code for get thumbnail from youtube, some fixes to testes about django 1.9.1

This commit is contained in:
DiogoMarques 2016-01-18 16:03:31 +00:00
parent d234ec5185
commit 6573ef549f
3 changed files with 11 additions and 10 deletions

View file

@ -18,11 +18,6 @@ from .settings import EMBED_VIDEO_BACKENDS, EMBED_VIDEO_TIMEOUT, \
EMBED_VIDEO_YOUTUBE_DEFAULT_QUERY
def checkUrl(url):
r = requests.head(url)
return int(r.status_code) < 400
class EmbedVideoException(Exception):
""" Parental class for all embed_video exceptions """
pass
@ -313,7 +308,7 @@ class YoutubeBackend(VideoBackend):
resolutions = [
'maxresdefault.jpg',
'sddefault.jpg',
'hqdefault.jpg'
'hqdefault.jpg',
'mqdefault.jpg',
]
@ -343,11 +338,9 @@ class YoutubeBackend(VideoBackend):
for resolution in self.resolutions:
temp_thumbnail_url = self.pattern_thumbnail_url.format(
code=self.code, protocol=self.protocol, resolution=resolution)
if checkUrl(temp_thumbnail_url):
if int(requests.head(temp_thumbnail_url).status_code) < 400:
return temp_thumbnail_url
break
return self.pattern_thumbnail_url.format(
code=self.code, protocol=self.protocol, resolution='hqdefault.jpg')
return None
class VimeoBackend(VideoBackend):

View file

@ -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)

View file

@ -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',
)