Skip to content

Commit

Permalink
Make oembed through embed.ly more reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Aug 23, 2011
1 parent 01a5d20 commit 6df5ae3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
29 changes: 9 additions & 20 deletions feincms_oembed/contents.py
@@ -1,11 +1,9 @@
import feedparser
from urllib import urlopen

from django.core.exceptions import ValidationError
from django.db import models
from django.template.loader import render_to_string
from django.utils import simplejson
from django.utils.http import urlquote, urlencode
from django.utils.translation import ugettext_lazy as _

from feincms_oembed.models import CachedLookup
Expand All @@ -20,36 +18,27 @@ class Meta:
verbose_name_plural = _('External contents')

@classmethod
def initialize_type(cls, PARAM_CHOICES=None, DIMENSION_CHOICES=None):
if PARAM_CHOICES is not None:
cls.add_to_class('parameters', models.CharField(max_length=50,
choices=PARAM_CHOICES,
default=PARAM_CHOICES[0][0]))
def initialize_type(cls, DIMENSION_CHOICES=None):
if DIMENSION_CHOICES is not None:
cls.add_to_class('dimension', models.CharField(_('dimension'),
max_length=10, blank=True, null=True, choices=DIMENSION_CHOICES,
default=DIMENSION_CHOICES[0][0]))

def get_html_from_json(self):
params = ''
params = {}
if 'dimension' in dir(self):
dimensions = self.dimension.split('x')
params += urlencode({'maxwidth' : dimensions[0], 'maxheight' : dimensions[1]})
if 'parameters' in dir(self):
params += self.parameters
if len(params) > 0:
params = '&%s' % params

oohembed_url = 'http://api.embed.ly/1/oembed?url=%s%s' % (urlquote(self.url), params)
params.update({'maxwidth' : dimensions[0], 'maxheight' : dimensions[1]})

try:
json = simplejson.loads(CachedLookup.objects.request(oohembed_url))
type = json.get('type')
embed = CachedLookup.objects.oembed(self.url, **params)
except simplejson.JSONDecodeError:
raise ValidationError('The specified url %s does not respond oembed json' % oohembed_url)
raise ValidationError('The specified URL %s cannot be used with embed.ly' % self.url)

return render_to_string(('external/%s.html' % type, 'external/default.html'),
{'response' : json, 'content' : self})
return render_to_string((
'external/%s.html' % embed.get('type', 'default'),
'external/default.html',
), {'response': embed, 'content': self})

def clean(self, *args, **kwargs):
self.get_html_from_json()
Expand Down
8 changes: 8 additions & 0 deletions feincms_oembed/models.py
Expand Up @@ -4,6 +4,8 @@

from django.core.exceptions import ValidationError
from django.db import models
from django.utils import simplejson
from django.utils.http import urlencode
from django.utils.translation import ugettext_lazy as _


Expand All @@ -28,6 +30,12 @@ def get_by_url(self, url, max_age=DEFAULT_MAX_AGE):
def request(self, url, max_age=DEFAULT_MAX_AGE):
return self.get_by_url(url, max_age).response

def oembed(self, url, max_age=DEFAULT_MAX_AGE, **kwargs):
kwargs['url'] = url
embedly_url = 'http://api.embed.ly/1/oembed?%s' % urlencode(kwargs)
lookup = self.get_by_url(embedly_url, max_age=max_age)
return simplejson.loads(lookup.response)


class CachedLookup(models.Model):
hash = models.CharField(_('hash'), max_length=40, unique=True,
Expand Down

0 comments on commit 6df5ae3

Please sign in to comment.