Skip to content

Instantly share code, notes, and snippets.

@nfreear
Last active December 11, 2015 05:19
Show Gist options
  • Select an option

  • Save nfreear/4551718 to your computer and use it in GitHub Desktop.

Select an option

Save nfreear/4551718 to your computer and use it in GitHub Desktop.
ScraperWiki - oEmbed provider API (my first Python!) -- https://scraperwiki.com/views/scraperwiki_oembed_v1
import scraperwiki
"""
ScraperWiki oEmbed provider API, v1
Copyright 2013-01-16 Nick Freear. All rights reserved.
License: GNU General Public License <http://gnu.org/licenses/gpl-3.0.html>
Usage:
https://views.scraperwiki.com/run/scraperwiki_oembed_v1/?callback=_jsonp12&format=json&url=https%3A//views.scraperwiki.com/run/cloudworks_mindmap/%3FcloudscapeID=2451
Source: <https://gist.github.com/4551718>
"""
import os, cgi, re, json
from urlparse import urlparse
p_url = re.compile('^https://views.scraperwiki.com/run/([\w_]+)/?')
p_cb = re.compile('^[a-zA-Z_][\w_]+$')
try:
qsenv = dict(cgi.parse_qsl(os.getenv("QUERY_STRING")))
if 'url' in qsenv:
url = qsenv['url']
m_url = p_url.match(url)
if not m_url:
print "Error, invalid 'url' parameter: ", url
exit(-400)
o = urlparse(url)
if o.scheme != 'https':
print "Error, invalid 'url' parameter (2): ", url
exit(-3)
else:
print "Error, 'url' parameter is missing."
exit(-2)
except:
exit(-1)
# print "OK, URL found, ", url
#---
html_template = ("<div class='scraperwiki embed-rsp'>"
"<iframe width='100%%' height='400' frameborder='0' src='%s'></iframe></div>" % url)
#---
oembed_rsp = {'version' : '1.0',
'type' : 'rich',
'provider_name': 'ScraperWiki - write code, get data',
'provider_url' : 'https://scraperwiki.com/',
'original_url' : url,
'html' : html_template}
if 'callback' in qsenv:
callback = qsenv['callback']
m_cb = p_cb.match(callback)
if not m_cb:
print "Error, invalid 'callback' parameter: ", callback
exit(-4)
scraperwiki.utils.httpresponseheader("Content-Type", "application/json")
if 'callback' in qsenv:
print callback, '(', json.dumps(oembed_rsp), ')'
else:
print json.dumps(oembed_rsp)
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment