Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.

Commit 05b3aa7

Browse files
author
Rock Howard
committed
Added GetFriendIDsPaged and refactored the API a bit
1 parent 9a9b27e commit 05b3aa7

File tree

1 file changed

+52
-27
lines changed

1 file changed

+52
-27
lines changed

twitter/api.py

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,46 @@ def GetFriendIDs(self,
15711571

15721572
return result
15731573

1574+
def _GetIDsPaged(self,
1575+
url, # must be the url for followers/ids.json or friends/ids.json
1576+
user_id,
1577+
screen_name,
1578+
cursor,
1579+
stringify_ids,
1580+
count):
1581+
"""
1582+
See GetFollowerIDs for an explanation of the inpout arguments.
1583+
"""
1584+
# assert(url.endswith('followers/ids.json') or url.endswith('friends/ids.json'))
1585+
if not self.__auth:
1586+
raise TwitterError({'message': "twitter.Api instance must be authenticated"})
1587+
parameters = {}
1588+
if user_id is not None:
1589+
parameters['user_id'] = user_id
1590+
if screen_name is not None:
1591+
parameters['screen_name'] = screen_name
1592+
if stringify_ids:
1593+
parameters['stringify_ids'] = True
1594+
if count is not None:
1595+
parameters['count'] = count
1596+
result = []
1597+
1598+
parameters['cursor'] = cursor
1599+
1600+
json = self._RequestUrl(url, 'GET', data=parameters)
1601+
data = self._ParseAndCheckTwitter(json.content)
1602+
1603+
if 'next_cursor' in data:
1604+
next_cursor = data['next_cursor']
1605+
else:
1606+
next_cursor = 0
1607+
if 'previous_cursor' in data:
1608+
previous_cursor = data['previous_cursor']
1609+
else:
1610+
previous_cursor = 0
1611+
1612+
return next_cursor, previous_cursor, data
1613+
15741614
def GetFollowerIDsPaged(self,
15751615
user_id=None,
15761616
screen_name=None,
@@ -1601,34 +1641,19 @@ def GetFollowerIDsPaged(self,
16011641
next_cursor, previous_cursor, data sequence of twitter.User instances, one for each follower
16021642
"""
16031643
url = '%s/followers/ids.json' % self.base_url
1604-
if not self.__auth:
1605-
raise TwitterError({'message': "twitter.Api instance must be authenticated"})
1606-
parameters = {}
1607-
if user_id is not None:
1608-
parameters['user_id'] = user_id
1609-
if screen_name is not None:
1610-
parameters['screen_name'] = screen_name
1611-
if stringify_ids:
1612-
parameters['stringify_ids'] = True
1613-
if count is not None:
1614-
parameters['count'] = count
1615-
result = []
1616-
1617-
parameters['cursor'] = cursor
1618-
1619-
json = self._RequestUrl(url, 'GET', data=parameters)
1620-
data = self._ParseAndCheckTwitter(json.content)
1644+
return self._GetIDsPaged(url, user_id, screen_name, cursor, stringify_ids, count)
16211645

1622-
if 'next_cursor' in data:
1623-
next_cursor = data['next_cursor']
1624-
else:
1625-
next_cursor = 0
1626-
if 'previous_cursor' in data:
1627-
previous_cursor = data['previous_cursor']
1628-
else:
1629-
previous_cursor = 0
1630-
1631-
return next_cursor, previous_cursor, data
1646+
def GetFriendIDsPaged(self,
1647+
user_id=None,
1648+
screen_name=None,
1649+
cursor=-1,
1650+
stringify_ids=False,
1651+
count=5000):
1652+
"""
1653+
See GetFollowerIDs for an explanation of the inpout arguments.
1654+
"""
1655+
url = '%s/friends/ids.json' % self.base_url
1656+
return self._GetIDsPaged(url, user_id, screen_name, cursor, stringify_ids, count)
16321657

16331658
def GetFollowerIDs(self,
16341659
user_id=None,

0 commit comments

Comments
 (0)