Social Media Integration Using Drupal 6 - Part 3

January 24, 2011Comments (2)Printer-friendly versionPrinter-friendly version

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.

Onyx Goodreads.com Drupal Widget

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:

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
Adrian Mummey
Posted by: Adrian Mummey
Posted on: January 24, 2011
Kelly comments:
posted: 02.11.11

Great article but quite difficult for me :s casino en ligne

Twitter Trackbacks for Social Media Integration Using Drupa endorses:
posted: 02.03.11

Social Media Integration Using Drupal 6 - Part 3

Post a comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
Image CAPTCHA
Enter the characters shown in the image.
Adrian Mummey

you should know

Adrian is a programmer and web development expert living on the beach in Ventura, CA. He is currently attending California State University Channel Islands and is pursuing an MS in Computer Science. He recently returned to California after a long stint in Mongolia. He travelled to Mongolia as a Peace Corps volunteer in 2004, where he worked as a teacher at a rural school until 2006.

His specialties in development are PHP, MySQL, Javascript and CSS and he is always looking for interesting programming projects and enjoys producing Open Source Software. Adrian is a Zend PHP 5 certified engineer as well as an AB MySQL certified developer.

you should follow

you should love

  • Is currently training for Las Vegas Marathon
  • Honed his amazing web skills in a yurt on the frigid steppe of Mongolia.
  • Climbed Mt. Kilimanjaro.
  • Can give a culturally appropriate “Cheers” in over 10 languages.
  • His international port of Joomla! was localized in nine countries.
  • Is currently pursuing his lifelong goal of living on the beach.
  • Graduated from California Polytechnic State University, San Luis Obispo, B.S. Computer Science.

you should see