BuzzFeedNews/2016-10-facebook-fact-check

API calls used to get data

Closed this issue · 2 comments

Dear BuzzFeedNews,

We found your post:
https://www.buzzfeed.com/craigsilverman/partisan-fb-pages-analysis?utm_term=.sjaa0bQa9#.pgl5aq35Z
extremely timely and illuminating for our research into mame evolution:
https://github.com/raazesh-sainudiin/scalable-data-science/tree/master/meme-evolution

Adding the codes for the facebook API calls would make this analysis much more complete and possible to scale-up or be part of course I would like to teach in the future.

Best,

Raaz

Thanks for your interest, Raaz! And good luck with your course.

To collect the engagement metrics, we used the FB Graph API's post endpoint, which is documented here. You can explore that endpoint here and can use that interface to generate corresponding code in several programming languages.

If you're familiar with Python, you can use this code, which I've lightly adapted, from our project, for broader use:

import requests
import time
ACCESS_TOKEN = "[your access token]"

def fetch_fb_engagement(account_id, post_id):
    url = "https://graph.facebook.com/v2.8/{0}_{1}/".format(account_id, post_id)
    params = {
        "fields": "comments.limit(0).summary(true),reactions.limit(0).summary(true),shares,created_time",
        "access_token": ACCESS_TOKEN
    }
    while True:
        res = requests.get(url, params=params)
        if res.status_code != 200: # Catch errors / rate-limiting
            sys.stderr.write("\nERR: {0} @ {1}_{2}\n".format(
                res.status_code,
                account_id,
                post_id
            ))
            if res.status_code == 400: # Cannot find post
                return {
                    "account_id": account_id,
                    "post_id": post_id,
                }
            time.sleep(10)
            continue
        data = res.json()
        return {
            "account_id": account_id,
            "post_id": post_id,
            "created_time": data.get("created_time"),
            "share_count": data.get("shares", {}).get("count"),
            "reaction_count": data.get("reactions", {}).get("summary", {}).get("total_count"),
            "comment_count": data.get("comments", {}).get("summary", {}).get("total_count"),
        }

To get engagement data for this NZ Herald post, you'd run:

# NZ Herald account ID is `34497296301`
print(fetch_fb_engagement(34497296301, 10153875941151302))

... and get:

{'account_id': 34497296301,
 'comment_count': 42,
 'created_time': '2016-10-24T17:13:30+0000',
 'post_id': 10153875941151302,
 'reaction_count': 269,
 'share_count': 33}