Created
August 10, 2011 16:28
-
-
Save screeley/1137334 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
#!/usr/bin/env python | |
"""Proxy""" | |
import urllib | |
import json | |
import tornado.httpserver | |
import tornado.ioloop | |
import tornado.web | |
import tornado.wsgi | |
import tornado.httpclient | |
HTML = """ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> | |
</head> | |
<body> | |
<form method="GET"> | |
<input id="id_url" type="text" name="url" /> | |
<input id="id_submit" type="submit" value="Submit" /> | |
</form> | |
<script> | |
$(document).ready(function(){ | |
$('form').bind('submit', function(e){ | |
e.preventDefault(); | |
$.ajax({ | |
url: '/proxy', | |
data: $(this).serialize(), | |
success: function(data){ | |
alert('{\\n'+$.map(data, function(v,n){return '\\t'+n+' : '+v+','}).join('\\n')+'\\n}'); | |
}, | |
dataType: 'json' | |
}); | |
}); | |
}); | |
</script> | |
</body> | |
</html> | |
""" | |
class MainHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.write(HTML) | |
class ProxyHandler(tornado.web.RequestHandler): | |
@tornado.web.asynchronous | |
def get(self): | |
#Get the url out of the request arguement | |
url = self.get_argument('url') | |
#create data | |
data = { | |
'key' : ':key', #your API key | |
'maxwidth' : 500, | |
'frame' : 'true', | |
'secure' : 'true', | |
'autoplay' : 'true', | |
'url' : url | |
} | |
#Create the Fetch URL | |
fetch_url = 'http://api.embed.ly/1/preview?%s' % urllib.urlencode(data) | |
http = tornado.httpclient.AsyncHTTPClient() | |
http.fetch(fetch_url, self._metadata_callback) | |
def _metadata_callback(self, response): | |
#Load the content as json | |
data = json.loads(response.body) | |
def clean(value): | |
#Make sure there is no malicious values | |
return value | |
#Here is where you could clean values | |
for name, value in data.items(): | |
data[name] = clean(value) | |
self.write(json.dumps(data)) | |
self.finish(); | |
url_mapping = [ | |
(r"/", MainHandler), | |
(r"/proxy", ProxyHandler), | |
] | |
if __name__ == "__main__": | |
application = tornado.web.Application(url_mapping, {'debug':True}) | |
http_server = tornado.httpserver.HTTPServer(application, xheaders=True) | |
http_server.listen(8000) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment