03-获取 TargetUser 的 Followings 列表

03-获取 TargetUser 的 Followings 列表

郑昀 201005  隶属于《02.技术预研》小节

【注:去年的旧文。上一篇是《02-在 Kuber SocialBadge 基础上再前进一步》和《01-学习 Kuber 的 SocialBadge 好榜样 | 02.技术预研 | Social》,下一篇是《04-WebFinger的利用 | 02.技术预研 | Social》】

www.google.com/profiles/jason5ng32 的链接开始吧。

一、通过 TargetUser 的输入连接获取他的其他链接

测试代码:

import socialgraph
q="www.google.com/profiles/jason5ng32"
its_me = u'me'
types = u'types'
instance = socialgraph.Api()

results = instance.lookup(q)

attributes = [k for k in results['nodes'].iteritems()][0][1]['attributes']
nodes_referenced = [k for k in results['nodes'].iteritems()][0][1]['nodes_referenced']

# 此人姓名:
myname = attributes['fn'])
# 此人其他链接:
links_of_me = ['http://'+q,]#把起源链接也加进去
for link in nodes_referenced.iterkeys():
    if(its_me in nodes_referenced[link][types]):
        #print link
        links_of_me.append(link)
all_nodes_of_me = ','.join(links_of_me)

不过,为了确保我们主要精力放在那些常用的社会化媒体上,还要过滤一下这些链接。

 

二、把 TargetUser 的所有链接提交给 SocialGraph 获取 Followings

在提交前,为了确保我们主要精力放在那些常用的社会化媒体上,还要过滤一下这些链接。只保留 Twitter 、Google Reader Shared Items、Google Profile、Delicious 、豆瓣这几种链接。

获取 followings 的 http 请求类似于:

http://socialgraph.apis.google.com/lookup?q=http%3A%2F%2Fwww.google.com%2Fprofiles%2Fzhengyun%2Chttp%3A%2F%2Fwww.google.com%2Freader%2Fshared%2F15221435823542888940&fme=1&pretty=1&sgn=0&edi=1&edo=1&jme=1&pretty=1

测试代码:

import re

"""
import httplib2
import socks
h = httplib2.Http(proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, 'localhost', 1984))
"""
import socialgraph
#q="www.google.com/profiles/jason5ng32"
#q="twitter.com/fenng"
#q="www.google.com/profiles/electronixtar"
q="www.google.com/profiles/zhengyun"
its_me = u'me'
its_contact = u'contact'
types = u'types'
patternSupportServices = re.compile(u'(douban\.|twitter\.|google\.com\/reader\/shared|delicious\.|google\.com\/profile\/)',re.IGNORECASE)

instance = socialgraph.Api()
#instance = socialgraph.Api(httplib2_inst=h)

results = instance.lookup(q)

from print_r import print_r
attributes = [k for k in results['nodes'].iteritems()][0][1]['attributes']
nodes_referenced = [k for k in results['nodes'].iteritems()][0][1]['nodes_referenced']

# 此人姓名:
myname = attributes['fn']
# 此人其他链接:
support_links_of_me = ['http://'+q,]
for link in nodes_referenced.iterkeys():
    if(its_me in nodes_referenced[link][types]):
        if(len(patternSupportServices.findall(link))>0):
            support_links_of_me.append(link)

# 过滤后的此人的链接,以逗号分隔
support_nodes_of_me = ','.join(support_links_of_me)
print support_nodes_of_me
# 请求 Followings :
results = instance.lookup(support_nodes_of_me,edo=1,edi=0,fme=1,jme=0)
myFollowings = []
for node in results['nodes'].iteritems():
    nodes_referenced = node[1]['nodes_referenced']
    for fo in nodes_referenced.iterkeys():
        if(its_contact in nodes_referenced[fo][types]):
            if(len(patternSupportServices.findall(fo))>0):
                myFollowings.append(fo)

print myFollowings
print_r(instance._last_request['res']['content-location'])

 

注:

这里有一个问题:

当想获取 Google Reader 里关注的人时,有一个选项可能阻碍获取。

估计必须该人在 google profile 里专门为“在我的个人资料上显示我正在关注的人和正在关注我的人的名单” 打上勾,才能够使得 google social graph 显示该人的关注列表。待确认。
默认“在我的个人资料上显示我正在关注的人和正在关注我的人的名单” 是打开的,当初 google buzz 出世时大家还争论过这个默认选项侵犯隐私。

 

附录A:

http://pypi.python.org/pypi/socialgraph/

是Python wrapper for Google's Social Graph API。

注意1:它使用了 httplib2 库,需要预先安装。

注意2:它使用了 cjson 库,需要用 easy_install python-cjson 安装。但在 Windows 上安装之前,需要先确保你安装了 MingGW,然后阅读 http://python.cx.hu/python-cjson/#win32 ,就是试图用 MingGW 作为编译器。

如果你安装 cjson 失败,那么就需要修改 socialgraph.py 的源代码了,把

import cjson

替换为:

# We require a JSON parsing library. These seem to be the most popular.
try:
    import simplejson
    parse_json_func = lambda s: simplejson.loads(s.decode("utf-8"))
except ImportError:
    try:
        import cjson
        parse_json_func = lambda s: cjson.decode(s.decode("utf-8"), True)
    except ImportError:
        import json
        parse_json_func = lambda s: _unicodify(json.read(s))

也就是优先采用 simplejson 解析 json。

然后把两处

cjson.decode(content)

都替换为

parse_json_func(content)

注意3:

为了更好地控制 HTTP 请求,我修改了 socialgraph.py 的代码:传入了 httplib2 的实例。这样可以用代理;lookup 方法增加了一个 jme 的参数。

posted @ 2011-03-07 22:06  旁观者  阅读(2119)  评论(0编辑  收藏  举报