Last active
December 11, 2015 06:08
-
-
Save nfreear/4556849 to your computer and use it in GitHub Desktop.
ScraperWiki oEmbed provider API v1.2.
This file contains 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 scraperwiki | |
""" | |
ScraperWiki oEmbed provider API, v1.2. | |
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> | |
""" | |
class Scraperwiki_Oembed: | |
regex = '^https://views.scraperwiki.com/run/([\w_]+)/?' | |
about = '' | |
displayname = 'ScraperWiki - write code, get data' | |
type = 'rich' | |
about_url = 'https://scraperwiki.com/' | |
width = '100%' | |
height = 500 | |
#---- | |
import sys, os, cgi, re, json | |
from urlparse import urlparse | |
def getServiceName(): | |
# --scraper=NAME | |
return sys.argv[2][sys.argv[2].find('=') + 1 :] | |
def oembedGetUrl(regex): | |
p_url = re.compile(regex) | |
try: | |
qsenv = dict(cgi.parse_qsl(os.getenv("QUERY_STRING"))) | |
if 'url' in qsenv: | |
r_url = qsenv['url'] | |
m_url = p_url.match(r_url) | |
if not m_url: | |
print "Error, invalid 'url' parameter: ", url | |
exit(-400) | |
o = urlparse(r_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, ", r_url | |
return r_url, m_url.group(1) | |
def oembedRender(oembed_rsp): | |
p_cb = re.compile('^[a-zA-Z_][\w_]+$') | |
qsenv = dict(cgi.parse_qsl(os.getenv("QUERY_STRING"))) | |
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) | |
return | |
#---- | |
provider = Scraperwiki_Oembed | |
url, short_name = oembedGetUrl(provider.regex) | |
html_template = ("<div class='scraperwiki embed-rsp'>" | |
"<iframe width='{0}' height='{1}' frameborder='0' src='{2}'></iframe></div>".format(provider.width, provider.height, url)) | |
oembed_rsp = {'version' : '1.0', | |
'type' : provider.type, | |
'provider_name': provider.displayname, | |
'provider_url' : provider.about_url, | |
'original_url' : url, | |
'title' : short_name, | |
'service_name' : getServiceName(), | |
'html' : html_template, | |
'width': provider.width, | |
'height':provider.height} | |
oembedRender(oembed_rsp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment