danielpronych / python-twitter

Automatically exported from code.google.com/p/python-twitter
Apache License 2.0
0 stars 0 forks source link

Fix for cursor bug in GetFollowers() #198

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
The cursor value is not updated for multiple loops in GetFollowers(). Patch to 
fix the problem:

diff -r 263fe2a0db8b twitter.py
--- a/twitter.py    Sun May 08 17:44:41 2011 -0400
+++ b/twitter.py    Wed Jun 08 19:41:02 2011 +0200
@@ -2994,6 +2994,7 @@
       if 'next_cursor' in data:
         if data['next_cursor'] == 0 or data['next_cursor'] == data['previous_cursor']:
           break
+        cursor = data['next_cursor']
       else:
         break
     return result

Original issue reported on code.google.com by jacob.so...@me.com on 8 Jun 2011 at 5:46

GoogleCodeExporter commented 8 years ago
OK, this has annoyed me so much I rewrote the thing. Working version below. 
Someone should check it, I've been a C programmer for 20 years, but python less 
than a week.

=================

  def GetFollowers(self, cursor=-1, rate_limit=350):
    '''Fetch the sequence of twitter.User instances, one for each follower

    The twitter.Api instance must be authenticated.

    Args:
      cursor:
        Specifies the Twitter API Cursor location to start at. [Optional]
        Note: there are pagination limits.
      rate_limit:
         Specifies the Twitter rate limit. Defaults to 350. Individuals are limited
         to 150

    Returns:
      A sequence of twitter.User instances, one for each follower
    '''
    if not self._oauth_consumer:
      raise TwitterError("twitter.Api instance must be authenticated")
    url = '%s/statuses/followers.json' % self.base_url
    result = []
    rate_check = 1
    while True:
      parameters = { 'cursor': cursor }
      json = self._FetchUrl(url, parameters=parameters)
      data = self._ParseAndCheckTwitter(json)
      result += [User.NewFromJsonDict(x) for x in data['users']]
      if 'next_cursor' in data:
        cursor = data['next_cursor']
        if data['next_cursor'] == 0 or data['next_cursor'] == data['previous_cursor']:
          break
      else:
        break
      if rate_check == rate_limit:
        break
      else:
        rate_check += 1

    return result

=============

Original comment by rick.dea...@gmail.com on 21 Sep 2011 at 2:42

GoogleCodeExporter commented 8 years ago
Looks good to me -- Python coder for 10 years, twitter hacker for ten minutes

Original comment by jamiesonbecker@gmail.com on 21 Sep 2011 at 9:55