diff --git a/src/calibre/library/server/base.py b/src/calibre/library/server/base.py index 319feefa44..9921524343 100644 --- a/src/calibre/library/server/base.py +++ b/src/calibre/library/server/base.py @@ -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 diff --git a/src/calibre/library/server/content.py b/src/calibre/library/server/content.py index ad28c532a4..e1141d3d95 100644 --- a/src/calibre/library/server/content.py +++ b/src/calibre/library/server/content.py @@ -5,7 +5,7 @@ __license__ = 'GPL v3' __copyright__ = '2010, Kovid Goyal ' __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,