mirror of
https://github.com/Hopiu/django-embed-video.git
synced 2026-04-26 23:54:42 +00:00
support soundcloud
This commit is contained in:
parent
8fa15ab059
commit
a1dc7d99fa
3 changed files with 20 additions and 12 deletions
|
|
@ -6,6 +6,7 @@ DETECT_YOUTUBE = re.compile(
|
|||
DETECT_VIMEO = re.compile('^(http(s)?://(www\.)?)?vimeo\.com.*', re.I)
|
||||
DETECT_SOUNDCLOUD = re.compile('^(http(s)?://(www\.)?)?soundcloud\.com.*', re.I)
|
||||
import requests
|
||||
import json
|
||||
|
||||
class UnknownBackendException(Exception):
|
||||
pass
|
||||
|
|
@ -13,12 +14,11 @@ class NoIdVideoFound(Exception):
|
|||
pass
|
||||
|
||||
def detect_backend(url):
|
||||
assert False, DETECT_SOUNDCLOUD.match(url)
|
||||
if DETECT_YOUTUBE.match(url):
|
||||
return YoutubeBackend(url)
|
||||
elif DETECT_VIMEO.match(url):
|
||||
return VimeoBackend(url)
|
||||
elif DETECT_SOUNDCLOUD.match(url)
|
||||
elif DETECT_SOUNDCLOUD.match(url):
|
||||
return SoundCloundBackend(url)
|
||||
else:
|
||||
raise UnknownBackendException
|
||||
|
|
@ -53,21 +53,25 @@ class VideoBackend(object):
|
|||
|
||||
class SoundCloundBackend(VideoBackend):
|
||||
_base_url = "http://soundcloud.com/oembed"
|
||||
url = None
|
||||
def __init__(self, url):
|
||||
import requests
|
||||
params = {
|
||||
'format':'json',
|
||||
'url': url,
|
||||
}
|
||||
r = requests.get(self._base_url, params)
|
||||
json_response = r.json()
|
||||
r = requests.get(self._base_url, data=params)
|
||||
json_response = json.loads(r.text)
|
||||
self._response = json_response
|
||||
self.thumbnail = json_response.get("thumbnail_url")
|
||||
match = re.search(r'src="(.*?)"', json_response.get("html"))
|
||||
self.url = match[0]
|
||||
assert False,match
|
||||
|
||||
if match:
|
||||
self.url = match.groups()[0]
|
||||
super(SoundCloundBackend,self).__init__(url)
|
||||
|
||||
def get_thumbnail_url(self):
|
||||
pass
|
||||
return self.thumbnail
|
||||
|
||||
def get_url(self):
|
||||
return self.url
|
||||
def get_code(self):
|
||||
|
|
|
|||
|
|
@ -26,12 +26,11 @@ class EmbedVideoField(models.URLField):
|
|||
class EmbedVideoFormField(forms.URLField):
|
||||
def validate(self, url):
|
||||
super(EmbedVideoFormField, self).validate(url)
|
||||
detect_backend(url)
|
||||
try:
|
||||
detect_backend(url)
|
||||
except UnknownBackendException:
|
||||
raise forms.ValidationError(_(u'URL could not be recognized.'))
|
||||
except NoIdVideoFound:
|
||||
except NoIdVideoFound:
|
||||
raise forms.ValidationError(_(u'Video Id not found .'))
|
||||
|
||||
return url
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from django.template import Library, Node, TemplateSyntaxError
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
from ..base import detect_backend
|
||||
from ..base import detect_backend, SoundCloundBackend
|
||||
|
||||
register = Library()
|
||||
|
||||
|
|
@ -48,6 +48,8 @@ def embed(backend, _size='small'):
|
|||
'huge': (1280, 960),
|
||||
}
|
||||
|
||||
|
||||
|
||||
if _size in sizes:
|
||||
size = sizes[_size]
|
||||
elif 'x' in _size:
|
||||
|
|
@ -58,7 +60,10 @@ def embed(backend, _size='small'):
|
|||
'width': int(size[0]),
|
||||
'height': int(size[1]),
|
||||
}
|
||||
|
||||
if isinstance(backend,SoundCloundBackend):
|
||||
# max height of soundcloud
|
||||
params.update({'height':81})
|
||||
|
||||
return mark_safe(
|
||||
'<iframe width="%(width)d" height="%(height)d" '
|
||||
'src="%(url)s" frameborder="0" allowfullscreen>'
|
||||
|
|
|
|||
Loading…
Reference in a new issue