Question

I am trying to follow some user in a list with python-twitter library. But I am taking "You've already requested to follow username" error for some users. That means I have sent a following request to that user so I cant do that again. So how can I control users, I sent following request. Or is there other way to control it.

for userID in UserIDs:
    api.CreateFriendship(userID)

EDIT: I am summerizing: You can follow some users when you want. But some ones don't let it. Firstly you must send friendship request, then he/she might accept it or not. What I want to learn is, how I can list requested users.

Was it helpful?

Solution

You have two options here:

  • call GetFriends before the loop:

    users = [u.id for u in api.GetFriends()]
    for userID in UserIDs:
        if userID not in users:
            api.CreateFriendship(userID)
    
  • use try/except:

    for userID in UserIDs:
        try:
            api.CreateFriendship(userID)
        except TwitterError:
            continue
    

Hope that helps.

OTHER TIPS

It has been close to three years since this question was asked but responding for reference as it shows up as the top hit when you Google the issue.

As of this post, this is still the case with python-twitter (i.e. there is no direct way with python-twitter to identify pending friendship or follower requests).

That said, one can extend the API class to achieve it. An example is available here: https://github.com/itemir/twitter_cli

Relevant snippet:

class ExtendedApi(twitter.Api):
    '''
    Current version of python-twitter does not support retrieving pending
    Friends and Followers. This extension adds support for those.
    '''
    def GetPendingFriendIDs(self):
        url = '%s/friendships/outgoing.json' % self.base_url
        resp = self._RequestUrl(url, 'GET')
        data = self._ParseAndCheckTwitter(resp.content.decode('utf-8'))

    return data.get('ids',[]) 

    def GetPendingFollowerIDs(self):
        url = '%s/friendships/incoming.json' % self.base_url
        resp = self._RequestUrl(url, 'GET')
        data = self._ParseAndCheckTwitter(resp.content.decode('utf-8'))

        return data.get('ids',[])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top