mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 02:34:06 -04:00
Content server: Add endpoint that return book metadata as JSON object /get/json/<id>
This commit is contained in:
parent
ecabc13152
commit
2ee7653427
@ -26,6 +26,7 @@ from calibre.library.server.cache import Cache
|
|||||||
from calibre.library.server.browse import BrowseServer
|
from calibre.library.server.browse import BrowseServer
|
||||||
from calibre.utils.search_query_parser import saved_searches
|
from calibre.utils.search_query_parser import saved_searches
|
||||||
from calibre import prints
|
from calibre import prints
|
||||||
|
from calibre.ebooks.metadata.book.json_codec import JsonCodec
|
||||||
|
|
||||||
|
|
||||||
class DispatchController(object): # {{{
|
class DispatchController(object): # {{{
|
||||||
@ -167,6 +168,7 @@ class LibraryServer(ContentServer, MobileServer, XMLServer, OPDSServer, Cache,
|
|||||||
root_conf = self.config.get('/', {})
|
root_conf = self.config.get('/', {})
|
||||||
root_conf['request.dispatch'] = self.__dispatcher__.dispatcher
|
root_conf['request.dispatch'] = self.__dispatcher__.dispatcher
|
||||||
self.config['/'] = root_conf
|
self.config['/'] = root_conf
|
||||||
|
self.json_codec = JsonCodec()
|
||||||
|
|
||||||
def set_database(self, db):
|
def set_database(self, db):
|
||||||
self.db = db
|
self.db = db
|
||||||
|
@ -5,7 +5,7 @@ __license__ = 'GPL v3'
|
|||||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
import re, os, posixpath
|
import re, os, posixpath, json
|
||||||
|
|
||||||
import cherrypy
|
import cherrypy
|
||||||
|
|
||||||
@ -92,6 +92,8 @@ class ContentServer(object):
|
|||||||
return self.get_cover(id)
|
return self.get_cover(id)
|
||||||
if what == 'opf':
|
if what == 'opf':
|
||||||
return self.get_metadata_as_opf(id)
|
return self.get_metadata_as_opf(id)
|
||||||
|
if what == 'json':
|
||||||
|
return self.get_metadata_as_json(id)
|
||||||
return self.get_format(id, what)
|
return self.get_format(id, what)
|
||||||
|
|
||||||
def static(self, name):
|
def static(self, name):
|
||||||
@ -193,6 +195,17 @@ class ContentServer(object):
|
|||||||
|
|
||||||
return data
|
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):
|
def get_format(self, id, format):
|
||||||
format = format.upper()
|
format = format.upper()
|
||||||
fmt = self.db.format(id, format, index_is_id=True, as_file=True,
|
fmt = self.db.format(id, format, index_is_id=True, as_file=True,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user