Created
June 1, 2010 21:44
-
-
Save embedly/421551 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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.", | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"regex": ["http://soundcloud\\.com/.*", | |
"http://soundcloud\\.com/.*/.*", | |
"http://soundcloud\\.com/.*/sets/.*", | |
"http://soundcloud\\.com/groups/.*" | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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