In part 1 of my series on social media integration using Drupal I covered Twitter and Flickr and in part 2 I covered Facebook. In this part, I will cover how we integrated Goodreads into our website. If you haven't used Goodreads.com before, it is a great website that integrates books and social networking. It allows you to rate books you've read, get updates on what your friends are reading, and post and read reviews on books. Several of the stafff members at Onyx maintain accounts at Goodreads and we decided to show the books we have read on our individual pages.
.png)
As you can see, we list up to 12 books that are randomly selected from each staff member's Goodreads page. Goodreads provides several "shelves" that users can assign books to. We display books from each staff member's "read" shelf. Luckily, Goodreads provides us with an RSS feed capable of showing information from that shelf. Let's take a look at a sample feed:
goodreads.com/review/list_rss/1015726-adrian?shelf=read&sort=random&format=json
You may notice that we need a goodreads ID in order to specify which user's RSS feed we want to grab. If we login to Goodreads and click the "read" shelf, we can see our user ID in the URL:
 (sorted by_ date added) Adrian has 130 books on his read shelf.jpg)
So essentially we can just copy the URL and add sort=random&format=json to the end of the URL. This will give us a randomly sorted feed in json format (which is easy to parse in PHP).
Before we dive into the code, there is a required module that you will need to install in order for this to work. The Remote File module allows you to grab a remote file and then copy it to your server. The reason I decided to do this is so that I could resize the images using imagecache. Unfortunately, as of writing this article, imagecache doesn't allow you to use images unless they are hosted on your server.
function getGoodReads($goodreadsId, $num=4){
if(($cache = cache_get('goodreads_'.intval($goodreadsId))) && !empty($cache->data)) {
$books = unserialize($cache->data);
}
else{
$books = array();
$feed = @simplexml_load_file('http://www.goodreads.com/review/list_rss/'.$goodreadsId.'?shelf=read&sort=random&format=json');
if($feed !== false){
foreach( $feed->channel->item as $item ) {
if(!empty($item->book_medium_image_url)){
$book = array(
'image'=> trim(strval($item->book_medium_image_url)),
'title'=>trim(strval($item->title)),
'link'=>trim(strval($item->link)),
'author'=>trim(strval($item->author_name))
);
$books = $book;
}
}
shuffle($books);
$books = array_slice($books, 0,$num);
cache_set('goodreads_'.intval($goodreadsId), serialize($books),'cache', time() 360);
}
}
$source = '<ul class="goodreadsBooks">';
foreach($books as $book){
$remote_img = get_remote_image_from_url($book);
if($remote_img){
$alt = $book.' by '.$book;
$book = theme('imagecache','goodreads', $remote_img , $alt, $alt);
$source .= '<li>'.l($book, $book, array('html'=>true, 'attributes'=>array('title'=>'View '.$alt.' on Goodreads.com'))).'</li>';
}
}
$source .= '</ul>';
return $source;
}
Above is the function we use to grab and parse the Goodreads XML feed and turn it into an HTML list of linked thumbnails. The function accepts a Goodreads user ID as a parameter as well as the number of books to display.
Whenever I deal with grabbing remote data from feeds I cache the result in an effort to save resources and prevent being blackballed from some web services. In this function, I make use of cache_set and cache_get.The basic idea is to pull in the feed and build an array out of the feed elements. For each book, we will grab the remote image and store it locally, then put imagecache to work to resize the image. I have previously created an imagecache preset called "goodreads" that displays the book thumbnails at a consistent size.
Hopefully this function can help you out. There is some more information that could be displayed, you just need to look at what is provided in the feed that you can extract.
Related Links
Back to my blog
Posted by:
Adrian Mummey
Posted on: January 24, 2011
Great article but quite difficult for me :s casino en ligne
Social Media Integration Using Drupal 6 - Part 3
Post a comment