Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

"count" to be handled internally #188

Closed
wants to merge 4 commits into from

2 participants

Pavlo Baron Joshua Roesslein
Pavlo Baron

Twitter's sample request doesn't accept "count", so any attempt to use this parameter resulted in a 401. This patch implements internal count. Every relevant loop takes care of counting statuses and breaks when the maximum is reached.

Joshua Roesslein

This would be fairly trivial to implement in your own stream listener class. I don't see enough value having this inside the stream loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 14, 2012
  1. Pavlo Baron
  2. Pavlo Baron

    fixed double closing

    pavlobaron authored
Commits on Jul 17, 2012
  1. Pavlo Baron
Commits on Aug 26, 2012
  1. Pavlo Baron
This page is out of date. Refresh to see the latest.
Showing with 16 additions and 9 deletions.
  1. +16 −9 tweepy/streaming.py
25 tweepy/streaming.py
View
@@ -85,7 +85,7 @@ def __init__(self, auth, listener, **options):
self.parameters = None
self.body = None
- def _run(self):
+ def _run(self, count=None):
# Authenticate
url = "%s://%s%s" % (self.scheme, self.host, self.url)
@@ -93,6 +93,7 @@ def _run(self):
error_counter = 0
conn = None
exception = None
+ counter = 0
while self.running:
if self.retry_count is not None and error_counter > self.retry_count:
# quit if error count greater than retry count
@@ -114,7 +115,10 @@ def _run(self):
sleep(self.retry_time)
else:
error_counter = 0
- self._read_loop(resp)
+ counter = self._read_loop(resp, count=count, counter=counter)
+ if count and counter == count:
+ self.on_closed(resp)
+ break
except timeout:
if self.listener.on_timeout() == False:
break
@@ -139,8 +143,7 @@ def _data(self, data):
if self.listener.on_data(d) is False:
self.running = False
- def _read_loop(self, resp):
-
+ def _read_loop(self, resp, count=None, counter=None):
while self.running and not resp.isclosed():
# Note: keep-alive newlines might be inserted before each length value.
@@ -161,15 +164,21 @@ def _read_loop(self, resp):
next_status_obj = resp.read( int(delimited_string) )
self._data(next_status_obj)
+ if count:
+ counter += 1
+ if counter == count:
+ break
if resp.isclosed():
self.on_closed(resp)
- def _start(self, async):
+ return counter
+
+ def _start(self, async, count=None):
self.running = True
if async:
Thread(target=self._run).start()
else:
- self._run()
+ self._run(count=count)
def on_closed(self, resp):
""" Called when the response has been closed by Twitter """
@@ -204,9 +213,7 @@ def sample(self, count=None, async=False):
if self.running:
raise TweepError('Stream object already connected!')
self.url = '/%i/statuses/sample.json?delimited=length' % STREAM_VERSION
- if count:
- self.url += '&count=%s' % count
- self._start(async)
+ self._start(async, count=count)
def filter(self, follow=None, track=None, async=False, locations=None, count = None):
self.parameters = {}
Something went wrong with that request. Please try again.