/gdata/books/service.py

http://radioappz.googlecode.com/ · Python · 266 lines · 206 code · 9 blank · 51 comment · 5 complexity · 95b54f77c048a3032b160ca3e0e7a086 MD5 · raw file

  1. #!/usr/bin/python
  2. """
  3. Extend gdata.service.GDataService to support authenticated CRUD ops on
  4. Books API
  5. http://code.google.com/apis/books/docs/getting-started.html
  6. http://code.google.com/apis/books/docs/gdata/developers_guide_protocol.html
  7. TODO: (here and __init__)
  8. * search based on label, review, or other annotations (possible?)
  9. * edit (specifically, Put requests) seem to fail effect a change
  10. Problems With API:
  11. * Adding a book with a review to the library adds a note, not a review.
  12. This does not get included in the returned item. You see this by
  13. looking at My Library through the website.
  14. * Editing a review never edits a review (unless it is freshly added, but
  15. see above). More generally,
  16. * a Put request with changed annotations (label/rating/review) does NOT
  17. change the data. Note: Put requests only work on the href from
  18. GetEditLink (as per the spec). Do not try to PUT to the annotate or
  19. library feeds, this will cause a 400 Invalid URI Bad Request response.
  20. Attempting to Post to one of the feeds with the updated annotations
  21. does not update them. See the following for (hopefully) a follow up:
  22. google.com/support/forum/p/booksearch-apis/thread?tid=27fd7f68de438fc8
  23. * Attempts to workaround the edit problem continue to fail. For example,
  24. removing the item, editing the data, readding the item, gives us only
  25. our originally added data (annotations). This occurs even if we
  26. completely shut python down, refetch the book from the public feed,
  27. and re-add it. There is some kind of persistence going on that I
  28. cannot change. This is likely due to the annotations being cached in
  29. the annotation feed and the inability to edit (see Put, above)
  30. * GetAnnotationLink has www.books.... as the server, but hitting www...
  31. results in a bad URI error.
  32. * Spec indicates there may be multiple labels, but there does not seem
  33. to be a way to get the server to accept multiple labels, nor does the
  34. web interface have an obvious way to have multiple labels. Multiple
  35. labels are never returned.
  36. """
  37. __author__ = "James Sams <sams.james@gmail.com>"
  38. __copyright__ = "Apache License v2.0"
  39. from shlex import split
  40. import gdata.service
  41. try:
  42. import books
  43. except ImportError:
  44. import gdata.books as books
  45. BOOK_SERVER = "books.google.com"
  46. GENERAL_FEED = "/books/feeds/volumes"
  47. ITEM_FEED = "/books/feeds/volumes/"
  48. LIBRARY_FEED = "/books/feeds/users/%s/collections/library/volumes"
  49. ANNOTATION_FEED = "/books/feeds/users/%s/volumes"
  50. PARTNER_FEED = "/books/feeds/p/%s/volumes"
  51. BOOK_SERVICE = "print"
  52. ACCOUNT_TYPE = "HOSTED_OR_GOOGLE"
  53. class BookService(gdata.service.GDataService):
  54. def __init__(self, email=None, password=None, source=None,
  55. server=BOOK_SERVER, account_type=ACCOUNT_TYPE,
  56. exception_handlers=tuple(), **kwargs):
  57. """source should be of form 'ProgramCompany - ProgramName - Version'"""
  58. gdata.service.GDataService.__init__(self, email=email,
  59. password=password, service=BOOK_SERVICE, source=source,
  60. server=server, **kwargs)
  61. self.exception_handlers = exception_handlers
  62. def search(self, q, start_index="1", max_results="10",
  63. min_viewability="none", feed=GENERAL_FEED,
  64. converter=books.BookFeed.FromString):
  65. """
  66. Query the Public search feed. q is either a search string or a
  67. gdata.service.Query instance with a query set.
  68. min_viewability must be "none", "partial", or "full".
  69. If you change the feed to a single item feed, note that you will
  70. probably need to change the converter to be Book.FromString
  71. """
  72. if not isinstance(q, gdata.service.Query):
  73. q = gdata.service.Query(text_query=q)
  74. if feed:
  75. q.feed = feed
  76. q['start-index'] = start_index
  77. q['max-results'] = max_results
  78. q['min-viewability'] = min_viewability
  79. return self.Get(uri=q.ToUri(),converter=converter)
  80. def search_by_keyword(self, q='', feed=GENERAL_FEED, start_index="1",
  81. max_results="10", min_viewability="none", **kwargs):
  82. """
  83. Query the Public Search Feed by keyword. Non-keyword strings can be
  84. set in q. This is quite fragile. Is there a function somewhere in
  85. the Google library that will parse a query the same way that Google
  86. does?
  87. Legal Identifiers are listed below and correspond to their meaning
  88. at http://books.google.com/advanced_book_search:
  89. all_words
  90. exact_phrase
  91. at_least_one
  92. without_words
  93. title
  94. author
  95. publisher
  96. subject
  97. isbn
  98. lccn
  99. oclc
  100. seemingly unsupported:
  101. publication_date: a sequence of two, two tuples:
  102. ((min_month,min_year),(max_month,max_year))
  103. where month is one/two digit month, year is 4 digit, eg:
  104. (('1','2000'),('10','2003')). Lower bound is inclusive,
  105. upper bound is exclusive
  106. """
  107. for k, v in kwargs.items():
  108. if not v:
  109. continue
  110. k = k.lower()
  111. if k == 'all_words':
  112. q = "%s %s" % (q, v)
  113. elif k == 'exact_phrase':
  114. q = '%s "%s"' % (q, v.strip('"'))
  115. elif k == 'at_least_one':
  116. q = '%s %s' % (q, ' '.join(['OR "%s"' % x for x in split(v)]))
  117. elif k == 'without_words':
  118. q = '%s %s' % (q, ' '.join(['-"%s"' % x for x in split(v)]))
  119. elif k in ('author','title', 'publisher'):
  120. q = '%s %s' % (q, ' '.join(['in%s:"%s"'%(k,x) for x in split(v)]))
  121. elif k == 'subject':
  122. q = '%s %s' % (q, ' '.join(['%s:"%s"' % (k,x) for x in split(v)]))
  123. elif k == 'isbn':
  124. q = '%s ISBN%s' % (q, v)
  125. elif k == 'issn':
  126. q = '%s ISSN%s' % (q,v)
  127. elif k == 'oclc':
  128. q = '%s OCLC%s' % (q,v)
  129. else:
  130. raise ValueError("Unsupported search keyword")
  131. return self.search(q.strip(),start_index=start_index, feed=feed,
  132. max_results=max_results,
  133. min_viewability=min_viewability)
  134. def search_library(self, q, id='me', **kwargs):
  135. """Like search, but in a library feed. Default is the authenticated
  136. user's feed. Change by setting id."""
  137. if 'feed' in kwargs:
  138. raise ValueError("kwarg 'feed' conflicts with library_id")
  139. feed = LIBRARY_FEED % id
  140. return self.search(q, feed=feed, **kwargs)
  141. def search_library_by_keyword(self, id='me', **kwargs):
  142. """Hybrid of search_by_keyword and search_library
  143. """
  144. if 'feed' in kwargs:
  145. raise ValueError("kwarg 'feed' conflicts with library_id")
  146. feed = LIBRARY_FEED % id
  147. return self.search_by_keyword(feed=feed,**kwargs)
  148. def search_annotations(self, q, id='me', **kwargs):
  149. """Like search, but in an annotation feed. Default is the authenticated
  150. user's feed. Change by setting id."""
  151. if 'feed' in kwargs:
  152. raise ValueError("kwarg 'feed' conflicts with library_id")
  153. feed = ANNOTATION_FEED % id
  154. return self.search(q, feed=feed, **kwargs)
  155. def search_annotations_by_keyword(self, id='me', **kwargs):
  156. """Hybrid of search_by_keyword and search_annotations
  157. """
  158. if 'feed' in kwargs:
  159. raise ValueError("kwarg 'feed' conflicts with library_id")
  160. feed = ANNOTATION_FEED % id
  161. return self.search_by_keyword(feed=feed,**kwargs)
  162. def add_item_to_library(self, item):
  163. """Add the item, either an XML string or books.Book instance, to the
  164. user's library feed"""
  165. feed = LIBRARY_FEED % 'me'
  166. return self.Post(data=item, uri=feed, converter=books.Book.FromString)
  167. def remove_item_from_library(self, item):
  168. """
  169. Remove the item, a books.Book instance, from the authenticated user's
  170. library feed. Using an item retrieved from a public search will fail.
  171. """
  172. return self.Delete(item.GetEditLink().href)
  173. def add_annotation(self, item):
  174. """
  175. Add the item, either an XML string or books.Book instance, to the
  176. user's annotation feed.
  177. """
  178. # do not use GetAnnotationLink, results in 400 Bad URI due to www
  179. return self.Post(data=item, uri=ANNOTATION_FEED % 'me',
  180. converter=books.Book.FromString)
  181. def edit_annotation(self, item):
  182. """
  183. Send an edited item, a books.Book instance, to the user's annotation
  184. feed. Note that whereas extra annotations in add_annotations, minus
  185. ratings which are immutable once set, are simply added to the item in
  186. the annotation feed, if an annotation has been removed from the item,
  187. sending an edit request will remove that annotation. This should not
  188. happen with add_annotation.
  189. """
  190. return self.Put(data=item, uri=item.GetEditLink().href,
  191. converter=books.Book.FromString)
  192. def get_by_google_id(self, id):
  193. return self.Get(ITEM_FEED + id, converter=books.Book.FromString)
  194. def get_library(self, id='me',feed=LIBRARY_FEED, start_index="1",
  195. max_results="100", min_viewability="none",
  196. converter=books.BookFeed.FromString):
  197. """
  198. Return a generator object that will return gbook.Book instances until
  199. the search feed no longer returns an item from the GetNextLink method.
  200. Thus max_results is not the maximum number of items that will be
  201. returned, but rather the number of items per page of searches. This has
  202. been set high to reduce the required number of network requests.
  203. """
  204. q = gdata.service.Query()
  205. q.feed = feed % id
  206. q['start-index'] = start_index
  207. q['max-results'] = max_results
  208. q['min-viewability'] = min_viewability
  209. x = self.Get(uri=q.ToUri(), converter=converter)
  210. while 1:
  211. for entry in x.entry:
  212. yield entry
  213. else:
  214. l = x.GetNextLink()
  215. if l: # hope the server preserves our preferences
  216. x = self.Get(uri=l.href, converter=converter)
  217. else:
  218. break
  219. def get_annotations(self, id='me', start_index="1", max_results="100",
  220. min_viewability="none", converter=books.BookFeed.FromString):
  221. """
  222. Like get_library, but for the annotation feed
  223. """
  224. return self.get_library(id=id, feed=ANNOTATION_FEED,
  225. max_results=max_results, min_viewability = min_viewability,
  226. converter=converter)