Skip to content

Commit

Permalink
Fixed a bunch of bugs in the paging mechanisms of the IdIterator:
Browse files Browse the repository at this point in the history
- Passing same argument twice (max_id / since_id) to method
- If you supply only max_id and not since_id, you try to subtract 1 from since_id which is set to None
- The paging mechanism itself did not work as expected, now it does (both next and prev)
  • Loading branch information
AmirQAhired authored and default_git_name committed Feb 25, 2014
1 parent 3941489 commit 77bb0b6
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions tweepy/cursor.py
Expand Up @@ -83,40 +83,68 @@ class IdIterator(BaseIterator):

def __init__(self, method, args, kargs):
BaseIterator.__init__(self, method, args, kargs)
self.max_id = kargs.get('max_id')
self.since_id = kargs.get('since_id')

# remove these parameters from the kargs and save them separately if they were specified
self.max_id = long(kargs.pop('max_id')) if kargs.has_key('max_id') else None
self.since_id = long(kargs.pop('since_id')) if kargs.has_key('since_id') else None

# set the first max_id - the top of the tweet stack
self.next_max_id = self.max_id if self.max_id else None
self.prev_since_id = None
self.prev_max_ids = []

self.count = 0

def next(self):
"""Fetch a set of items with IDs less than current set."""
if self.limit and self.limit == self.count:
raise StopIteration

# max_id is inclusive so decrement by one
# to avoid requesting duplicate items.
max_id = self.since_id - 1 if self.max_id else None
data = self.method(max_id = max_id, *self.args, **self.kargs)
data = self.method(max_id = self.next_max_id, since_id = self.since_id, *self.args, **self.kargs)

# reached the end / since_id
if len(data) == 0:
raise StopIteration
self.max_id = data.max_id
self.since_id = data.since_id

self.prev_since_id = data.max_id
self.prev_max_ids.append(self.next_max_id)
# max_id is inclusive so decrement the since_id by one to avoid requesting duplicate items.
self.next_max_id = data.since_id - 1
self.count += 1

return data

def prev(self):
"""Fetch a set of items with IDs greater than current set."""
if self.limit and self.limit == self.count:
raise StopIteration

since_id = self.max_id
data = self.method(since_id = since_id, *self.args, **self.kargs)
# reached since_id
if self.max_id and self.prev_since_id >= self.max_id:
raise StopIteration

# get previous page's max_id
prev_max_id = None
if len(self.prev_max_ids) >= 2:
self.prev_max_ids.pop()
prev_max_id = self.prev_max_ids[-1]
elif len(self.prev_max_ids) == 1:
prev_max_id = self.prev_max_ids[0]

data = self.method(since_id = self.prev_since_id, max_id = prev_max_id, *self.args, **self.kargs)

# reached the end
if len(data) == 0:
raise StopIteration
self.max_id = data.max_id
self.since_id = data.since_id

# set next/previous ID's
self.next_max_id = data.since_id - 1
self.prev_since_id = data.max_id
self.count += 1

return data


class PageIterator(BaseIterator):

def __init__(self, method, args, kargs):
Expand Down

0 comments on commit 77bb0b6

Please sign in to comment.