demo.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage Demos
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Loader
  23. */
  24. require_once 'Zend/Loader.php';
  25. /**
  26. * @see Zend_Gdata_Books
  27. */
  28. Zend_Loader::loadClass('Zend_Gdata_Books');
  29. /**
  30. * @see Zend_Gdata_ClientLogin
  31. */
  32. Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
  33. /**
  34. * @see Zend_Gdata_App_AuthException
  35. */
  36. Zend_Loader::loadClass('Zend_Gdata_App_AuthException');
  37. class SimpleDemo {
  38. /**
  39. * Constructor
  40. *
  41. * @param string $email
  42. * @param string $password
  43. * @return void
  44. */
  45. public function __construct($email, $password)
  46. {
  47. try {
  48. $client = Zend_Gdata_ClientLogin::getHttpClient($email, $password,
  49. Zend_Gdata_Books::AUTH_SERVICE_NAME);
  50. } catch (Zend_Gdata_App_AuthException $ae) {
  51. exit("Error: ". $ae->getMessage() ."\nCredentials provided were ".
  52. "email: [$email] and password [$password].\n");
  53. }
  54. $this->gdClient = new Zend_Gdata_Books($client);
  55. }
  56. /**
  57. * Print the content of a feed
  58. *
  59. * @param Zend_Gdata_Gbase_Feed $feed
  60. * @return void
  61. */
  62. public function printFeed($feed)
  63. {
  64. $i = 0;
  65. foreach($feed as $entry) {
  66. $titles = $entry->getTitles();
  67. $rating = $entry->getRating();
  68. if (count($titles)) {
  69. if (!is_object($rating)) {
  70. $rating_str = "?";
  71. } else {
  72. $rating_str = $rating->getAverage();
  73. }
  74. print $i." ".$titles[0]->getText().
  75. ", Rating: ".$rating_str."\n";
  76. $i++;
  77. }
  78. }
  79. }
  80. /**
  81. * List books in the My library feed
  82. *
  83. * @return void
  84. */
  85. public function listLibrary()
  86. {
  87. $feed = $this->gdClient->getUserLibraryFeed();
  88. print "== Books in my library ==\n";
  89. $this->printFeed($feed);
  90. print "\n";
  91. }
  92. /**
  93. * List books in the annotation feed.
  94. *
  95. * @return void
  96. */
  97. public function listReviewed()
  98. {
  99. $feed = $this->gdClient->getUserLibraryFeed(
  100. Zend_Gdata_Books::MY_ANNOTATION_FEED_URI);
  101. print "== Books I annotated ==\n";
  102. $this->printFeed($feed);
  103. print "\n";
  104. }
  105. /**
  106. * Add an arbitrary book to the library feed.
  107. *
  108. * @param string $volumeId Volume to the library
  109. * @return void
  110. */
  111. public function addBookToLibrary($volumeId)
  112. {
  113. $entry = new Zend_Gdata_Books_VolumeEntry();
  114. $entry->setId(
  115. new Zend_Gdata_App_Extension_Id($volumeId));
  116. print "Inserting ".$volumeId."\n\n";
  117. return $this->gdClient->insertVolume($entry);
  118. }
  119. /**
  120. * Add an arbitrary book to the library feed.
  121. *
  122. * @param string $volumeId Volume to add a rating to
  123. * @param float $rating Numeric rating from 0 to 5
  124. * @return void
  125. */
  126. public function addRating($volumeId, $rating)
  127. {
  128. $entry = new Zend_Gdata_Books_VolumeEntry();
  129. $entry->setId(
  130. new Zend_Gdata_App_Extension_Id($volumeId));
  131. $entry->setRating(
  132. new Zend_Gdata_Extension_Rating($rating, "0", 5, 1));
  133. print "Inserting a rating of ".$rating." for ".$volumeId."\n\n";
  134. return $this->gdClient->insertVolume($entry,
  135. Zend_Gdata_Books::MY_ANNOTATION_FEED_URI);
  136. }
  137. /**
  138. * Remove an an arbitrary book from a feed (either remove
  139. * from library feed or remove the annotations from annotation
  140. * feed).
  141. *
  142. * @param Zend_Gdata_Books_VolumeEntry $entry
  143. * @return void
  144. */
  145. public function removeBook($entry)
  146. {
  147. print "Deleting ".$entry->getId()->getText()."\n\n";
  148. $this->gdClient->deleteVolume($entry);
  149. }
  150. /**
  151. * Main logic for the demo.
  152. *
  153. * @return void
  154. */
  155. public function run()
  156. {
  157. $test_volume = "8YEAAAAAYAAJ";
  158. // Playing with the library feed
  159. $this->listLibrary();
  160. $entry = $this->addBookToLibrary($test_volume);
  161. $this->listLibrary();
  162. $this->removeBook($entry);
  163. $this->listLibrary();
  164. // Playing with the annotation feed
  165. $this->listReviewed();
  166. $entry = $this->addRating($test_volume, 4.0);
  167. $this->listReviewed();
  168. $this->removeBook($entry);
  169. $this->listReviewed();
  170. }
  171. }
  172. /**
  173. * getInput
  174. *
  175. * @param string $text
  176. * @return string
  177. */
  178. function getInput($text)
  179. {
  180. echo $text.': ';
  181. return trim(fgets(STDIN));
  182. }
  183. echo "Books Gdata API - my library demo\n\n";
  184. $email = null;
  185. $pass = null;
  186. // process command line options
  187. foreach ($argv as $argument) {
  188. $argParts = explode('=', $argument);
  189. if ($argParts[0] == '--email') {
  190. $email = $argParts[1];
  191. } else if ($argParts[0] == '--pass') {
  192. $pass = $argParts[1];
  193. }
  194. }
  195. if (($email == null) || ($pass == null)) {
  196. $email = getInput(
  197. "Please enter your email address [example: username@gmail.com]");
  198. $pass = getInput(
  199. "Please enter your password [example: mypassword]");
  200. }
  201. $demo = new SimpleDemo($email, $pass);
  202. $demo->run();