How can I find follow requested users with python-twitter

社会主义新天地 提交于 2019-12-11 03:48:45

问题


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.


回答1:


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.




回答2:


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',[])


来源:https://stackoverflow.com/questions/17374427/how-can-i-find-follow-requested-users-with-python-twitter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!