with the book name and a link to the work on lt. Also prints the rating as a score from 0-10.
Author: Atle Frenvik Sveen
Author URI: http://atlefren.net
*/
/*
Copyright (c) 2009 Atle Frenvik Sveen
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* call this from Wordpress
*
*
*/
function libraryThing_lastbooks($username,$devkey,$numBooks=5){
$lastBooks = new ltLastBooks($username,$devkey,$numBooks);
echo $lastBooks->getBooksHtml();
}
/**
* Gets the n last read books from LibraryThing.com and presents them in an
* with the book name and a link to the work on lt. Also prints the rating as a score from 0-10
*
* Makes use of the Javascript APIs from LT, which means that some string magic has to be done.
*
* Todo: Implement support for caching of LT-data
*
* @author Atle Frenvik Sveen
*/
class ltLastBooks{
protected $username,$devkey,$numBooks;
/**
* Constructor
*
* @access public
* @param string $username LibraryThing username
* @param string $devkey JSON book API key, see http://www.librarything.com/api/json.php
* @param int $numBooks number of books to show
*/
public function __construct($username,$devkey,$numBooks=5) {
$this->username = $username;
$this->devkey = $devkey;
$this->numBooks = $numBooks;
}
/**
* returns the html code for the books
*
* @access public
*/
public function getBooksHtml(){
//get the sorted array of books
$books = $this->getBookInfo();
$numBooks = $this->numBooks;
if($books != false){
if(sizeof($books) < $numBooks){
$numBooks = sizeof($books);
}
//create an unordened list
$html = "\n";
for ($i=0;$i<$numBooks;$i++){
$html .= "\t- ";
//link to the work on LT
$html .= "" . $books[$i]['title'] . " ";
$html .= $this->parseBookRating($books[$i]['rating']);
$html .= "
\n";
}
$html .= "
";
return $html;
}
else {
return "No books to show\n";
}
}
/**
* Parse rating
*
* @access private
* @param float $rating Bookrating from LT (0-5, .5 allowed)
*/
private function parseBookRating($rating){
//check if set
if($rating){
$rating = $rating*2;
return "(" . $rating . "/10)";
}
else {
return "(n/a)";
}
}
/**
* Get JSON from LT
*
* @access private
*/
private function getBookInfo(){
//build the url for JSON call
$url = "http://www.librarything.com/api/json_books.php?";
$url .= "userid=" . $this->username;
$url .= "&key=" . $this->devkey;
//fetching 1000 to make sure we get all books
$url .= "&resultsets=books,bookdates,bookratings&limit=bookswithstartorfinishdates&max=1000";
//call JSON
$lt = file_get_contents($url);
//make proper JSON of the returned string
$json = $this->fixJson($lt);
if($json == false){
echo "an error occured when parsing JSON";
//an error occured when parsing "JSON"
return false;
}
else {
//decode JSON to asscociative array
$json = json_decode($json,true);
if(!$json){
//no books to show
echo "no books to show\n
";
return false;
}
else {
//get the book info as an aray
$books = $json["books"];
//the new array to put the books we want in
$myBooks = array();
foreach ($books as $key =>$book) {
//we only want books with finishdate set
if ($book['startfinishdates'][0]['finished_stamp'] != null) {
$myBooks[$key]['id'] = $book['book_id'];
$myBooks[$key]['title'] = $book['title'];
$myBooks[$key]['rating'] = $book['rating'];
$myBooks[$key]['finished'] = $book['startfinishdates'][0]['finished_stamp'];
}
}
if(sizeof($myBooks) == 0){
//no books with startorfinishdates
echo "no books with startorfinishdates
\n";
return false;
}
else {
//sort the array by finished_stamp
$myBooks = $this->sortArrayByKey($myBooks,"finished");
return $myBooks;
}
}
}
}
/**
* Fix the JSON from LT
*
* @access private
* @param string $inputStr LT-formatted "JSON"
*/
private function fixJson($inputStr) {
$left = "var widgetResults = ";
$right = "LibraryThing.bookAPI.makeBasicwidget(widgetResults);";
$inputStr = strstr($inputStr,$left);
$posLeft=strpos($inputStr, $left);
if ( $posLeft===false ) {
return false;
}
$posLeft+=strlen($left);
$posRight=strpos($inputStr, $right, $posLeft);
if ( $posRight===false ) {
return false;
}
return substr(substr($inputStr, $posLeft, $posRight-$posLeft),0,-3);
}
/**
* Sort an associative array on a key (uses the array_sorter class)
* @access private
* @param mixed $array array to sort
* @param string $key array key to sort by
* @param boolean $asc sort order (ascending or descending)
*/
private function sortArrayByKey(&$array, $key, $asc=true){
$sorter = new array_sorter($array, $key, $asc);
return $sorter->sortit();
}
}//end of class
//class to sort arrays (taken from http://us2.php.net/manual/en/function.uksort.php#71152)
//THIS CLASS IS NOT SUBJECT TO THE LISENCE STATED ABOVE, SEE THE MENTIONED WEBPAGE FOR DETAILS
/**
* Handles multidimentional array sorting by a key (not recursive)
*
* @author Oliwier Ptak
*/
class array_sorter
{
var $skey = false;
var $sarray = false;
var $sasc = true;
/**
* Constructor
*
* @access public
* @param mixed $array array to sort
* @param string $key array key to sort by
* @param boolean $asc sort order (ascending or descending)
*/
function array_sorter(&$array, $key, $asc=true)
{
$this->sarray = $array;
$this->skey = $key;
$this->sasc = $asc;
}
/**
* Sort method
*
* @access public
* @param boolean $remap if true reindex the array to rewrite indexes
*/
function sortit($remap=true)
{
$array = &$this->sarray;
uksort($array, array($this, "_as_cmp"));
if ($remap)
{
$tmp = array();
while (list($id, $data) = each($array))
$tmp[] = $data;
return $tmp;
}
return $array;
}
/**
* Custom sort function
*
* @access private
* @param mixed $a an array entry
* @param mixed $b an array entry
*/
function _as_cmp($a, $b){
//since uksort will pass here only indexes get real values from our array
if (!is_array($a) && !is_array($b)){
$a = $this->sarray[$a][$this->skey];
$b = $this->sarray[$b][$this->skey];
}
//if string - use string comparision
if (!ctype_digit($a) && !ctype_digit($b)){
if ($this->sasc)
return strcasecmp($a, $b);
else
return strcasecmp($b, $a);
}
else{
if (intval($a) == intval($b))
return 0;
if ($this->sasc)
return (intval($a) > intval($b)) ? -1 : 1;
else
return (intval($a) > intval($b)) ? 1 : -1;
}
}
}//end of class
?>