Adding stream object to library. Got stream reading loop working. Added test program...
authorJosh Roesslein <jroesslein@gmail.com>
Fri, 7 Aug 2009 18:36:46 +0000 (13:36 -0500)
committerJosh Roesslein <jroesslein@gmail.com>
Fri, 7 Aug 2009 18:36:46 +0000 (13:36 -0500)
stream_watcher.py [new file with mode: 0644]
tweepy/__init__.py
tweepy/streaming.py [new file with mode: 0644]

diff --git a/stream_watcher.py b/stream_watcher.py
new file mode 100644 (file)
index 0000000..949c90a
--- /dev/null
@@ -0,0 +1,26 @@
+import time
+
+import tweepy
+
+def callback(stream_object):
+
+  if 'text' in stream_object:
+    print stream_object['text']
+    
+
+stream = tweepy.Stream('spritzer', 'tweebly', 'omega1987twitter')
+stream.connect(callback)
+
+while True:
+
+  try:
+    if not stream.running:
+      print 'Stream stopped!'
+      break
+
+    time.sleep(1)
+
+  except KeyboardInterrupt:
+    break
+
+stream.disconnect()
index e3543c7b0d4ab9af34d4d831ae7d97923a2ab26c..8b01bb7dcc10d52d55ff58a2b616e1f5f6c572ff 100644 (file)
@@ -12,6 +12,7 @@ from error import TweepError
 from api import API
 from cache import *
 from auth import BasicAuthHandler, OAuthHandler
+from streaming import Stream
 
 # Global, unauthenticated instance of API
 api = API()
diff --git a/tweepy/streaming.py b/tweepy/streaming.py
new file mode 100644 (file)
index 0000000..afa8cab
--- /dev/null
@@ -0,0 +1,67 @@
+# Tweepy
+# Copyright 2009 Joshua Roesslein
+# See LICENSE
+
+import httplib
+from threading import Thread
+
+from . auth import BasicAuthHandler
+
+try:
+  import json
+except ImportError:
+  import simplejson as json
+
+class Stream(object):
+
+  def __init__(self, method, username, password, host='stream.twitter.com', buffer_size=1500):
+    self.method = method if method[0] == '/' else '/' + method
+    self.host = host
+    self.auth = BasicAuthHandler(username, password)
+    self.running = False
+    self.buffer_size = buffer_size
+
+  def _run(self):
+    conn = httplib.HTTPConnection(self.host)
+    headers = {}
+    self.auth.apply_auth(None, None, headers, None)
+    conn.request('POST', self.method + '.json?delimited=length', headers=headers)
+    resp = conn.getresponse()
+    data = ''
+    while self.running:
+      if resp.isclosed():
+        break
+
+      # read length
+      length = ''
+      while True:
+        c = resp.read(1)
+        if c == '\n':
+          break
+        length += c
+      length = length.strip()
+      if length.isdigit():
+        length = int(length)
+      else:
+        continue
+
+      # read data
+      data = resp.read(length)
+      jobject = json.loads(data)
+      self.callback(jobject)
+
+    conn.close()
+    self.running = False
+
+  def connect(self, callback):
+    if self.running:
+      raise TweepError('Stream object already connected!')
+    self.callback = callback
+    self.running = True
+    Thread(target=self._run).start()
+    
+  def disconnect(self):
+    if not self.running:
+      raise TweepError('Stream object not connected!')
+    self.running = False
+