Skip to content

Instantly share code, notes, and snippets.

@screeley
Created July 19, 2010 18:58
Show Gist options
  • Save screeley/481809 to your computer and use it in GitHub Desktop.
Save screeley/481809 to your computer and use it in GitHub Desktop.
import re
import urllib
import urllib2
# JSON decoder
try:
import json
except ImportError:
try:
import simplejson as json
except ImportError:
raise ImportError("Need a json decoder")
# Embed.ly Multi Provider API Endpoint
OEMBED_API_ENDPOINT = 'http://api.embed.ly/v1/api/oembed'
#generate from http://api.embed.ly/tools/generator. Do not use .*
embedly_re = re.compile('.*')
def get_multi(urls, **kwargs):
"""
Get Multiple oEmbed responses
"""
oembed_urls = []
for url in urls:
if embedly_re.search(url):
oembed_urls.append(urllib.quote(url))
query = urllib.urlencode(kwargs)
if query:
query += '&urls=%s' % ','.join(oembed_urls)
else:
query = 'urls=%s' % ','.join(oembed_urls)
# api endpoint url
fetch_url = "%s?%s" %(OEMBED_API_ENDPOINT,query)
try:
r = urllib2.urlopen(fetch_url).read()
objs = json.loads(r)
except urllib2.HTTPError, e:
return []
except urllib2.URLError:
return []
return objs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment