Created
September 24, 2012 21:48
-
-
Save paulcnichols/3778634 to your computer and use it in GitHub Desktop.
Sample python firehose client
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
import urllib2 | |
import json | |
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() | |
password_mgr.add_password(None, 'https://stream.twitter.com/1/statuses/sample.json', '<user>', '<password>') | |
handler = urllib2.HTTPBasicAuthHandler(password_mgr) | |
opener = urllib2.build_opener(handler) | |
try: | |
conn = opener.open('https://stream.twitter.com/1/statuses/sample.json') | |
except urllib2.HTTPError, exception: | |
if exception.code == 401: | |
raise AuthenticationError("Access denied") | |
elif exception.code == 404: | |
raise ConnectionError("URL not found: %s" % self.url) | |
else: # re raise. No idea what would cause this, so want to know | |
raise | |
except urllib2.URLError, exception: | |
raise ConnectionError(exception.reason) | |
for line in conn: | |
try: | |
obj = json.loads(line) | |
print obj | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment