Skip to content

Instantly share code, notes, and snippets.

@embedly
Created June 2, 2010 07:39
Show Gist options
  • Save embedly/422072 to your computer and use it in GitHub Desktop.
Save embedly/422072 to your computer and use it in GitHub Desktop.
"""
Generate New Provider list for jQuery oEmbed.
"""
import json
import urllib2
import json
import re
def main():
response = urllib2.urlopen('http://api.embed.ly/v1/api/services/javascript')
services = json.loads(response.read())
#don't rock the boat.
providers = ['new OEmbedProvider("wikipedia", "http:\/\/wikipedia\.org\/wiki\/.*", "http://oohembed.com/oohembed/")',]
for service in services:
#Ones that have script tags.
if service['name'] in ['github','polldaddy','polleverywhere']:
continue
for regex in service['regex']:
providers.append('new OEmbedProvider("%s", "%s", "http://api.embed.ly/v1/api/oembed")' % (service['name'], regex))
jquery_oembed = urllib2.urlopen('http://jquery-oembed.googlecode.com/svn/trunk/jquery.oembed.js').read()
provider_text = u'\tvar providers = [\n'
for i, provider in enumerate(providers):
if i is len(providers)-1:
provider_text += '\t\t\t%s\n' % provider
else:
provider_text += '\t\t\t%s,\n' % provider
provider_text += '\t]'
#Replace the providers list.
p_replace = re.compile('var\sproviders\s=\s\[.*?\]', re.S)
jquery_oembed = p_replace.sub(provider_text, unicode(jquery_oembed, 'ascii', 'ignore'))
#Replace
matches = "this.matches = function(externalUrl) {\n\t\t\treturn externalUrl.match(this.urlPattern) != null;\n\t\t}"
m_replace = re.compile('this\.matches\s=\sfunction\(externalUrl\)\s\{.*?\}', re.S)
jquery_oembed = m_replace.sub(matches, jquery_oembed)
f = open('jquery.oembed.js', 'w')
f.write(jquery_oembed)
f.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment