Made of Everything You're Not

If you're a stalker I'd prefer if you didn't kill me. Thanks.
  • Home
  • Projects
  • Portfolio
  • Resume

Posts Tagged ‘friendfeed’

Using the FriendFeed API with PHP

Posted in Code, Programming on March 18th, 2009 by Eric Lamb – Be the first to comment

Dissenting opinion: I don’t like social networking as a consumer. I just find the whole thing pretty stupid; meeting people online that I’ll never meet in real life, who I have only a vague similarity to,  makes me feel retarded. Facebook, MySpace, Twitter can all go to hell. Sorry, had to be said. (I’ll probably be writing a post on this soon.)

FriendFeed Logo

FriendFeed Logo

I do love building them though. They’re a joy to work on because of the complexities involved and right now it’s prettty exciting because of all the new stuff coming out daily.

Anyway, for an upcoming project I needed to integrate the FriendFeed API into a website. This was painful because, as I said above, I hate social networking sites. I didn’t belong to too any at the time so I had to spend about an hour signing up with a few of the  social networks FriendFeed supports.

After that though, working with the API was pretty painless. To get started all you have to do is

  1. Sign up with FriendFeed
  2. Import your sites
  3. Get API Key
  4. Download php API code

Try not to look too closely to the API code; there’s almost zero formatting. Just know it works. If you have php compiled with curl support that is; you HAVE to have curl support.

API Calls

Pull a user feed:

<?php
$friendfeed = new FriendFeed($friendfeed_nickname, $friendfeed_key);
$feed = $friendfeed->fetch_user_feed("$nickname", $service=null, $start=0,$num=30);
?>

Pull a user comment feed:

<?php
$friendfeed = new FriendFeed($friendfeed_nickname, $friendfeed_key);
$feed = $friendfeed->fetch_user_comments_feed($nickname, $service=null, $start=0,$num=30);
?>

Pull a user likes feed:

<?php
$friendfeed = new FriendFeed($friendfeed_nickname, $friendfeed_key);
$feed = $friendfeed->fetch_user_likes_feed($nickname, $service=null, $start=0,$num=30);
?>

Pull a user discussion feed:

<?php
$friendfeed = new FriendFeed($friendfeed_nickname, $friendfeed_key);
$feed = $friendfeed->fetch_user_discussion_feed($nickname, $service=null, $start=0,$num=30);
?>

Pull a merged feed with all of the given users’ entries. Note that authentication is required if any one of the users’ feeds isn’t public and that $nicknames is an array of users:

<?php
$feed = $friendfeed->fetch_multi_user_feed($nicknames, $service=null, $start=0,$num=30);
?>

Pull the entries the authenticated user sees on their home page:

<?php
$friendfeed = new FriendFeed($friendfeed_nickname, $friendfeed_key);
$feed = $friendfeed->fetch_home_feed($service=null, $start=0, $num=30);
?>

Perform a search. The format for $query is the same as the FriendFeed search. If you authenticate the request the search applies to the authenticated user. If it’s not authenticated the search is against all public feeds.

<?php
$friendfeed = new FriendFeed($friendfeed_nickname, $friendfeed_key);
$feed = $friendfeed->search($query, $service=null, $start=0, $num=30);
?>

Publishes the provided link/title to the authenticated users page:

<?php
// image_urls is a list of URLs that will be downloaded and included as
// thumbnails beneath the link. The thumbnails will all link to the
// destination link. If you would prefer that the images link somewhere
// else, you can specify images instead, which should be an array of
// name-associated arrays of the form array("url"=>...,"link"=>...).
// The thumbnail with the given url will link to the specified link.
//
// audio_urls is a list of MP3 URLs that will show up as a play
// button beneath the link. You can optionally supply audio[]
// instead, which should be a list of name-associated arrays of the 
// form ("url"=> ..., "title"=> ...). The given title will appear when
// the audio file is played.
//
// We return the parsed/published entry as returned from the server,
// which includes the final thumbnail URLs as well as the ID for the
$friendfeed = new FriendFeed($friendfeed_nickname, $friendfeed_key);
$feed = $friendfeed->publish_link($title, $link, $comment=null, $image_urls=null,$images=null, $via=null, $audio_urls=null, $audio=null, $room=null);
?>

Add a comment to a users page; returns the comment_id

<?php
$friendfeed = new FriendFeed($friendfeed_nickname, $friendfeed_key);
$feed = $friendfeed->add_comment($entry_id, $body);
?>

Edit a comment on a users page:

<?php
$friendfeed = new FriendFeed($friendfeed_nickname, $friendfeed_key);
$feed = $friendfeed->edit_comment($entry_id, $comment_id, $body);
?>

Delete a comment:

<?php
$friendfeed = new FriendFeed($friendfeed_nickname, $friendfeed_key);
$feed = $friendfeed->delete_comment($entry_id, $comment_id);
?>

Un-deletes a comment:

<?php
$friendfeed = new FriendFeed($friendfeed_nickname, $friendfeed_key);
$feed = $friendfeed->undelete_comment($entry_id, $comment_id);
?>

Add like for a given $entry_id:

<?php
$friendfeed = new FriendFeed($friendfeed_nickname, $friendfeed_key);
$feed = $friendfeed->add_like($entry_id);
?>

Deletes a like for a give $entry_id:

<?php
$friendfeed = new FriendFeed($friendfeed_nickname, $friendfeed_key);
$feed = $friendfeed->delete_like($entry_id);
?>

Further Reading

Getting started with the FriendFeed API and PHP
FriendFeed API Documentation

Bookmark and Share

Shifting Requirements = Better Coder

Posted in Code, Programming on March 10th, 2009 by Eric Lamb – Be the first to comment

So often in the development process shifting requirements can wreak havoc on the entire project. But sometimes, every once in a while, if the moon is in just the right place and the wind is blowing in the proper direction, you can walk away a better coder for it.

Rhino

Rhino

Take for example my recent experience integrating the FriendFeed API into a client’s site.

Pop Quiz

The client has a website that utilizes the FriendFeed API, on the homepage only, to display information about their online activities. Basically, they want the site to be updated automatically whenever they update their MySpace or YouTube pages and the like. They’ve specified the layout to be something like the below:

“ClientName” just published a piece titled “Title” on “Site”. Published “X hours ago”

Pretty simple right? An obvious design would go something like this:
1. Connect to FriendFeed API.
2. Make request for data.
3. Display on the page.

The only issue is the formatting of the date / time difference. No worries though; there’s php code all over the place for that:

function RelativeTime($timestamp){
	$difference = time() - $timestamp;
	$periods = array("sec", "min", "hour", "day", "week","month", "years", "decade");
	$lengths = array("60","60","24","7","4.35","12","10");
 
	if ($difference > 0) { // this was in the past
		$ending = "ago";
	} else { // this was in the future
		$difference = -$difference;
		$ending = "to go";
	}
 
	for($j = 0; $difference >= $lengths[$j]; $j++) {
		$difference /= $lengths[$j];
	}
 
	$difference = round($difference);
	if($difference != 1) {
		$periods[$j].= "s";
	}
 
	$text = "$difference $periods[$j] $ending";
	return $text;
}

Using the above you can just execute the output like the below:

echo $me.' just published a piece titled '.$post_title.' on '.$site;
echo ' Published '.RelativeTime($relative_time);

Feeling pretty proud of yourself you show it off to the Producer in charge of the program and they come back with, “Nice! But how will it hold up after caching is enabled?”.

Well, I hadn’t thought of that (my bad fully)…

The Oh Shit Moment

Turns out, they’re expecting a lot of traffic (or the website is pretty resource intensive; take your pick) so full page caching is going to be required.

The problem is the timestamp; since all output is going to be cached the generated time, “5 minutes ago” or “2 days ago”, etc, isn’t going to change until the cache expires. This is not good…

How I Fixed This (The Wrong Way)

The first thing I did was make the conclusion that if php couldn’t be used then the job of formatting the string is up to JavaScript. “OK”, I thought, “fine, I’ll just port the php function to JavaScript and go about my day.

Here’s what I came up with:

 
function getUnixTimestamp(timeObj) {
	var dateObj = null;
	if (timeObj != null) {
		dateObj = new Date(timeObj);
	} else {
		dateObj = new Date();
	}
	return (parseInt(dateObj.getTime().toString().substring(0, 10)));
}
 
function RelativeTime(timestamp){
 
	difference = getUnixTimestamp() - timestamp;
	periodArr = new Array();
	periods = "sec,min,hour,day,week,month,years,decade";
	periodArr = periods.split(',');
 
	TimeLengths = new Array()
	timelength = "60,60,24,7,4.35,12,10";
	TimeLengths = timelength.split(',');
 
	if (difference > 0) { // this was in the past
		ending = "ago";
	} else { // this was in the future
		difference = - difference;
		ending = "to go";
	}
 
	for(j = 0; difference >= TimeLengths[j]; j++) {
		difference /= TimeLengths[j];
	}
 
	difference = Math.round(difference);
	if(difference != 1) {
		periodArr[j] = periodArr[j]+"s";
	}
	text = difference+' '+periodArr[j]+' '+ending;
	document.write(text);
}

Feeling sure of myself, again, a thought occurs to me that this still doesn’t fix the issue 100%. Sure, the date format will be consistent with the relative requirement, but the feed won’t be updated in “Real Time” so the full requirement isn’t there.

The Real Solution

A better idea would be to use AJAX to populate the area of the site where the blurb will be displayed. Since the AJAX request is independent of the rest of the site caching won’t be an issue. Sure, it’s a little more complicated but by doing an AJAX call the client gets exactly what they want.

The Lesson

Now, I’m not going to go into detail about the AJAX solution, mostly because I haven’t written it yet :) , but, to me, this is the far superior solution.

Admittedly, I was a little frustrated that I didn’t think of the AJAX solution sooner. That is, until I realized how much I had learned going in the “wrong” direction.

I had no idea that generating a UNIX timestamp in JavaScript was “complicated”.
I learned a lot about JavaScript string handling.
I got a quick primer in how JavaScript’s builtin Split function works.
I got a little humility out of the process too.

Sometimes, it’s good to go astray on a project.

Bookmark and Share
  • Subscribe: Entries | Comments
  • About Me

    Email Email
    Twitter Twitter
    310.739.3322
  • Categories

    • Brain Dump
    • Business
    • Code
    • IT
    • Programming
    • Rant
    • Servers
  • Archives

    • October 2011
    • August 2011
    • July 2011
    • June 2011
    • May 2011
    • April 2011
    • March 2011
    • February 2011
    • January 2011
    • December 2010
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008

Copyright © 2008 - 2012 Eric Lamb - All rights reserved