Permalink
Browse files

adds special handling for Vimeo and YouTube

  • Loading branch information...
1 parent e4967d0 commit b67cee859018f72405d8047cccf08190723c2dd8 @stefanfoulis stefanfoulis committed Nov 20, 2012
Showing with 52 additions and 15 deletions.
  1. +8 −1 README.rst
  2. +2 −2 djangocms_oembed/cms_plugins.py
  3. +41 −12 djangocms_oembed/models.py
  4. +1 −0 setup.py
View
@@ -1,4 +1,11 @@
djangocms-oembed
================
-A simple oembed plugin. Currently only video oembeds are implemented.
+A simple oembed plugin.
+Currently only video oembeds are implemented. More content types will follow.
+
+Sadly most advanced embedding options (e.g looping) are not supported by most oembed providers.
+So there is some special handling for YouTube and Vimeo embeds:
+
+* looping for Vimeo
+* no related videos at the end for YouTube
@@ -12,8 +12,8 @@ class CMSOembedVideoPlugin(CMSPluginBase):
admin_preview = False
text_enabled = True
fieldsets = (
- (None, {'fields': ('oembed_url', ('width', 'height',), ('autoplay', 'loop', 'show_related', ),)}),
- ('advanced', {'fields': ('type', 'provider', 'html', 'data')}),
+ (None, {'fields': ('oembed_url', ('width', 'height',), 'autoplay', 'loop', 'show_related',)}),
+ ('advanced', {'fields': ('type', 'provider', 'html', 'data'), 'classes': ['collapse']}),
)
readonly_fields = ('type', 'provider', 'html', 'data',)
View
@@ -1,25 +1,27 @@
# -*- coding: utf-8 -*-
from cms.models import CMSPlugin
+import pprint
+import urllib
+import urlparse
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import ugettext_lazy as _
import micawber
-from micawber.exceptions import ProviderNotFoundException
-
+from micawber.exceptions import ProviderNotFoundException, ProviderException
+from pyquery import PyQuery
providers = micawber.bootstrap_basic()
class OembedVideoPlugin(CMSPlugin):
- oembed_url = models.URLField()
+ oembed_url = models.URLField(verbose_name=_('url'))
width = models.IntegerField(null=True, blank=True)
height = models.IntegerField(null=True, blank=True)
autoplay = models.BooleanField(default=False)
- show_related = models.BooleanField(default=False, help_text=_('hiding related videos is not supported by vimeo'))
- loop = models.BooleanField(default=False)
-
+ show_related = models.BooleanField(default=False, help_text=_('hiding related videos is not supported by Vimeo (you need vimeo plus)'))
+ loop = models.BooleanField(default=False, help_text=_('looping is not supported by YouTube'))
- # this is cached from the request to oembed
+ # cached oembed data
type = models.CharField(max_length=255, blank=True, default='')
provider = models.CharField(max_length=255, blank=True, default='')
data = models.TextField(blank=True, default='')
@@ -34,16 +36,43 @@ def clean(self):
extra['autoplay'] = self.autoplay
extra['rel'] = self.show_related
extra['loop'] = self.loop
+ extra['title'] = False # Vimeo
+ extra['byline'] = False # Vimeo
+ extra['portrait'] = False # Vimeo
try:
data = providers.request(self.oembed_url, **extra)
except ProviderNotFoundException, e:
raise ValidationError(e.message)
+ except ProviderException, e:
+ raise ValidationError(e.message)
if not data['type'] == 'video':
- raise ValidationError('This must be an url for a video, not %(type)s.' % {'type': data['type']},)
- self.type = data['type']
- if 'provider_name' in data:
- self.provider = data['provider_name']
- self.html = data['html']
+ raise ValidationError('This must be an url for a video. The "%(type)s" type is not supported.' % {'type': data['type']},)
+ self.type = data.get('type', '')
+ self.provider = data.get('provider_name', '')
+ html = data.get('html', '')
+ if 'provider_name' in data and self.provider in ['YouTube', 'Vimeo']:
+ # dirty special handling of youtube and vimeo.
+ # they ignore these parameters over oembed... so we hack them into the iframe url.
+ iframe_html = PyQuery(html)
+ url = iframe_html.attr('src')
+ params = {
+ 'autoplay': int(self.autoplay),
+ 'loop': int(self.loop),
+ 'rel': int(self.show_related),
+ 'showinfo': 0 # YouTube
+ }
+ url_parts = list(urlparse.urlparse(url))
+ query = dict(urlparse.parse_qsl(url_parts[4]))
+ query.update(params)
+ url_parts[4] = urllib.urlencode(query)
+ new_url = urlparse.urlunparse(url_parts)
+ # for some reason this does not work with just an iframe node. And it also urlescapes the src url again
+ #iframe_html.attr['src'] = new_url
+ #new_html = iframe_html.html(method='html')
+ # quick and dirty
+ new_html = html.replace(url, new_url)
+ html = new_html
+ self.html = html
self.data = data
View
@@ -13,6 +13,7 @@
install_requires = (
'Django>=1.4',
'django-cms',
+ 'pyquery',
'micawber',
),
include_package_data=True,

0 comments on commit b67cee8

Please sign in to comment.