下面看壹下getfriends.py的源碼:
[python] view plain copy print?
#! /usr/bin/python
import time
PAGE_SIZE = 200
def print_users_list(ul):
"""
打印用戶列表的詳細信息
"""
index = 0
for user in ul:
uid = user["id"]
ugen = user["gender"]
uname = user["screen_name"]
# uloc = user["location"]
udesc = user["description"]
print "%-6d%-12d%-3s%s%s" % (index, uid, ugen, uname.ljust(20), udesc.ljust(40))
index += 1
def get_friends(client, uid=None, maxlen=0):
"""
讀取uid用戶的關註用戶列表,默認uid=None,此時uid賦值為client.uid,而client.uid表示的是當前授權用戶的uid.
"""
if not uid:
uid = client.uid
return get_users(client, False, uid, maxlen)
def get_followers(client, uid=None, maxlen=0):
"""
讀取uid用戶的粉絲列表,默認uid=None,此時uid賦值為client.uid,而client.uid表示的是當前授權用戶的uid.
"""
if not uid:
uid = client.uid
return get_users(client, True, uid, maxlen)
def get_users(client, followersorfriends, uid, maxlen):
"""
調用API讀取uid用戶的關註用戶列表或者粉絲列表,followersorfriends為True讀取粉絲列表,為False讀取關註好友列表,
參數maxlen設置要獲取的好友列表的最大長度,為0表示沒有設置最大長度,此時會嘗試讀取整個好友列表,但是API對於讀取的
好友列表的長度會有限制,測試等級最大只能獲取壹個用戶的5000條好友信息。
"""
fl = []
next_cursor = 0
while True:
if followersorfriends:
raw_fl = client.friendships.followers.get(uid=uid, cursor=next_cursor, count=PAGE_SIZE)
else:
raw_fl = client.friendships.friends.get(uid=uid, cursor=next_cursor, count=PAGE_SIZE)
fl.extend(raw_fl["users"])
next_cursor = raw_fl["next_cursor"]
if not next_cursor:
break
if maxlen and len(fl) >= maxlen:
break
time.sleep(1)
return fl