#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created on 2015-1-7
@author: beyondzhou
@name: fetch_users_information.py
'''
# Fetch user information
def fetch_users_information():
# import
import json
from login import weibo_login
from users import get_users_profile
# Access to sina api
weibo_api = weibo_login()
# Get users information
user_info = get_users_profile(weibo_api, user_ids=[1669560933, 3286717353])
print json.dumps(user_info, indent=1)
if __name__ == '__main__':
fetch_users_information()
# Get user information (multi))
def get_users_profile(weibo_api, screen_names=None, user_ids=None):
from login import make_weibo_request
# Must have either screen_name or user_id (logical xor)
assert (screen_names != None) != (user_ids != None), \
"Must have screen_names or user_ids, but not both"
items_to_info = {}
items = screen_names or user_ids
while len(items) > 0:
items_str = ','.join([str(item) for item in items[:1]]) # can only get one record each time
items = items[1:]
if screen_names:
response = make_weibo_request(weibo_api.users.show.get, screen_name=items_str)
else: # user_ids
response = make_weibo_request(weibo_api.users.show.get, uid=items_str)
'''
can only get one record each time, so the follow code does not work
for user_info in response:
if screen_names:
items_to_info[user_info['screen_name']] = user_info
else: # user_ids
items_to_info[user_info['id']] = user_info
'''
user_info = response
if screen_names:
items_to_info[user_info['screen_name']] = user_info
else: # user_ids
items_to_info[user_info['id']] = user_info
return items_to_info
Result:
callback_url: https://api.weibo.com/oauth2/authorize?redirect_uri=http%3A//apps.weibo.com/guaguastd&response_type=code&client_id=2925245021
return_redirect_uri: http://weibo.com/login.php?url=http%3A%2F%2Fapps.weibo.com%2Fguaguastd%3Fcode%3D54d0d2dfbe167a3adf2d5c1d33d3423b
code: ['54d0d2dfbe167a3adf2d5c1d33d3423b']
{
"3286717353": {
"bi_followers_count": 0,
"domain": "",
"avatar_large": "http://tp2.sinaimg.cn/3286717353/180/0/1",
"verified_source": "",
"ptype": 0,
"block_word": 0,
"statuses_count": 5,
"id": 3286717353,
"verified_reason_url": "",
"city": "8",
"verified": false,
"credit_score": 80,
"block_app": 0,
"follow_me": false,
"verified_reason": "",
"followers_count": 1,
"location": "\u5317\u4eac \u6d77\u6dc0\u533a",
"verified_trade": "",
"mbtype": 0,
"verified_source_url": "",
"profile_url": "u/3286717353",
"status": {
"darwin_tags": [],
"reposts_count": 0,
"source_type": 1,
"favorited": false,
"text": "\u65b0\u6d6a\u5fae\u535a\u6570\u636e\u6316\u6398\u98df\u8c31 http://t.cn/RZyisHd",
"created_at": "Wed Dec 31 07:00:28 +0800 2014",
"truncated": false,
"visible": {
"type": 0,
"list_id": 0
},
"idstr": "3793735240326731",
"mid": "3793735240326731",
"source": "<a href=\"http://weibo.com/\" rel=\"nofollow\">\u5fae\u535a weibo.com</a>",
"attitudes_count": 0,
"pic_urls": [],
"in_reply_to_screen_name": "",
"in_reply_to_user_id": "",
"in_reply_to_status_id": "",
"comments_count": 0,
"geo": null,
"id": 3793735240326731,
"mlevel": 0
},
"avatar_hd": "http://tp2.sinaimg.cn/3286717353/180/0/1",
"star": 0,
"description": "",
"friends_count": 12,
"online_status": 0,
"mbrank": 0,
"idstr": "3286717353",
"profile_image_url": "http://tp2.sinaimg.cn/3286717353/50/0/1",
"allow_all_act_msg": false,
"allow_all_comment": true,
"geo_enabled": true,
"class": 1,
"screen_name": "beyondzhou8",
"lang": "zh-cn",
"weihao": "",
"remark": "",
"favourites_count": 5,
"name": "beyondzhou8",
"url": "",
"province": "11",
"created_at": "Tue Aug 20 00:41:09 +0800 2013",
"verified_type": -1,
"gender": "m",
"following": false,
"pagefriends_count": 0,
"urank": 0
},
"1669560933": {
"bi_followers_count": 0,
"domain": "",
"avatar_large": "http://tp2.sinaimg.cn/1669560933/180/0/1",
"verified_source": "",
"ptype": 0,
"statuses_count": 0,
"allow_all_comment": true,
"id": 1669560933,
"verified_reason_url": "",
"city": "1000",
"province": "32",
"credit_score": 80,
"block_app": 0,
"follow_me": false,
"verified_reason": "",
"followers_count": 0,
"location": "\u6c5f\u82cf",
"verified_trade": "",
"mbtype": 0,
"verified_source_url": "",
"profile_url": "u/1669560933",
"block_word": 0,
"avatar_hd": "http://tp2.sinaimg.cn/1669560933/180/0/1",
"star": 0,
"description": "",
"friends_count": 0,
"online_status": 0,
"mbrank": 0,
"idstr": "1669560933",
"profile_image_url": "http://tp2.sinaimg.cn/1669560933/50/0/1",
"allow_all_act_msg": false,
"verified": false,
"geo_enabled": true,
"class": 1,
"screen_name": "BEYONDZHOU",
"lang": "zh-cn",
"weihao": "",
"remark": "",
"favourites_count": 0,
"name": "BEYONDZHOU",
"url": "",
"gender": "m",
"created_at": "Sat Dec 12 22:10:40 +0800 2009",
"verified_type": -1,
"following": true,
"pagefriends_count": 0,
"urank": 0
}
}