mirror of
https://github.com/Hopiu/django-embed-video.git
synced 2026-03-16 21:30:23 +00:00
Get better resolutions from youtube with fallback for default resolution.
This commit is contained in:
parent
bd87759e51
commit
d234ec5185
1 changed files with 28 additions and 1 deletions
|
|
@ -18,6 +18,11 @@ 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
|
||||
|
|
@ -303,8 +308,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 +333,22 @@ 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 checkUrl(temp_thumbnail_url):
|
||||
return temp_thumbnail_url
|
||||
break
|
||||
return self.pattern_thumbnail_url.format(
|
||||
code=self.code, protocol=self.protocol, resolution='hqdefault.jpg')
|
||||
|
||||
|
||||
class VimeoBackend(VideoBackend):
|
||||
"""
|
||||
|
|
|
|||
Loading…
Reference in a new issue