Banksy's caveman

Functional is a kind way to describe this . . . but it’s hard to argue with computers doing tedious work that was previously done by hand.

The goal here was to automate the collection of the comments on 300 or so posts from the CDC’s Facebook page so they could be analyzed. Ebola is in the mix which leads to some comments that may very well rival YouTube comments in terms of causing me sadness. We did end up with around 34,000 comments which would have been hand copied in the past. So some karma balance was achieved.

wTake all this with a grain or two of salt. It works but may very well be illegal in some states or do things in a way that would make real programmers weep. Consider my programming skills to be like a man trying to swat flies with a broomstick- lots of furious action, panting, cursing, and every so often I will kill a fly and celebrate wildly.1

The Facebook

This was my first experience trying to do anything with Facebook. The hardest part here for me was just getting the stupid access token needed to get at the JSON feeds. I eventually found the secretly named Facebook Login Example. Part of my issue was that I thought I could skip the user level login and just use the app id/secret stuff like I’d do in Instagram (which is the main API I’ve messed with previously). It may be that someone can do that but I failed to do it in lots of ways. I eventually bowed to my own limits and went with the login option.

To make this work, you set up that login file and in my case I entered by app id/secret stuff in there directly. I’m just running this locally on a virtual server so I don’t really care about exposing it any ways. If you put this somewhere, you’d want to think harder about this and there’s likely a proper way to do it that I missed.

<?php
session_start();

//wherever you stuck the FB SDK PHP stuff
require_once 'vendor/facebook/php-sdk-v4/src/Facebook/autoload.php';

//I may have overloaded here but if you wanted perfection, you are in the wrong place
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
 

$fb = new Facebook\Facebook([
  'app_id' => 'xxxYOURMAGICIDxxx',
  'app_secret' => 'xxxYOURSUPERSECRETSECRETxxx',
  'default_graph_version' => 'v2.2',
  ]);

$helper = $fb->getRedirectLoginHelper();

$permissions = ['email']; // Optional permissions
$loginUrl = $helper->getLoginUrl('http://192.168.33.10/fb/fb-callback.php', $permissions);//send them to the place where the stuff happens

echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';
;?>

So now, you’re logged in. FB is kind enough to make the landing page so I used that initially. It gave me a warm comfortable feeling to see that the token was actually generated.2 I did a quick manual test by tacking it on the end of a URL by hand and presto- I could get the comments for a particular post via JSON.

The URL I used for testing looks like https://graph.facebook.com/76625396025_10152630338806026/comments/?access_token=A_WHOLE_MESS_OF_NUMBERS_AND_LETTERS

Now that I knew I could get the JSON, it became a pretty close match to what I did a few weeks ago for Instagram. The one element I added here was the foreach element. I tied it in to the array of article IDs and that makes it loop through the entire set. I also through in a break in the loop to see if it’d kill the loop if there was no next option present. It may have worked. I’m not sure.

Feel free to wander around in the comments here.


//small sample of the array consisting of the article IDs
$articlearray = array(
10152558689186026,
10152558324256026,
10152558235996026,
10152558022806026,
10152557874411026);


foreach ($articlearray as &$articleid) {
$cdcLink = 'https://graph.facebook.com/76625396025_'.$articleid.'/comments/?access_token='.($accessToken->getvalue());
//echo '<a href="'.$cdcLink.'">link</a>';
$search ='ebola';
$json = file_get_contents($cdcLink);
$obj = json_decode($json);
date_default_timezone_set('EST');

			$list = array();
			foreach ($obj->data as $comment) {
			 	$username = $comment->from->name;
                $userid = $comment->from->id;
                $message = $comment->message;
                $created = $comment->created_time;
                $commentid = $comment->id;
                  
				array_push($list, $username . '?' . $userid . '?' . $message . '?' . $created . '?' . $commentid) ;
				}
			$file = fopen($search .'fbdata.csv',"a+");

			foreach ($list as $line)
				  {
					  fputcsv($file,explode('?',$line));
 				  }

			fclose($file); 

//starts the looping for comments above those initially listed

 for ($x = 0; $x <= 70; $x++) {
				$url = $cdcLink;	
				$next = $obj->paging->next;	
				$url = $next;
				
				 if ($next == FALSE) {
       			 break;    /* You could also write 'break 1;' here. */
    }
				
				$json = file_get_contents($url);	
				$obj = json_decode($json);
//				echo $next . '<br>'; - was for testing url returns 
				
				$list = array();
				foreach ($obj->data as $comment) {
			 	$username = $comment->from->name;
                $userid = $comment->from->id;
                $message = $comment->message;
                $created = $comment->created_time;
                $commentid = $comment->id;
                  
				array_push($list, $username . '?' . $userid . '?' . $message . '?' . $created . '?' . $commentid) ;
				}
			$file = fopen($search .'fbdata.csv',"a+");

			foreach ($list as $line)
				  {
					  fputcsv($file,explode('?',$line));
 				  }

			fclose($file); 			 

};
}


1 I am killing more flies, more frequently. So I have that going for me.

2 At least 90% of my programming is just making sure I achieved what I thought I did in each step- so there’s lots of echo/dump/console logging.

13 thoughts on “Grabbing FaceBook Comments – More Primitive Programming

    1. All this stuff is pretty new to me as well. I don’t really qualify as a programmer.

      I run it on a Scotch box install on my computer. That lets you avoid having to put it on a server somewhere.

      What exactly are you trying to do? I might be able to give some advice if I had some specifics. If you’re going to use the code above you will need a FB API key.

  1. sometimes i need to see full comments on some posts if i use your previous bookmarklet code i can expand not only one post but many if i scroll down is it possible within this php code to expand many posts at the same time ?

    can you help me through teamviewer i tried to instal scotch box but i failed

    1. The PHP actually downloads all of the comments and puts them in a CSV file.

      If you just want the plugin, send me a link to something on FB that I can see and I’ll see if I can make the bookmarklet work with it.

    1. This addresses the first issue. The second one would be a link to a new page rather than expanding anything so it’s a different thing entirely. I’m not sure about the third piece but I think that’s addressed as well.

  2. thanks tom first issue solved now . about second and third issue is it possible to make Facebook show posts in the same page without clicking on Continue reading and see more in any page i want to see full posts when i scroll down in any Facebook page or account

  3. 1.thanks tom if you type in facebook posts liked by my or by any of your friend you cant see any comment in any posts unless you click on comments on each posts is it possible to make Facebook show all comments in all posts ?

    2.about (continue reading ) so you mean its impossible to do it in any programming method ?

    3. this code not solved see more in posts it solved in comments not in posts i hope you can find solution for it

    4. in some posts with 1000 comments page will be Page unresponsive mikake edit his code maybe it will solve this issue please update
    your code

    http://mikakesensei.com/perso/javascript/fb_expander.js

    https://gist.github.com/woodwardtw/951fe0d14081f822ea9b

Leave a Reply