t('Book Post Filter')); case 'description': return t('Replaces ISBN or Open Library ID, e.g. {{0738531367}}, {{OL1880549M}}, with book information and cover'); case 'no cache': return TRUE; case 'prepare': return $text; case 'process': preg_match_all('/{{(.*)}}/', $text, $match); $tag = $match[0]; $book = array(); $counter=0; for($i=0;$i 'fieldset', '#title' => t('Book Post Filter'), '#collapsible' => TRUE, '#collapsed' => FALSE, ); $form['bookpost_filter']['worldcat'] = array( '#type' => 'checkbox', '#title' => t('Link to WorldCat'), '#default_value' => variable_get('worldcat', 0), ); $form['bookpost_filter']['librarything'] = array( '#type' => 'checkbox', '#title' => t('Link to LibraryThing'), '#default_value' => variable_get('librarything', 0), ); $form['bookpost_filter']['googlebooks'] = array( '#type' => 'checkbox', '#title' => t('Link to Google Book Search'), '#default_value' => variable_get('googlebooks', 0), ); return $form; } } function retrieve_bookdata($id) { $emptyblock = '
'; if(preg_match('/^OL/', $id)) { $bookkey = '/b/'.$id; } else { $isbn = $id; //clean ISBN of dashes and spaces $isbn = str_replace("-", "", $isbn); $isbn = str_replace(" ", "", $isbn); $path = drupal_get_path('module', 'bookpost'); //link to WorldCat, LibraryThing, and Google books if selected if(variable_get('worldcat', 0)) { $worldcat = 'Find on WorldCat

'; } if(variable_get('librarything', 0)) { $librarything = 'Find on LibraryThing

'; } if(variable_get('googlebooks', 0)) { $googlebooks = 'Find on Google Books
'; } //query OpenLibrary for OL ids that match the ISBN $url_bookkeys = "http://openlibrary.org/api/search?q={%22query%22:%22(isbn_10:(".$isbn.")%20OR%20isbn_13:(".$isbn."))%22}&text=true"; $bookkeys = get_url_contents($url_bookkeys, $curltimeout); if($bookkeys == null) return $emptyblock; $obj = json_decode($bookkeys); $bookkeyresult = $obj->{'result'}; //determine number of versions and choose first available //version if none specified $bookversioncount = count($bookkeyresult); if($bookversion == "") $bookversion = 1; elseif ($bookversion > $bookversioncount) $bookversion = $bookversioncount; $bookversion = $bookversion -1; $bookkey = $bookkeyresult[$bookversion]; } $bookpage = "http://openlibrary.org" . $bookkey; $curltimeout = 10; $url = "http://openlibrary.org/api/get?key=".$bookkey."&text=true"; $bookdata = get_url_contents($url, $curltimeout); if($bookdata == null) return $emptyblock; $obj = json_decode($bookdata); $bookdataresult = $obj->{'result'}; //title $title = $bookdataresult->{'title'}; $subtitle = $bookdataresult->{'subtitle'}; $title=ucwords($title); //authors -- handle multiple $authors = $bookdataresult->{'authors'}; if(is_array($authors)) { for($i=0;$i{'key'}; $url_author = "http://openlibrary.org/api/get?key=".$authorkey."&text=true"; $authordata = get_url_contents($url_author, $curltimeout); if($authordata == null) return $emptyblock; $obj = json_decode($authordata); $authorresult = $obj->{'result'}; $name = $authorresult->{'name'}; if($i==0) $authorlist = $name; else $authorlist = $authorlist.",".$name; } } $authors = $authorlist; if($authors=="") { $authors = $bookdataresult->{'by_statement'}; if($authors=="") { $authors=$bookdataresult->{'contributions'}; if(is_array($authors)) $authors=implode(",", $authors); } } $authors = ucwords($authors); //publishers $publishers = $bookdataresult->{'publishers'}; if(is_array($publishers)) { for($i=0;$i{'number_of_pages'}; //publication date $date = $bookdataresult->{'publish_date'}; //link to fulltext if available $link = $bookdataresult->{'ocaid'}; if($link) { $fulltext = '
'; } //build HTML $html_bookdata = ""; //coverimage $html_coverimage = ""; $html_coverimage = "" . $html_coverimage . ""; //title $html_title = '' . $title . ''; //author $html_authors = $authors; //publishers $html_publishers = $publishers; //assemble text $html_text = "Title: " . $html_title . "
" . "Author(s): ". $html_authors . "
" . "Publisher: " .$html_publishers . '
' . "Pages: " . $pages . '
' . "Date: " .$date . '
';// .$fulltext . $worldcat . $librarything . $googlebooks; //div id for themeing $html_bookdata = '

' . $html_coverimage . $html_text . '
'; return $html_bookdata; } //this method replaces file_get_contents, which is sometimes disallowed on servers function get_url_contents($url, $curltimeout) { // Establish a cURL handle. $ch = curl_init($url); // Set our options curl_setopt($ch, CURLOPT_HEADER, false); //false=do not include headers curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //true=return as string curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $curltimeout); //timeout for when OL is down curl_setopt($ch, CURLOPT_TIMEOUT, $curltimeout); //timeout for when OL is down // Execute the request $output = curl_exec($ch); // Close the cURL session. curl_close($ch); // This took out my whole site! //if($output == "") throw new Exception("Open Library Data Unavailable"); if($output == "") return null; else return $output; }