Skip to content

Commit

Permalink
Handle Twitter API issue with duplicate pages for API.search_users
Browse files Browse the repository at this point in the history
Resolves #1465
  • Loading branch information
Harmon758 committed Feb 7, 2021
1 parent a7d1608 commit 3c1a397
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tweepy/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,29 @@ class PageIterator(BaseIterator):
def __init__(self, method, *args, **kwargs):
BaseIterator.__init__(self, method, *args, **kwargs)
self.current_page = 1
# Keep track of previous page of items to handle Twitter API issue with
# duplicate pages
# https://twittercommunity.com/t/odd-pagination-behavior-with-get-users-search/148502
# https://github.com/tweepy/tweepy/issues/1465
# https://github.com/tweepy/tweepy/issues/958
self.previous_items = []

def next(self):
if self.limit > 0:
if self.current_page > self.limit:
raise StopIteration

items = self.method(page=self.current_page, *self.args, **self.kwargs)

if len(items) == 0:
raise StopIteration

for item in items:
if item in self.previous_items:
raise StopIteration

self.current_page += 1
self.previous_items = items
return items

def prev(self):
Expand Down

0 comments on commit 3c1a397

Please sign in to comment.