I wanted to backup my FriendFeed (FF), because... well, because it backs up a lot of my social activities. So this is the first attempt. I will use MongoDB because it works naturally with JSON, which suits my needs just fine. I will also use PHP.
What follows is dead simple. First a crude wrapper for accessing FF.
class FriendFeed {
private $user;
function __construct($user){
$this->user=$user;
}
/**
* This function constructs the FF url which reads the user's feed.
* It reads $num messages starting from the $start message.
*/
private function getUrl($start, $num){
return "http://friendfeed-api.com/v2/feed/".$this->user."?start=$start&num=$num";
}
/**
* Reads $num messages starting from the $start message.
* It seems like $num=100 is the maximum supported by FF.
*/
public function read($start=1, $num=100){
$json = file_get_contents($this->getUrl($start,$num));
return self::conv_obj(json_decode($json));
}
/**
* This function converts the FF json to array.
*/
public static function conv_obj($data){
if(!is_object($data) && !is_array($data)) return $data;
if(is_object($data)) $data = get_object_vars($data);
return array_map(array('FriendFeed','conv_obj'), $data);
}
}
This wrapper only reads the FF entries and decodes them to array. Now lets use it to read 100 entries and insert them in MongoDB.
//CONFIGURATION
$mondo_db_name = 'YOUR_MONGO_DB_NAME';
$mongo_db_collection = 'YOUR_MONGO_COLLECTION_NAME';
$ff_username = 'FriendFeed_USERNAME';
//Set up connection to Mongo
$m = new Mongo();
$collection = $m->selectDB( $mondo_db_name )->selectCollection( $mongo_db_collection );
$collection->ensureIndex( array( "id" => 1 ) );
//Read data from FF
$ff = new FriendFeed($ff_username);
$data = $ff->read();
//Process the FF data, updating Mongo with each item
if (!empty($data))
foreach ($data['entries'] as $entry){
//output new items
if (!$collection->findOne(array("id" => $entry['id'])))
echo $entry['date'].":".$entry['body']."\n";
//upsert the item in mongo
$collection->update(array("id" => $entry['id']), $entry, true);
}
//Output number of items in collection
$collcnt = $collection->count();
echo $collcnt."\n";
Make this run once per hour or once per day and you will have a local copy of your FF stream. Of course, this will not download photos and files. I may add an extension for this in the future, in case I need it.
If you need to cycle through all your local FF items:
$cursor = $collection->find();
$cursor = $cursor->sort( array( "date" => 1 ) );
foreach ($cursor as $id => $value) {
var_dump( $value );
}
If you need to backup all your items, you need to replace
$data = $ff->read(); and the code that follows with something like this:
while ($data = $ff->read($start)) {
//processing code
$start+=100;
}
This code (without the while loop) has been tested and works both on Linux and Windows, with PHP 5.3 and MongoDB 0.9.7.