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

Commit f31f3a9

Browse files
author
Rock Howard
committed
getFriendIDs now uses GetFriendIDsPaged
1 parent 05b3aa7 commit f31f3a9

File tree

1 file changed

+80
-66
lines changed

1 file changed

+80
-66
lines changed

twitter/api.py

Lines changed: 80 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,65 +1512,6 @@ def GetFriends(self, user_id=None, screen_name=None, cursor=-1, count=None, skip
15121512

15131513
return result
15141514

1515-
def GetFriendIDs(self,
1516-
user_id=None,
1517-
screen_name=None,
1518-
cursor=-1,
1519-
stringify_ids=False,
1520-
count=5000):
1521-
"""Returns a list of twitter user id's for every person
1522-
the specified user is following.
1523-
1524-
Args:
1525-
user_id:
1526-
The id of the user to retrieve the id list for. [Optional]
1527-
screen_name:
1528-
The screen_name of the user to retrieve the id list for. [Optional]
1529-
cursor:
1530-
Specifies the Twitter API Cursor location to start at.
1531-
Note: there are pagination limits. [Optional]
1532-
stringify_ids:
1533-
if True then twitter will return the ids as strings instead of integers.
1534-
[Optional]
1535-
count:
1536-
The number of user id's to retrieve per API request. Please be aware that
1537-
this might get you rate-limited if set to a small number.
1538-
By default Twitter will retrieve 5000 UIDs per call. [Optional]
1539-
1540-
Returns:
1541-
A list of integers, one for each user id.
1542-
"""
1543-
url = '%s/friends/ids.json' % self.base_url
1544-
if not self.__auth:
1545-
raise TwitterError({'message': "twitter.Api instance must be authenticated"})
1546-
parameters = {}
1547-
if user_id is not None:
1548-
parameters['user_id'] = user_id
1549-
if screen_name is not None:
1550-
parameters['screen_name'] = screen_name
1551-
if stringify_ids:
1552-
parameters['stringify_ids'] = True
1553-
if count is not None:
1554-
parameters['count'] = count
1555-
result = []
1556-
1557-
while True:
1558-
parameters['cursor'] = cursor
1559-
json_data = self._RequestUrl(url, 'GET', data=parameters)
1560-
data = self._ParseAndCheckTwitter(json_data.content)
1561-
result += [x for x in data['ids']]
1562-
if 'next_cursor' in data:
1563-
if data['next_cursor'] == 0 or data['next_cursor'] == data['previous_cursor']:
1564-
break
1565-
else:
1566-
cursor = data['next_cursor']
1567-
else:
1568-
break
1569-
sec = self.GetSleepTime('/friends/ids')
1570-
time.sleep(sec)
1571-
1572-
return result
1573-
15741515
def _GetIDsPaged(self,
15751516
url, # must be the url for followers/ids.json or friends/ids.json
15761517
user_id,
@@ -1579,7 +1520,10 @@ def _GetIDsPaged(self,
15791520
stringify_ids,
15801521
count):
15811522
"""
1582-
See GetFollowerIDs for an explanation of the inpout arguments.
1523+
This is the lowlest level paging logic for fetching IDs. It is used soley by
1524+
GetFollowerIDsPaged and GetFriendIDsPaged. It is not intended for other use.
1525+
1526+
See GetFollowerIDsPaged or GetFriendIDsPaged for an explanation of the input arguments.
15831527
"""
15841528
# assert(url.endswith('followers/ids.json') or url.endswith('friends/ids.json'))
15851529
if not self.__auth:
@@ -1649,8 +1593,28 @@ def GetFriendIDsPaged(self,
16491593
cursor=-1,
16501594
stringify_ids=False,
16511595
count=5000):
1652-
"""
1653-
See GetFollowerIDs for an explanation of the inpout arguments.
1596+
"""Make a cursor driven call to return the list of all friends
1597+
1598+
The caller is responsible for handling the cursor value and looping
1599+
to gather all of the data
1600+
1601+
Args:
1602+
user_id:
1603+
The twitter id of the user whose friends you are fetching.
1604+
If not specified, defaults to the authenticated user. [Optional]
1605+
screen_name:
1606+
The twitter name of the user whose friends you are fetching.
1607+
If not specified, defaults to the authenticated user. [Optional]
1608+
cursor:
1609+
Should be set to -1 for the initial call and then is used to
1610+
control what result page Twitter returns.
1611+
count:
1612+
The number of user id's to retrieve per API request. Please be aware that
1613+
this might get you rate-limited if set to a small number.
1614+
By default Twitter will retrieve 5000 UIDs per call. [Optional]
1615+
1616+
Returns:
1617+
next_cursor, previous_cursor, data sequence of twitter.User instances, one for each friend
16541618
"""
16551619
url = '%s/friends/ids.json' % self.base_url
16561620
return self._GetIDsPaged(url, user_id, screen_name, cursor, stringify_ids, count)
@@ -1688,10 +1652,6 @@ def GetFollowerIDs(self,
16881652
Returns:
16891653
A list of integers, one for each user id.
16901654
"""
1691-
url = '%s/followers/ids.json' % self.base_url
1692-
if not self.__auth:
1693-
raise TwitterError({'message': "twitter.Api instance must be authenticated"})
1694-
16951655
result = []
16961656
if total_count and total_count < count:
16971657
count = total_count
@@ -1713,6 +1673,60 @@ def GetFollowerIDs(self,
17131673

17141674
return result
17151675

1676+
def GetFriendIDs(self,
1677+
user_id=None,
1678+
screen_name=None,
1679+
cursor=-1,
1680+
stringify_ids=False,
1681+
count=None,
1682+
total_count=None):
1683+
"""Returns a list of twitter user id's for every person
1684+
that is followed by the specified user.
1685+
1686+
Args:
1687+
user_id:
1688+
The id of the user to retrieve the id list for. [Optional]
1689+
screen_name:
1690+
The screen_name of the user to retrieve the id list for. [Optional]
1691+
cursor:
1692+
Specifies the Twitter API Cursor location to start at.
1693+
Note: there are pagination limits. [Optional]
1694+
stringify_ids:
1695+
if True then twitter will return the ids as strings instead of integers.
1696+
[Optional]
1697+
count:
1698+
The number of user id's to retrieve per API request. Please be aware that
1699+
this might get you rate-limited if set to a small number.
1700+
By default Twitter will retrieve 5000 UIDs per call. [Optional]
1701+
total_count:
1702+
The total amount of UIDs to retrieve. Good if the account has many followers
1703+
and you don't want to get rate limited. The data returned might contain more
1704+
UIDs if total_count is not a multiple of count (5000 by default). [Optional]
1705+
1706+
Returns:
1707+
A list of integers, one for each user id.
1708+
"""
1709+
result = []
1710+
if total_count and total_count < count:
1711+
count = total_count
1712+
1713+
while True:
1714+
next_cursor, previous_cursor, data = self.GetFriendIDsPaged(user_id, screen_name, cursor, stringify_ids,
1715+
count)
1716+
result += [x for x in data['ids']]
1717+
if next_cursor == 0 or next_cursor == previous_cursor:
1718+
break
1719+
else:
1720+
cursor = next_cursor
1721+
if total_count is not None:
1722+
total_count -= len(data['ids'])
1723+
if total_count < 1:
1724+
break
1725+
sec = self.GetSleepTime('/followers/ids')
1726+
time.sleep(sec)
1727+
1728+
return result
1729+
17161730
def GetFollowersPaged(self,
17171731
user_id=None,
17181732
screen_name=None,

0 commit comments

Comments
 (0)