0

我正在尝试获取用户与他或她在 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])

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

4

2 回答 2

0

由于返回的数据是分页的,因此无法直接获取帖子数。您将不得不求助于间接方式,即第一次获取提要,检查下一个 url 并从下一个 url 获取提要。继续做事,直到下一个 url 不存在。

于 2013-09-04T10:23:08.907 回答
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,)
于 2013-09-05T17:02:06.650 回答