Skip to content

Instantly share code, notes, and snippets.

@embedly
Created June 1, 2010 21:44
Show Gist options
  • Save embedly/421551 to your computer and use it in GitHub Desktop.
Save embedly/421551 to your computer and use it in GitHub Desktop.
import urllib2
import json
import re
response = urllib2.urlopen('http://api.embed.ly/v1/api/services/python')
services = json.loads(response.read())
url = 'http://soundcloud.com/dociler/dociler-futuricity'
for service in services:
for regex in service['regex']:
if re.match(regex, url):
print "Matched %s to %s" % (url, service['displayname'])
break
{
"name": "soundcloud",
"type": "audio"
"displayname": "SoundCloud",
"domain": "soundcloud.com",
"subdomains": [],
  "favicon": "http://c1611152.cdn.cloudfiles.rackspacecloud.com/soundcloud.ico",
"regex": ["http://soundcloud.com/*",
"http://soundcloud.com/*/*",
"http://soundcloud.com/*/sets/*",
"http://soundcloud.com/groups/*"],
"about": "SoundCloud lets you move music fast & easy. The platform
takes the daily hassle out of receiving, sending & distributing music
for artists, record labels & other music professionals.",
}
"regex": ["http://soundcloud\\.com/.*",
"http://soundcloud\\.com/.*/.*",
"http://soundcloud\\.com/.*/sets/.*",
"http://soundcloud\\.com/groups/.*"
]
import memcache
cache = memcache.client(['127.0.0.1:11211'])
result = cache.get('embedly_services')
if result is None:
   result = urllib2.urlopen('http://api.embed.ly/v1/api/services/python').read()
   cache.get('embedly_services', result, 60*60*24)
services = json.loads(result)
params = {'url' : 'http://soundcloud.com/dociler/dociler-futuricity',
         'maxwidth' : 600,
         'format' : 'json'}
fetch_url = 'http://api.embed.ly/v1/api/oembed?%s' % urllib.urlencode(params)
try:
result = urllib2.urlopen(fetch_url).read()
except:
return None
oembed = json.loads(result)
if oembed is None or oembed['type'] == 'link':
return None
elif oembed['type'] in ['video', 'rich']:
return '<div class="embed">%s</div>' % oembed['html']
elif oembed['type'] == 'photo':
return '<div class="embed"><a href="%s" title="%s"><img src="%s"></img></a></div>'% (url,oembed.get('title', ''), oembed['url'])
import re
def replace(matchobj):
url = matchobj.groupdict().get('url', matchobj.groupdict().get('url_or_title', None))
bbcode_url_re = re.compile('\[url(|\=(?P<url>.*?))\](?P<url_or_title>.*?)\[/url]')
bbcode_url_re.sub(replace, text, re.M )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment