mirror of
https://github.com/Hopiu/django-embed-video.git
synced 2026-04-13 01:30:58 +00:00
Merge pull request #1 from zodman/master
Enhanced getingt the id code from youtube
This commit is contained in:
commit
5b994c2b5a
2 changed files with 15 additions and 4 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import re
|
||||
|
||||
import urlparse
|
||||
DETECT_YOUTUBE = re.compile(
|
||||
'^(http(s)?://(www\.)?)?youtu(\.?)be(\.com)?.*', re.I
|
||||
)
|
||||
|
|
@ -8,7 +8,8 @@ DETECT_VIMEO = re.compile('^(http(s)?://(www\.)?)?vimeo\.com.*', re.I)
|
|||
|
||||
class UnknownBackendException(Exception):
|
||||
pass
|
||||
|
||||
class NoIdVideoFound(Exception):
|
||||
pass
|
||||
|
||||
def detect_backend(url):
|
||||
if DETECT_YOUTUBE.match(url):
|
||||
|
|
@ -31,6 +32,14 @@ class VideoBackend(object):
|
|||
match = self.re_code.search(self._url)
|
||||
if match:
|
||||
return match.group('code')
|
||||
else:
|
||||
parse_data = urlparse.urlparse(self._url)
|
||||
try:
|
||||
return urlparse.parse_qs(parse_data.query)["v"][0]
|
||||
except KeyError:
|
||||
pass
|
||||
raise NoIdVideoFound
|
||||
|
||||
|
||||
def get_url(self):
|
||||
return self.pattern_url % self.code
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from django.db import models
|
|||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from .base import detect_backend
|
||||
from .base import detect_backend, NoIdVideoFound, UnknownBackendException
|
||||
|
||||
__all__ = ('EmbedVideoField', 'EmbedVideoFormField')
|
||||
|
||||
|
|
@ -29,7 +29,9 @@ class EmbedVideoFormField(forms.URLField):
|
|||
|
||||
try:
|
||||
detect_backend(url)
|
||||
except:
|
||||
except UnknownBackendException:
|
||||
raise forms.ValidationError(_(u'URL could not be recognized.'))
|
||||
except NoIdVideoFound:
|
||||
raise forms.ValidationError(_(u'Video Id not found .'))
|
||||
|
||||
return url
|
||||
|
|
|
|||
Loading…
Reference in a new issue