문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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',[])
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top