view goodreads/bkp/syncGoodreadsShelves.php @ 6:077b0a0a3e6d

remaining originals according to dependency walk
author Robert Boland <robert@markup.co.uk>
date Thu, 16 Feb 2017 22:29:02 +0000
parents
children 385ddd7c4b55 a67bf725e87b
line wrap: on
line source

<?php

$userID=$_GET['userID'];
$grID=$_GET['grID'];

$url= "http://www.goodreads.com/shelf/list?format=xml&key=8vxmKmAn8AVolobtvBbMIg&user_id=" . $grID;
//echo $url;

	$crl = curl_init();
	$timeout = 5;
	curl_setopt ($crl, CURLOPT_URL,$url);
	curl_setopt ($crl, CURLOPT_ENCODING , "gzip");
	curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
	$ret = curl_exec($crl);	
	$ret = preg_replace("/[^[:alnum:][:punct:][:space:]]/","",$ret);
	//echo $ret;		
	
	include "../../private/db.php"; 
	
	$shelves = array();
	
	$xml = new SimpleXMLElement(convert_utf8($ret));
	for($i=0;$i<sizeof($xml->shelves->user_shelf);$i++){
		
		$shelfName= $xml->shelves->user_shelf[$i]->name;
		//echo $shelfName;
		$shelves[$i] = $shelfName;
		
		/*$url = "http://www.goodreads.com/review/list/$grID?shelf=$shelfName&format=xml&key=8vxmKmAn8AVolobtvBbMIg";
		curl_setopt ($crl, CURLOPT_URL,$url);
		$ret1 = curl_exec($crl);

		$ret1 = preg_replace("/[^[:alnum:][:punct:][:space:]]/"," ",$ret1);
		$ret1 = convert_utf8($ret1);
		$xml1 = new SimpleXMLElement($ret1);
		for($j=0;$j<sizeof($xml1->books->book);$j++){
			//echo $xml1->books->book[$j]->isbn . "\n";
			$isbn = trim($xml1->books->book[$j]->isbn);
			$picURL = trim($xml1->books->book[$j]->image_url);
			$title = trim($xml1->books->book[$j]->title);
			//echo $title;
			$add = "CALL insertUserSave($userID,\"$shelfName\",'$isbn','$picURL',\"$title\")"; 
			//echo $add;
			$run = mysqli_query($link, $add); //add the book to the list in usersave
		}*/
	}
	curl_close($crl);

	$ynrLists = "CALL getAllSavedBooksByUser($userID)";
	//echo $add;
	$results = mysqli_query($link, $ynrLists) or exit( mysqli_error( $link ));

	require_once('GoodreadsAPI.php');
	session_start();
	$access_token = $_SESSION['access_token'];
	print_r($access_token);
	$obj = new GoodreadsApi(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

	while($line = mysqli_fetch_assoc($results)) {
		$shelf = trim($line["SavedListName"]);
		$isbn = trim($line["ISBN"]);
		if($isbn != "")
		{
			if (!in_array($shelf, $shelves))
			{	
				$url= "http://www.goodreads.com/book/isbn?isbn=" . $isbn . "&key=8vxmKmAn8AVolobtvBbMIg";
				//echo $url;
				$crl = curl_init();
				$timeout = 5;
				curl_setopt ($crl, CURLOPT_URL,$url);
				curl_setopt ($crl, CURLOPT_ENCODING , "gzip");
				curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
				$ret = curl_exec($crl);
				curl_close($crl);

				$ret = preg_replace("/[^[:alnum:][:punct:][:space:]]/","",$ret);
				if(trim($ret) != "")
				{
					$xml = new SimpleXMLElement(convert_utf8($ret));			
					$book_id = $xml->book->id;					
					echo $shelf . ":" . $book_id;
					$content = $obj->doPost('http://www.goodreads.com/shelf/add_to_shelf.xml', array("name" => "$shelf", "book_id" => "$book_id"));
					print_r($content);
					echo "$$";
				}
			}
		}
	}
	
	unset ($obj);
	
	/*echo "<html>
		<link rel=\"stylesheet\" type=\"text/css\" href=\"../user/style.css\">
		<head>
		<title>Goodreads Import Successful</title>
		</head>
		<body>

		<h1>YourNextRead</h1>
		<meta http-equiv=\"refresh\" content=\"7; url=http://www.YourNextRead.com/\"> 
		<p>Thank you! Your Goodreads Lists have successfully been imported to your YourNextRead account</p>
		<p>Click <a href=\"javascript:window.history.go(-2)\">here</a> to go back to YourNextRead</p>

		<p>www.YourNextRead.com</p>

		</body>
		</html>";
	*/
	
	function convert_utf8($str){
    if(!seems_utf8($str))
        return mb_convert_encoding($str, 'UTF-8');
    return $str;
}
function seems_utf8($Str) {
 for ($i=0; $i<strlen($Str); $i++) {
  if (ord($Str[$i]) < 0x80) continue; # 0bbbbbbb
  elseif ((ord($Str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
  elseif ((ord($Str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
  elseif ((ord($Str[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
  elseif ((ord($Str[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
  elseif ((ord($Str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
  else return false; # Does not match any model
  for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
   if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80))
   return false;
  }
 }
 return true;
}
?>