mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add an option to content server ajax to get JSON metadata that is more compatible with the wireless driver.
This commit is contained in:
parent
ad9ab338e1
commit
a12f4d79aa
@ -15,7 +15,8 @@ from binascii import hexlify, unhexlify
|
|||||||
import cherrypy
|
import cherrypy
|
||||||
|
|
||||||
from calibre.utils.date import isoformat
|
from calibre.utils.date import isoformat
|
||||||
from calibre.utils.config import prefs
|
from calibre.utils.config import prefs, tweaks
|
||||||
|
from calibre.ebooks.metadata import title_sort
|
||||||
from calibre.ebooks.metadata.book.json_codec import JsonCodec
|
from calibre.ebooks.metadata.book.json_codec import JsonCodec
|
||||||
from calibre.utils.icu import sort_key
|
from calibre.utils.icu import sort_key
|
||||||
from calibre.library.server import custom_fields_to_display
|
from calibre.library.server import custom_fields_to_display
|
||||||
@ -118,12 +119,16 @@ class AjaxServer(object):
|
|||||||
|
|
||||||
|
|
||||||
# Get book metadata {{{
|
# Get book metadata {{{
|
||||||
def ajax_book_to_json(self, book_id, get_category_urls=True):
|
def ajax_book_to_json(self, book_id, get_category_urls=True,
|
||||||
|
device_compatible=False):
|
||||||
mi = self.db.get_metadata(book_id, index_is_id=True)
|
mi = self.db.get_metadata(book_id, index_is_id=True)
|
||||||
try:
|
|
||||||
mi.rating = mi.rating/2.
|
if not device_compatible:
|
||||||
except:
|
try:
|
||||||
mi.rating = 0.0
|
mi.rating = mi.rating/2.
|
||||||
|
except:
|
||||||
|
mi.rating = 0.0
|
||||||
|
|
||||||
data = self.ajax_json_codec.encode_book_metadata(mi)
|
data = self.ajax_json_codec.encode_book_metadata(mi)
|
||||||
for x in ('publication_type', 'size', 'db_id', 'lpath', 'mime',
|
for x in ('publication_type', 'size', 'db_id', 'lpath', 'mime',
|
||||||
'rights', 'book_producer'):
|
'rights', 'book_producer'):
|
||||||
@ -131,57 +136,68 @@ class AjaxServer(object):
|
|||||||
|
|
||||||
data['cover'] = absurl(self.opts.url_prefix, u'/get/cover/%d'%book_id)
|
data['cover'] = absurl(self.opts.url_prefix, u'/get/cover/%d'%book_id)
|
||||||
data['thumbnail'] = absurl(self.opts.url_prefix, u'/get/thumb/%d'%book_id)
|
data['thumbnail'] = absurl(self.opts.url_prefix, u'/get/thumb/%d'%book_id)
|
||||||
mi.format_metadata = {k.lower():dict(v) for k, v in
|
|
||||||
mi.format_metadata.iteritems()}
|
|
||||||
for v in mi.format_metadata.itervalues():
|
|
||||||
mtime = v.get('mtime', None)
|
|
||||||
if mtime is not None:
|
|
||||||
v['mtime'] = isoformat(mtime, as_utc=True)
|
|
||||||
data['format_metadata'] = mi.format_metadata
|
|
||||||
fmts = set(x.lower() for x in mi.format_metadata.iterkeys())
|
|
||||||
pf = prefs['output_format'].lower()
|
|
||||||
other_fmts = list(fmts)
|
|
||||||
try:
|
|
||||||
fmt = pf if pf in fmts else other_fmts[0]
|
|
||||||
except:
|
|
||||||
fmt = None
|
|
||||||
if fmts and fmt:
|
|
||||||
other_fmts = [x for x in fmts if x != fmt]
|
|
||||||
data['formats'] = sorted(fmts)
|
|
||||||
if fmt:
|
|
||||||
data['main_format'] = {fmt: absurl(self.opts.url_prefix, u'/get/%s/%d'%(fmt, book_id))}
|
|
||||||
else:
|
|
||||||
data['main_format'] = None
|
|
||||||
data['other_formats'] = {fmt: absurl(self.opts.url_prefix, u'/get/%s/%d'%(fmt, book_id)) for fmt
|
|
||||||
in other_fmts}
|
|
||||||
|
|
||||||
if get_category_urls:
|
if not device_compatible:
|
||||||
category_urls = data['category_urls'] = {}
|
mi.format_metadata = {k.lower():dict(v) for k, v in
|
||||||
ccache = self.categories_cache()
|
mi.format_metadata.iteritems()}
|
||||||
for key in mi.all_field_keys():
|
for v in mi.format_metadata.itervalues():
|
||||||
fm = mi.metadata_for_field(key)
|
mtime = v.get('mtime', None)
|
||||||
if (fm and fm['is_category'] and not fm['is_csp'] and
|
if mtime is not None:
|
||||||
key != 'formats' and fm['datatype'] not in ['rating']):
|
v['mtime'] = isoformat(mtime, as_utc=True)
|
||||||
categories = mi.get(key)
|
data['format_metadata'] = mi.format_metadata
|
||||||
if isinstance(categories, basestring):
|
fmts = set(x.lower() for x in mi.format_metadata.iterkeys())
|
||||||
categories = [categories]
|
pf = prefs['output_format'].lower()
|
||||||
if categories is None:
|
other_fmts = list(fmts)
|
||||||
categories = []
|
try:
|
||||||
dbtags = {}
|
fmt = pf if pf in fmts else other_fmts[0]
|
||||||
for category in categories:
|
except:
|
||||||
for tag in ccache.get(key, []):
|
fmt = None
|
||||||
if tag.original_name == category:
|
if fmts and fmt:
|
||||||
dbtags[category] = books_in_url(self.opts.url_prefix,
|
other_fmts = [x for x in fmts if x != fmt]
|
||||||
tag.category if tag.category else key,
|
data['formats'] = sorted(fmts)
|
||||||
tag.original_name if tag.id is None else
|
if fmt:
|
||||||
unicode(tag.id))
|
data['main_format'] = {fmt: absurl(self.opts.url_prefix, u'/get/%s/%d'%(fmt, book_id))}
|
||||||
break
|
else:
|
||||||
category_urls[key] = dbtags
|
data['main_format'] = None
|
||||||
|
data['other_formats'] = {fmt: absurl(self.opts.url_prefix, u'/get/%s/%d'%(fmt, book_id)) for fmt
|
||||||
|
in other_fmts}
|
||||||
|
|
||||||
|
if get_category_urls:
|
||||||
|
category_urls = data['category_urls'] = {}
|
||||||
|
ccache = self.categories_cache()
|
||||||
|
for key in mi.all_field_keys():
|
||||||
|
fm = mi.metadata_for_field(key)
|
||||||
|
if (fm and fm['is_category'] and not fm['is_csp'] and
|
||||||
|
key != 'formats' and fm['datatype'] not in ['rating']):
|
||||||
|
categories = mi.get(key)
|
||||||
|
if isinstance(categories, basestring):
|
||||||
|
categories = [categories]
|
||||||
|
if categories is None:
|
||||||
|
categories = []
|
||||||
|
dbtags = {}
|
||||||
|
for category in categories:
|
||||||
|
for tag in ccache.get(key, []):
|
||||||
|
if tag.original_name == category:
|
||||||
|
dbtags[category] = books_in_url(self.opts.url_prefix,
|
||||||
|
tag.category if tag.category else key,
|
||||||
|
tag.original_name if tag.id is None else
|
||||||
|
unicode(tag.id))
|
||||||
|
break
|
||||||
|
category_urls[key] = dbtags
|
||||||
|
else:
|
||||||
|
series = data.get('series', None)
|
||||||
|
if series:
|
||||||
|
tsorder = tweaks['save_template_title_series_sorting']
|
||||||
|
series = title_sort(series, order=tsorder)
|
||||||
|
else:
|
||||||
|
series = ''
|
||||||
|
data['_series_sort_'] = series
|
||||||
|
|
||||||
return data, mi.last_modified
|
return data, mi.last_modified
|
||||||
|
|
||||||
@Endpoint(set_last_modified=False)
|
@Endpoint(set_last_modified=False)
|
||||||
def ajax_book(self, book_id, category_urls='true', id_is_uuid='false'):
|
def ajax_book(self, book_id, category_urls='true', id_is_uuid='false',
|
||||||
|
device_compatible='false'):
|
||||||
'''
|
'''
|
||||||
Return the metadata of the book as a JSON dictionary.
|
Return the metadata of the book as a JSON dictionary.
|
||||||
|
|
||||||
@ -197,7 +213,8 @@ class AjaxServer(object):
|
|||||||
else:
|
else:
|
||||||
book_id = int(book_id)
|
book_id = int(book_id)
|
||||||
data, last_modified = self.ajax_book_to_json(book_id,
|
data, last_modified = self.ajax_book_to_json(book_id,
|
||||||
get_category_urls=category_urls.lower()=='true')
|
get_category_urls=category_urls.lower()=='true',
|
||||||
|
device_compatible=device_compatible.lower()=='true');
|
||||||
except:
|
except:
|
||||||
raise cherrypy.HTTPError(404, 'No book with id: %r'%book_id)
|
raise cherrypy.HTTPError(404, 'No book with id: %r'%book_id)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user