Content server: Add endpoint that return book metadata as JSON object /get/json/<id>

This commit is contained in:
Kovid Goyal 2011-08-28 12:25:51 -06:00
parent ecabc13152
commit 2ee7653427
2 changed files with 16 additions and 1 deletions

View File

@ -26,6 +26,7 @@ from calibre.library.server.cache import Cache
from calibre.library.server.browse import BrowseServer
from calibre.utils.search_query_parser import saved_searches
from calibre import prints
from calibre.ebooks.metadata.book.json_codec import JsonCodec
class DispatchController(object): # {{{
@ -167,6 +168,7 @@ class LibraryServer(ContentServer, MobileServer, XMLServer, OPDSServer, Cache,
root_conf = self.config.get('/', {})
root_conf['request.dispatch'] = self.__dispatcher__.dispatcher
self.config['/'] = root_conf
self.json_codec = JsonCodec()
def set_database(self, db):
self.db = db

View File

@ -5,7 +5,7 @@ __license__ = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import re, os, posixpath
import re, os, posixpath, json
import cherrypy
@ -92,6 +92,8 @@ class ContentServer(object):
return self.get_cover(id)
if what == 'opf':
return self.get_metadata_as_opf(id)
if what == 'json':
return self.get_metadata_as_json(id)
return self.get_format(id, what)
def static(self, name):
@ -193,6 +195,17 @@ class ContentServer(object):
return data
def get_metadata_as_json(self, id_):
cherrypy.response.headers['Content-Type'] = \
'application/json; charset=utf-8'
mi = self.db.get_metadata(id_, index_is_id=True)
cherrypy.response.timeout = 3600
cherrypy.response.headers['Last-Modified'] = \
self.last_modified(mi.last_modified)
data = self.json_codec.encode_book_metadata(mi)
return json.dumps(data, ensure_ascii=False).encode('utf-8')
def get_format(self, id, format):
format = format.upper()
fmt = self.db.format(id, format, index_is_id=True, as_file=True,