Frantisek
Frantisek

Reputation: 7693

How can I count the total number of all likes and comments on every object on a facebook page?

I'd like to use any of the available facebook APIs, ideally FQL to get the total number of all likes and comments on all objects on a certain facebook page. Is this possible?

So for example:

If page X has 10 posts, every post has 10 likes and 20 comments on it, the total number I'm going for is: 10*10 + 20*10 = 300.

Additionally, the page might have other objects like photos, videos, which also may contain additional likes and comments, which should also increment the total value.

Upvotes: 0

Views: 2817

Answers (2)

Martin.
Martin.

Reputation: 10529

Matthew's solution is pretty fine, but lacks pagination, which is pretty important. I assume you use PHP, so I'll show you solution I used once before

Assuming you're using PHP SDK

include("fb.class.php");

ignore_user_abort(true);
set_time_limit(0);

$fb = new Facebook(array(
    "appId" => "2859321",
    "secret" => "b226a5sd46as5d4as6d513as",
));

if (!$fb->getUser()) {
    if (file_get_contents("accessToken")) {
        $fb->setAccessToken(file_get_contents("accessToken"));
    } else {
        die("We have no access token for adult only pages");
    }
}

$page_ids = array();
$page_ids[] = 40796308305; // cocacola

function processPage($data, $id, &$pageComments, &$pageLikes, $&lastTime, $fb) {
    foreach($data as $index => $post) {
        $post = (object)$post;
        $pageComments += $post->comments['count'];
        $pageLikes += $post->likes['count'];
        $lastTime = $post->created_time;
    }

    $result = ($fb->api("/fql?q=".urlencode("SELECT comments.count, created_time, likes.count FROM stream WHERE source_id = {$id} AND created_time < {$lastTime} LIMIT 10000")));
    echo "Proccessing ".count($data)." posts. Last post has time $lastTime\n";
    if ($result) {
        processPage($result["data"], $id, $pageComments, $pageLikes, $lastTime, $fb);
    }
}

try {
    $totalComments = 0;
    $totalLikes = 0;

    foreach($page_ids as $id) {
        $pageComments = 0;
        $pageLikes = 0;

        $result = ($fb->api("/fql?q=".urlencode("SELECT comments.count, likes.count FROM stream WHERE source_id = ".$id." LIMIT 10000")));
        if (!empty($result["data"])) {
            //paginate
            processPage($result["data"], $id, $pageComments, $pageLikes, $lastTime, $fb);
            echo "Proccessed page id $id\n";
        }


        updateDbPost($id, $pageLikes, $pageComments);
    }


} catch (Exception $e) {
    die("Something failed, it should not have passed here :: ".$e->getMessage());
}


function updateDbPost($id, $pageLikes, $pageComments) {
    if ($pageLikes > 0) {
        //insert to the database
    }
}

Upvotes: 1

Matthew Johnston
Matthew Johnston

Reputation: 4439

This is relatively simple, here's the FQL query you can run:

SELECT likes.count, comments.count FROM stream WHERE source_id = 20531316728

This example uses the Facebook Page, but you can see how it works, just substitute your own Page ID for that pages ID.

Upvotes: 2

Related Questions