@@ -1512,35 +1512,20 @@ 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]
1515+ def _GetIDsPaged (self ,
1516+ url , # must be the url for followers/ids.json or friends/ids.json
1517+ user_id ,
1518+ screen_name ,
1519+ cursor ,
1520+ stringify_ids ,
1521+ count ):
1522+ """
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.
15391525
1540- Returns:
1541- A list of integers, one for each user id.
1526+ See GetFollowerIDsPaged or GetFriendIDsPaged for an explanation of the input arguments.
15421527 """
1543- url = '%s/friends/ ids.json' % self . base_url
1528+ # assert(url.endswith('followers/ ids.json') or url.endswith('friends/ids.json'))
15441529 if not self .__auth :
15451530 raise TwitterError ({'message' : "twitter.Api instance must be authenticated" })
15461531 parameters = {}
@@ -1554,22 +1539,21 @@ def GetFriendIDs(self,
15541539 parameters ['count' ] = count
15551540 result = []
15561541
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 )
1542+ parameters ['cursor' ] = cursor
15711543
1572- return result
1544+ json = self ._RequestUrl (url , 'GET' , data = parameters )
1545+ data = self ._ParseAndCheckTwitter (json .content )
1546+
1547+ if 'next_cursor' in data :
1548+ next_cursor = data ['next_cursor' ]
1549+ else :
1550+ next_cursor = 0
1551+ if 'previous_cursor' in data :
1552+ previous_cursor = data ['previous_cursor' ]
1553+ else :
1554+ previous_cursor = 0
1555+
1556+ return next_cursor , previous_cursor , data
15731557
15741558 def GetFollowerIDsPaged (self ,
15751559 user_id = None ,
@@ -1601,34 +1585,39 @@ def GetFollowerIDsPaged(self,
16011585 next_cursor, previous_cursor, data sequence of twitter.User instances, one for each follower
16021586 """
16031587 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 )
1621-
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
1588+ return self ._GetIDsPaged (url , user_id , screen_name , cursor , stringify_ids , count )
16301589
1631- return next_cursor , previous_cursor , data
1590+ def GetFriendIDsPaged (self ,
1591+ user_id = None ,
1592+ screen_name = None ,
1593+ cursor = - 1 ,
1594+ stringify_ids = False ,
1595+ count = 5000 ):
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
1618+ """
1619+ url = '%s/friends/ids.json' % self .base_url
1620+ return self ._GetIDsPaged (url , user_id , screen_name , cursor , stringify_ids , count )
16321621
16331622 def GetFollowerIDs (self ,
16341623 user_id = None ,
@@ -1663,10 +1652,6 @@ def GetFollowerIDs(self,
16631652 Returns:
16641653 A list of integers, one for each user id.
16651654 """
1666- url = '%s/followers/ids.json' % self .base_url
1667- if not self .__auth :
1668- raise TwitterError ({'message' : "twitter.Api instance must be authenticated" })
1669-
16701655 result = []
16711656 if total_count and total_count < count :
16721657 count = total_count
@@ -1688,6 +1673,60 @@ def GetFollowerIDs(self,
16881673
16891674 return result
16901675
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+
16911730 def GetFollowersPaged (self ,
16921731 user_id = None ,
16931732 screen_name = None ,
0 commit comments