获取facebook用户的“发布”对象总数

时间:2013-09-03 22:22:08

标签: python facebook facebook-graph-api

我正在尝试获取用户在Facebook上与其帐户关联的帖子对象的总数。使用Python,我可以像这样翻阅帖子:

graph = facebook.GraphAPI(token.token)

try:

    feed = graph.get_connections('me', 'feed')
    for item in feed['data']:
        celery_process_facebook_item.apply_async(args=[user_id, item, full_iteration])
    if full_iteration and feed['paging']['next']:
        next = feed['paging']['next']
        parsed = urlparse.urlparse(next)
        until = int(urlparse.parse_qs(parsed.query)['until'][0])
        celery_process_feed.apply_async(args=[user_id, provider, post_type, full_iteration, until])

不幸的是,这并没有告诉我他们的Feed中的帖子总数。有没有办法获得这些信息?我想向我的客户提供一个进度条,显示我们已经为他们处理了x%的商品,但我不知道如何。

2 个答案:

答案 0 :(得分:0)

由于返回的数据是分页的,因此无法直接获取帖子数。您将不得不采用间接方式,即首次获取Feed,检查下一个网址并从下一个网址获取Feed。继续做事情,直到下一个网址不存在。

答案 1 :(得分:0)

FWIW,这是我用来计算帖子的具体代码:

graph = facebook.GraphAPI(token.token)
connection_type = 'feed'
total_posts = 0
try:
    feed = graph.get_connections('me', connection_type, limit=1000)
    while 'paging' in feed and 'next' in feed['paging'] and feed['paging']['next']:
        total_posts += len(feed['data'])
        print 'celery_count_facebook_posts @ %s total_posts' % (total_posts,)
        nextUrl = feed['paging']['next']
        parsed = urlparse.urlparse(nextUrl)
        until = int(urlparse.parse_qs(parsed.query)['until'][0])
        feed = graph.get_connections('me', connection_type, limit=1000, until=until)
    total_posts += len(feed['data'])
    print 'celery_count_facebook_posts FINISHED @ %s total_posts' % (total_posts,)