Syncing Comments with Facebook

I’m posting this here in case anyone else wants to pick it up and run with it. I wrote a piece of code to sync my facebook notes and blog post comments. All of my blog posts already are added to facebook via RSS feed, but I was ending up with essentially two conversations. First install Services_Facebook from the PEAR repository. Then you’ll need to create a new private application with a Facebook developer account. Set the “Connect URL” to point at this script. Then uncomment the commented out section. Visit the page and you’ll be redirected to facebook. Authorize the app, and then take the session key that is returned when your are redirected and insert it into the appropriate place in the script. Comment out the code again.

You’ll need to replace all references to “blog/Blog.php”, “blog/Post.php”, and “blog/Comment.php” as those are custom to this site, but it should be fairly easy to replace them with something like a wrapper for blogger or wordpress.

I run this as a cron job on my server every 20 minutes.

require_once ‘Services/Facebook.php’; require_once ‘blog/Blog.php’; require_once ‘blog/Post.php’; require_once ‘blog/Comment.php’; ini_set(“display_errors”,“1”);

class FacebookUtils { private static $instance;

public function getInstance() {
    Services\_Facebook::$apiKey = '<\>';
    Services\_Facebook::$secret = '<\>';
    if(!FacebookUtils::$instance) {
        FacebookUtils::$instance = new FacebookUtils();
    }
    return FacebookUtils::$instance;
}

public function getApi() {
    $api = new Services\_Facebook();
    $api->sessionKey = '<\>';
    return $api;
}

public function getNoteId($user, $title) {
    $title = addslashes($title);
    $fql = "SELECT note\_id
              FROM note
             WHERE uid = $user
               AND title = '$title'";

    $result = $this->getApi()->fql->query($fql);

    $notes = array();
    foreach ($result->note as $note) {
        return $note->note\_id;
    }
}
public function getUserName($uid) {
    $fql = "SELECT first\_name, pic\_small
             FROM user
             WHERE uid = $uid";

    $result = $this->getApi()->fql->query($fql);
    foreach($result->user as $user) {
        return array(
            "first\_name" => "$user->first\_name",
            "pic" => "$user->pic\_small",
        );
    }

}
public function getComments($id) {
    $fql = "SELECT post\_id, fromid, time, text, id
              FROM comment
             WHERE object\_id = $id
             ORDER by time desc";

    $result = $this->getApi()->fql->query($fql);
    $comments = array();
    foreach($result->comment as $comment) {
        $comments\[\] = array(
            "id" => "$comment->id",
            "text" => "$comment->text",
            "time" => "$comment->time",
            "from" => $this->getUserName($comment->fromid),
            "post\_id" => "$comment->post\_id",
        );
    }
    return $comments;
}

}

$api = FacebookUtils::getInstance();
/\*
if(empty($\_REQUEST\["session\_key"\])) {
    header("Location: http://www.facebook.com/login.php?api\_key=" 
        . Services\_Facebook::$apiKey 
        . "&connect\_display=popup&v=1.0"
        . "&next=http://www.facebook.com/connect/login\_success.html"
        . "&cancel\_url=http://www.facebook.com/connect/login\_failure.html"
        . "&fbconnect=true&return\_session=true"
        . "&session\_key\_only=true"
        . "&req\_perms=read\_stream,publish\_stream,offline\_access");
}
else {
    print $\_REQUEST\["session\_key"\];
}
exit;
\*/
$blog = new Blog("NDLKM2VHYWYTZDZHMI1MYTG1LTHKN2YTZWU5NGZLNMI4ZGU0");
$posts = Post::getPosts('OWNER\_UID',Array($blog->UID), 
    " and visible = 'True' order by DATE desc limit 0, 5 ");

$map = array();
foreach($posts as $post) {
    $map\[$post->UID\] = $api->getNoteId("675098370", $post->TITLE);
}

foreach($map as $postUID => $noteID) {
    $localComments = Comment::getComments('POST\_UID',Array($postUID));
    $fbComments = $api->getComments($noteID);

    $addLocal = array();
    $addRemote = array();
    foreach($fbComments as $fbComment) {
        $foundComment = false;
        foreach($localComments as $localComment) {
            $fbText = preg\_replace("/^.\* on Tim's blog says.../", "", $fbComment\["text"\]);
            if($fbText == $localComment->COMMENT) {
                $foundComment = true;
            }
        }
        if(!$foundComment) {
            $addLocal\[$fbComment\["id"\]\] = $fbComment;
        }
    }
    foreach($localComments as $localComment) {
       $foundComment = false;
        foreach($fbComments as $fbComment) {
            $fbText = preg\_replace("/^.\* on Tim's blog says.../", "", $fbComment\["text"\]);
            if($fbText == $localComment->COMMENT) {
                $foundComment = true;
            }
        }
        if(!$foundComment) {
            $addRemote\[$localComment->UID\] = $localComment;
        }
    }
    print "Post " . $postUID . "n";
    print "Add locally:n";
    print\_r($addLocal);
    print "Add remote: n";
    print\_r($addRemote);
    foreach($addLocal as $fbComment) {
        $c = new Comment();
        $c->set('POST\_UID',$postUID);
        $c->set('NAME', $fbComment\["from"\]\["first\_name"\]);
        $c->set('ICON', $fbComment\["from"\]\["pic"\]);
        $c->set('TITLE', $fbComment\["from"\]\["first\_name"\] . " on Facebook says...");
        $c->set('COMMENT', $fbComment\["text"\]);
        #$c->set('EMAIL', $this->director->getPostParam('commentemail'));
        #$c->set('URL', $this->director->getPostParam('commenturl'));
        #$c->set('NOTIFY', $this->director->getPostParam('commentnotify'));
        $c->set('DATE', date('Y-m-d H:i:s', $fbComment\["time"\]));
        #$c->set('IP',$this->director->getServerParam('REMOTE\_ADDR'));
        $c->update();
    }

    foreach($addRemote as $comment) {
        $result = $api->getApi()->users->callMethod('comments.add', array(
            'session\_key' => $api->getApi()->sessionKey,
            'object\_id' => $noteID,
            'text' => $comment->NAME . ' on Tim's blog says...' . $comment->COMMENT,
        ));
    }
}

Comments

tim brockman (http://timbrockman.com)

2010-04-13T09:07:54.000Z

This is a nice piece of code. I’ve been looking at a few Wordpress plugs recently and like this option. I like the PEAR but, the dependency may make it difficult for general public plugin maybe some good ol’ cURL… probably just more headache. Anyway nice code.

Tim (http://www.loadedguntheory.com/blog/index.php/listblog/.html)

2010-04-13T09:13:26.000Z

That’s a good point. I’ll try to do an update. There’s really no reason for the dependencies, and I’ve had to make a few changes to the way it verifies posts on each end.

Anonymous (http://www.loadedguntheory.com/blog/index.php/listblog/.html)

2011-10-29T01:09:41.000Z

ugg boots sale uk - ugg boots - http://www.cheapboots-online.net uggs for cheap - Incuctneace - http://www.fashionbootsonsale.net - cheap ugg boots - Incuctneace - http://www.fashionbootsonsale.net ugg boots on sale - Incuctneace - http://www.auwarmboots.com - cheap ugg boots - Incuctneace - http://www.auwarmboots.com discount ugg boots - Incuctneace - http://www.cozywinterboots.com - ugg boots on sale - Incuctneace - http://www.cozywinterboots.com

Loaded Gun Theory is a sponsored project of Austin Creative Alliance.

For more information on Austin performing arts visit Now Playing Austin.