Commit 377b8360 authored by Josh Roesslein's avatar Josh Roesslein
Browse files

Polish up stream watcher. Add spitzer, follow, and track streaming modes to stream object.

parent e48a880c
......@@ -7,21 +7,40 @@ def callback(status):
print status.text
# Prompt for login credentials and setup stream object
username = raw_input('Twitter username: ')
password = getpass('Twitter password: ')
stream = tweepy.Stream('spritzer', username, password)
stream.connect(callback)
stream = tweepy.Stream(username, password, callback)
# Prompt for mode of streaming and connect
while True:
mode = raw_input('Mode? [spritzer/follow/track] ')
if mode == 'spritzer':
stream.spritzer()
break
elif mode == 'follow':
follow_list = raw_input('Users to follow (comma separated): ')
stream.follow(follow_list)
break
elif mode == 'track':
track_list = raw_input('Keywords to track (comma separated): ')
stream.track(track_list)
break
else:
print 'Invalid choice! Try again.'
# Run in a loop until termination
while True:
try:
if not stream.running:
if stream.running is False:
print 'Stream stopped!'
break
time.sleep(1)
except KeyboardInterrupt:
break
# Shutdown connection
stream.disconnect()
print 'Bye!'
......@@ -8,6 +8,7 @@ from threading import Thread
from . auth import BasicAuthHandler
from . parsers import parse_status
from . api import API
from . error import TweepError
try:
import json
......@@ -16,19 +17,19 @@ except ImportError:
class Stream(object):
def __init__(self, method, username, password, host='stream.twitter.com', buffer_size=1500):
self.method = method if method[0] == '/' else '/' + method
def __init__(self, username, password, callback, host='stream.twitter.com', buffer_size=1500):
self.host = host
self.auth = BasicAuthHandler(username, password)
self.running = False
self.buffer_size = buffer_size
self.callback = callback
def _run(self):
api = API()
conn = httplib.HTTPConnection(self.host)
conn = httplib.HTTPConnection(self.host, timeout=5)
headers = {}
self.auth.apply_auth(None, None, headers, None)
conn.request('POST', self.method + '.json?delimited=length', headers=headers)
conn.request('POST', self.url, headers=headers)
resp = conn.getresponse()
data = ''
while self.running:
......@@ -37,7 +38,7 @@ class Stream(object):
# read length
length = ''
while True:
while resp.isclosed() is False:
c = resp.read(1)
if c == '\n':
break
......@@ -62,15 +63,33 @@ class Stream(object):
conn.close()
self.running = False
def connect(self, callback):
def spritzer(self):
if self.running:
raise TweepError('Stream object already connected!')
self.callback = callback
self.url = '/spritzer.json?delimited=length'
self.running = True
Thread(target=self._run).start()
def follow(self, follow=None):
if self.running:
raise TweepError('Stream object already connected!')
self.url = '/follow.json?delimited=length'
if follow:
self.url += '&follow=%s' % str(follow).strip('[]').replace(' ', '')
self.running = True
Thread(target=self._run).start()
def track(self, track=None):
if self.running:
raise TweepError('Stream object already connected!')
self.url = '/track.json?delimited=length'
if track:
self.url += '&track=%s' % str(track).strip('[]').replace(' ', '')
self.running = True
Thread(target=self._run).start()
def disconnect(self):
if not self.running:
raise TweepError('Stream object not connected!')
if self.running is False:
return
self.running = False
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment