mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #856076 (Metadata download when adding books via ISBN)
This commit is contained in:
parent
a893ed6fca
commit
24562cc6a4
@ -22,7 +22,6 @@ from calibre.constants import preferred_encoding, filesystem_encoding
|
|||||||
from calibre.gui2.actions import InterfaceAction
|
from calibre.gui2.actions import InterfaceAction
|
||||||
from calibre.gui2 import question_dialog
|
from calibre.gui2 import question_dialog
|
||||||
from calibre.ebooks.metadata import MetaInformation
|
from calibre.ebooks.metadata import MetaInformation
|
||||||
from calibre.ebooks.metadata.sources.base import msprefs
|
|
||||||
|
|
||||||
def get_filters():
|
def get_filters():
|
||||||
return [
|
return [
|
||||||
@ -181,17 +180,9 @@ class AddAction(InterfaceAction):
|
|||||||
except IndexError:
|
except IndexError:
|
||||||
self.gui.library_view.model().books_added(self.isbn_add_dialog.value)
|
self.gui.library_view.model().books_added(self.isbn_add_dialog.value)
|
||||||
self.isbn_add_dialog.accept()
|
self.isbn_add_dialog.accept()
|
||||||
orig = msprefs['ignore_fields']
|
self.gui.iactions['Edit Metadata'].download_metadata(
|
||||||
new = list(orig)
|
ids=self.add_by_isbn_ids, ensure_fields=frozenset(['title',
|
||||||
for x in ('title', 'authors'):
|
'authors']))
|
||||||
if x in new:
|
|
||||||
new.remove(x)
|
|
||||||
msprefs['ignore_fields'] = new
|
|
||||||
try:
|
|
||||||
self.gui.iactions['Edit Metadata'].download_metadata(
|
|
||||||
ids=self.add_by_isbn_ids)
|
|
||||||
finally:
|
|
||||||
msprefs['ignore_fields'] = orig
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ class EditMetadataAction(InterfaceAction):
|
|||||||
self.action_merge.setEnabled(enabled)
|
self.action_merge.setEnabled(enabled)
|
||||||
|
|
||||||
# Download metadata {{{
|
# Download metadata {{{
|
||||||
def download_metadata(self, ids=None):
|
def download_metadata(self, ids=None, ensure_fields=None):
|
||||||
if ids is None:
|
if ids is None:
|
||||||
rows = self.gui.library_view.selectionModel().selectedRows()
|
rows = self.gui.library_view.selectionModel().selectedRows()
|
||||||
if not rows or len(rows) == 0:
|
if not rows or len(rows) == 0:
|
||||||
@ -76,7 +76,8 @@ class EditMetadataAction(InterfaceAction):
|
|||||||
ids = [db.id(row.row()) for row in rows]
|
ids = [db.id(row.row()) for row in rows]
|
||||||
from calibre.gui2.metadata.bulk_download import start_download
|
from calibre.gui2.metadata.bulk_download import start_download
|
||||||
start_download(self.gui, ids,
|
start_download(self.gui, ids,
|
||||||
Dispatcher(self.metadata_downloaded))
|
Dispatcher(self.metadata_downloaded),
|
||||||
|
ensure_fields=ensure_fields)
|
||||||
|
|
||||||
def metadata_downloaded(self, job):
|
def metadata_downloaded(self, job):
|
||||||
if job.failed:
|
if job.failed:
|
||||||
|
@ -98,7 +98,7 @@ def split_jobs(ids, batch_size=100):
|
|||||||
ids = ids[batch_size:]
|
ids = ids[batch_size:]
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
def start_download(gui, ids, callback):
|
def start_download(gui, ids, callback, ensure_fields=None):
|
||||||
d = ConfirmDialog(ids, gui)
|
d = ConfirmDialog(ids, gui)
|
||||||
ret = d.exec_()
|
ret = d.exec_()
|
||||||
d.b.clicked.disconnect()
|
d.b.clicked.disconnect()
|
||||||
@ -108,7 +108,8 @@ def start_download(gui, ids, callback):
|
|||||||
for batch in split_jobs(ids):
|
for batch in split_jobs(ids):
|
||||||
job = ThreadedJob('metadata bulk download',
|
job = ThreadedJob('metadata bulk download',
|
||||||
_('Download metadata for %d books')%len(batch),
|
_('Download metadata for %d books')%len(batch),
|
||||||
download, (batch, gui.current_db, d.identify, d.covers), {}, callback)
|
download, (batch, gui.current_db, d.identify, d.covers,
|
||||||
|
ensure_fields), {}, callback)
|
||||||
gui.job_manager.run_threaded_job(job)
|
gui.job_manager.run_threaded_job(job)
|
||||||
gui.status_bar.show_message(_('Metadata download started'), 3000)
|
gui.status_bar.show_message(_('Metadata download started'), 3000)
|
||||||
|
|
||||||
@ -127,10 +128,10 @@ def get_job_details(job):
|
|||||||
det_msg = '\n'.join(det_msg)
|
det_msg = '\n'.join(det_msg)
|
||||||
return id_map, failed_ids, failed_covers, all_failed, det_msg
|
return id_map, failed_ids, failed_covers, all_failed, det_msg
|
||||||
|
|
||||||
def merge_result(oldmi, newmi):
|
def merge_result(oldmi, newmi, ensure_fields=None):
|
||||||
dummy = Metadata(_('Unknown'))
|
dummy = Metadata(_('Unknown'))
|
||||||
for f in msprefs['ignore_fields']:
|
for f in msprefs['ignore_fields']:
|
||||||
if ':' not in f:
|
if ':' not in f and (ensure_fields and f not in ensure_fields):
|
||||||
setattr(newmi, f, getattr(dummy, f))
|
setattr(newmi, f, getattr(dummy, f))
|
||||||
fields = set()
|
fields = set()
|
||||||
for plugin in metadata_plugins(['identify']):
|
for plugin in metadata_plugins(['identify']):
|
||||||
@ -154,7 +155,7 @@ def merge_result(oldmi, newmi):
|
|||||||
|
|
||||||
return newmi
|
return newmi
|
||||||
|
|
||||||
def download(ids, db, do_identify, covers,
|
def download(ids, db, do_identify, covers, ensure_fields,
|
||||||
log=None, abort=None, notifications=None):
|
log=None, abort=None, notifications=None):
|
||||||
ids = list(ids)
|
ids = list(ids)
|
||||||
metadata = [db.get_metadata(i, index_is_id=True, get_user_categories=False)
|
metadata = [db.get_metadata(i, index_is_id=True, get_user_categories=False)
|
||||||
@ -184,7 +185,7 @@ def download(ids, db, do_identify, covers,
|
|||||||
pass
|
pass
|
||||||
if results:
|
if results:
|
||||||
all_failed = False
|
all_failed = False
|
||||||
mi = merge_result(mi, results[0])
|
mi = merge_result(mi, results[0], ensure_fields=ensure_fields)
|
||||||
identifiers = mi.identifiers
|
identifiers = mi.identifiers
|
||||||
if not mi.is_null('rating'):
|
if not mi.is_null('rating'):
|
||||||
# set_metadata expects a rating out of 10
|
# set_metadata expects a rating out of 10
|
||||||
@ -193,7 +194,7 @@ def download(ids, db, do_identify, covers,
|
|||||||
log.error('Failed to download metadata for', title)
|
log.error('Failed to download metadata for', title)
|
||||||
failed_ids.add(i)
|
failed_ids.add(i)
|
||||||
# We don't want set_metadata operating on anything but covers
|
# We don't want set_metadata operating on anything but covers
|
||||||
mi = merge_result(mi, mi)
|
mi = merge_result(mi, mi, ensure_fields=ensure_fields)
|
||||||
if covers:
|
if covers:
|
||||||
cdata = download_cover(log, title=title, authors=authors,
|
cdata = download_cover(log, title=title, authors=authors,
|
||||||
identifiers=identifiers)
|
identifiers=identifiers)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user