Permalink
Browse files

Make it possible to customize the embed.ly URL

  • Loading branch information...
1 parent 6df5ae3 commit 1e97d92739ac2a8ad1b42ebb041c7e6c4f44b9c2 @matthiask matthiask committed Sep 6, 2011
Showing with 27 additions and 3 deletions.
  1. +14 −0 README.rst
  2. +13 −3 feincms_oembed/models.py
View
@@ -19,4 +19,18 @@ It's stunningly simple to use:
3. There is no third step!
+
+If you want to customize the Embedly_ request or use another OEmbed provider,
+set ``OEMBED_PROVIDER`` to a function receiving the URL and a dict with
+additional arguments and returning a suitable URL which returns OEmbed JSON
+on access. ``OEMBED_PROVIDER`` must either be a dotted python path or a
+callable::
+
+ from feincms_oembed.models import oembed_provider
+ def my_provider(url, kwargs):
+ kwargs['wmode'] = 'opaque'
+ return oembed_provider(url, kwargs)
+ OEMBED_PROVIDER = my_provider
+
+
.. _Embedly: http://embed.ly/
View
@@ -2,6 +2,7 @@
import hashlib
import urllib2
+from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
from django.utils import simplejson
@@ -12,6 +13,15 @@
DEFAULT_MAX_AGE = 7 * 24 * 60 * 60 # Cache lookups for a week
+def oembed_provider(url, kwargs):
+ kwargs['url'] = url
+ embedly_url = 'http://api.embed.ly/1/oembed?%s' % urlencode(kwargs)
+
+if getattr(settings, 'OEMBED_PROVIDER', None):
+ from feincms.utils import get_object
+ oembed_provider = get_object(settings.OEMBED_PROVIDER)
+
+
class CachedLookupManager(models.Manager):
def get_by_url(self, url, max_age=DEFAULT_MAX_AGE):
lookup, created = self.get_or_create(
@@ -31,9 +41,9 @@ 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)
+ lookup = self.get_by_url(
+ oembed_provider(url, kwargs),
+ max_age=max_age)
return simplejson.loads(lookup.response)

0 comments on commit 1e97d92

Please sign in to comment.