From 1ad609996b7130fbb3b3feb8782539075478f8f3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 18 Jun 2013 17:30:53 -0500 Subject: [PATCH] enhanced --- embed_video/base.py | 13 +++++++++++-- embed_video/fields.py | 6 ++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/embed_video/base.py b/embed_video/base.py index b3d06ab..2f3af47 100644 --- a/embed_video/base.py +++ b/embed_video/base.py @@ -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 diff --git a/embed_video/fields.py b/embed_video/fields.py index 92ca84d..a40c238 100644 --- a/embed_video/fields.py +++ b/embed_video/fields.py @@ -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