显示Python/Django中朋友关注的Twitter粉丝

Bre*_*den 5 python django twitter

我正在构建我的应用程序的快速部分,该应用程序会查看用户的关注者,并突出显示用户关注的人(朋友)所遵循的部分.

我想知道两件事:

  1. 有没有更有效的方法来做到这一点?似乎这样会影响Twitter的API限制,因为我需要检查每个用户朋友的朋友.

  2. 这将创建一个包含朋友ID和他们关注的关注者的词典列表.相反,dict会更好地作为跟随者ID然后跟随他们的朋友.提示?

码:

# Get followers and friends
followers = api.GetFollowerIDs()['ids']
friends = api.GetFriendIDs()['ids']

# Create list of followers user is not following
followers_not_friends = set(followers).difference(friends)

# Create list of which of user's followers are followed by which friends
followers_that_friends_follow = []
for f in friends:
    ff = api.GetFriendIDs(f)['ids']
    users = followers_not_friends.intersection(ff)
    followers_that_friends_follow.append({'friend': f, 'users': users })
Run Code Online (Sandbox Code Playgroud)

DTi*_*ing 1

对于你问题的第二部分:

import collections

followers_that_friends_follow = collections.defaultdict(list)
for f in friends:
    ff = api.GetFriendsIDs(f)['ids']
    users = followers_not_friends.intersection(ff)
    for user in users:
        followers_that_friends_follow[user].append(f)
Run Code Online (Sandbox Code Playgroud)

这将产生一个字典:

keys = 关注用户、用户未关注以及用户好友关注的 ids 关注者。

值 = 关注者的好友 ID 列表,而用户未关注

例如,如果用户的关注者的 ID 为 23,并且用户的两个朋友(用户 16 和用户 28)关注用户 23,则使用密钥 23 应给出以下结果

>>> followers_that_friends_follow[23]
[16,28]
Run Code Online (Sandbox Code Playgroud)