Skip to content

Instantly share code, notes, and snippets.

@storborg
Created November 22, 2011 23:14
Show Gist options
  • Save storborg/1387370 to your computer and use it in GitHub Desktop.
Save storborg/1387370 to your computer and use it in GitHub Desktop.
"""
Poor man's firehose: use your friends to grab twitter data, bypassing the IP
rate limit by using a bunch of different clients. This works by serving up
javascript which will make the client grab a JSONP feed from twitter and then
re-submit it to your server.
Obviously, modify this to be more useful, e.g. by setting the page to
auto-refresh itself or use long polling to get more URLs to fetch.
Call this like:
python poor-mans-firehose.py <username>
To grab a feed for a given username.
In this form, requires webob and gevent, which you can install with
% easy_install webob gevent
"""
from webob import Request, Response
twitter_jsonp_url = \
'http://twitter.com/status/user_timeline/%(username)s.json?count=10'
templ = '''
<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
<script>
$(function() {
console.log("starting jsonp req");
$.ajax({
url: '%(jsonp_url)s',
dataType: 'jsonp',
success: function(data) {
console.log("jsonp handler", data);
$.ajax({
type: 'POST',
url: window.location,
data: JSON.stringify(data)
});
}
});
});
</script>
</head>
<body>
<div>Working, thanks.</div>
</body>
</html>
'''
class App(object):
def __init__(self, username):
self.username = username
def __call__(self, environ, start_response):
req = Request(environ)
resp = Response(self.process(req))
return resp(environ, start_response)
def process(self, req):
if req.method == 'POST':
# Dump le data.
print req.body
return ''
else:
# Show le template.
jsonp_url = twitter_jsonp_url % {'username': self.username}
s = templ % {'jsonp_url': jsonp_url}
return s
if __name__ == '__main__':
import sys
from gevent.wsgi import WSGIServer
username = sys.argv[1]
app = App(username)
port = 1337
server = WSGIServer(('0.0.0.0', port), app)
print "Serving for user %r, hit me at localhost:%d" % (username, port)
server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment