Created
June 9, 2009 00:30
-
-
Save jemerick/126173 to your computer and use it in GitHub Desktop.
spritzer sample code
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 urllib2 | |
import base64 | |
import threading | |
import Queue | |
import simplejson as json | |
from datetime import datetime | |
from dateutil import parser | |
# authentication stuff | |
username = 'username' | |
password = 'password' | |
basic = base64.encodestring('%s:%s' % (username, password))[:-1] | |
# a queue for holding all the incoming tweets | |
tweets = Queue.Queue(0) | |
# a consumer thread | |
class TweetConsumerThread(threading.Thread): | |
def run(self): | |
# loop forever | |
while True: | |
# get a tweet from the queue | |
tweet = tweets.get() | |
# if there was a tweet in the queue | |
if tweet: | |
# parse the json | |
t = json.loads(tweet) | |
# do whatever you want with the tweet here... | |
print '%s -- %s' % (t['id'], t['text']) | |
# a list of the threads | |
threads = [] | |
# create 2 threads | |
for x in xrange(2): | |
threads.append(TweetConsumerThread()) | |
# for each thread, set daemon mode to true and start them up | |
for t in threads: | |
t.daemon = True | |
t.start() | |
# create a request for the spritzer | |
request = urllib2.Request('http://stream.twitter.com/spritzer.json') | |
# add some basic auth | |
request.add_header('Authorization', 'Basic %s' % basic) | |
# open the request | |
spritzer = urllib2.urlopen(request) | |
# for each line in the spritzer handle (since each json tweet is on its own line...) | |
for tweet in spritzer: | |
# simple check for blank lines? | |
if len(tweet) > 2: | |
# put the tweet in the queue to be handled by the consumer threads | |
tweets.put(tweet) | |
# should never get here, but if it does, go ahead and be nice and close the stream | |
spritzer.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment