From 06f945f27840b53e57795dadbc38e76f7e11ab1c Mon Sep 17 00:00:00 2001 From: Horus3 Date: Mon, 24 Feb 2014 16:42:14 +0100 Subject: init --- zend/demos/Zend/Gdata/MyLibrary/demo.php | 226 +++++++++++++++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100755 zend/demos/Zend/Gdata/MyLibrary/demo.php (limited to 'zend/demos/Zend/Gdata/MyLibrary/demo.php') diff --git a/zend/demos/Zend/Gdata/MyLibrary/demo.php b/zend/demos/Zend/Gdata/MyLibrary/demo.php new file mode 100755 index 0000000..33045c5 --- /dev/null +++ b/zend/demos/Zend/Gdata/MyLibrary/demo.php @@ -0,0 +1,226 @@ +getMessage() ."\nCredentials provided were ". + "email: [$email] and password [$password].\n"); + } + $this->gdClient = new Zend_Gdata_Books($client); + } + + /** + * Print the content of a feed + * + * @param Zend_Gdata_Gbase_Feed $feed + * @return void + */ + public function printFeed($feed) + { + $i = 0; + foreach($feed as $entry) { + $titles = $entry->getTitles(); + $rating = $entry->getRating(); + if (count($titles)) { + if (!is_object($rating)) { + $rating_str = "?"; + } else { + $rating_str = $rating->getAverage(); + } + print $i." ".$titles[0]->getText(). + ", Rating: ".$rating_str."\n"; + $i++; + } + } + } + + /** + * List books in the My library feed + * + * @return void + */ + public function listLibrary() + { + $feed = $this->gdClient->getUserLibraryFeed(); + print "== Books in my library ==\n"; + $this->printFeed($feed); + print "\n"; + } + + /** + * List books in the annotation feed. + * + * @return void + */ + public function listReviewed() + { + $feed = $this->gdClient->getUserLibraryFeed( + Zend_Gdata_Books::MY_ANNOTATION_FEED_URI); + print "== Books I annotated ==\n"; + $this->printFeed($feed); + print "\n"; + } + + /** + * Add an arbitrary book to the library feed. + * + * @param string $volumeId Volume to the library + * @return void + */ + public function addBookToLibrary($volumeId) + { + $entry = new Zend_Gdata_Books_VolumeEntry(); + $entry->setId( + new Zend_Gdata_App_Extension_Id($volumeId)); + print "Inserting ".$volumeId."\n\n"; + return $this->gdClient->insertVolume($entry); + } + + /** + * Add an arbitrary book to the library feed. + * + * @param string $volumeId Volume to add a rating to + * @param float $rating Numeric rating from 0 to 5 + * @return void + */ + public function addRating($volumeId, $rating) + { + $entry = new Zend_Gdata_Books_VolumeEntry(); + $entry->setId( + new Zend_Gdata_App_Extension_Id($volumeId)); + $entry->setRating( + new Zend_Gdata_Extension_Rating($rating, "0", 5, 1)); + print "Inserting a rating of ".$rating." for ".$volumeId."\n\n"; + return $this->gdClient->insertVolume($entry, + Zend_Gdata_Books::MY_ANNOTATION_FEED_URI); + } + + /** + * Remove an an arbitrary book from a feed (either remove + * from library feed or remove the annotations from annotation + * feed). + * + * @param Zend_Gdata_Books_VolumeEntry $entry + * @return void + */ + public function removeBook($entry) + { + print "Deleting ".$entry->getId()->getText()."\n\n"; + $this->gdClient->deleteVolume($entry); + } + + /** + * Main logic for the demo. + * + * @return void + */ + public function run() + { + $test_volume = "8YEAAAAAYAAJ"; + + // Playing with the library feed + $this->listLibrary(); + + $entry = $this->addBookToLibrary($test_volume); + $this->listLibrary(); + + $this->removeBook($entry); + $this->listLibrary(); + + // Playing with the annotation feed + $this->listReviewed(); + + $entry = $this->addRating($test_volume, 4.0); + $this->listReviewed(); + + $this->removeBook($entry); + $this->listReviewed(); + } +} + +/** + * getInput + * + * @param string $text + * @return string + */ +function getInput($text) +{ + echo $text.': '; + return trim(fgets(STDIN)); +} + +echo "Books Gdata API - my library demo\n\n"; +$email = null; +$pass = null; + +// process command line options +foreach ($argv as $argument) { + $argParts = explode('=', $argument); + if ($argParts[0] == '--email') { + $email = $argParts[1]; + } else if ($argParts[0] == '--pass') { + $pass = $argParts[1]; + } +} + +if (($email == null) || ($pass == null)) { + $email = getInput( + "Please enter your email address [example: username@gmail.com]"); + $pass = getInput( + "Please enter your password [example: mypassword]"); +} + +$demo = new SimpleDemo($email, $pass); +$demo->run(); -- cgit v1.2.3