Add by ISBN: Allow adding using identifiers other than ISBN as well. Fixes #2003227 [enhancement request regarding add from isbn](https://bugs.launchpad.net/calibre/+bug/2003227)

This commit is contained in:
Kovid Goyal 2023-01-18 21:58:57 +05:30
parent bd0b511b81
commit b67c5a2104
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 45 additions and 18 deletions

View File

@ -371,14 +371,26 @@ class AddAction(InterfaceAction):
existing_isbns.pop('', None)
ok = []
duplicates = []
checker_maps = {}
for book in books:
if 'isbn' in book:
q = normalize_isbn(book['isbn'])
if q and q in existing_isbns:
duplicates.append((book, existing_isbns[q]))
else:
ok.append(book)
else:
key = book['']
if key not in checker_maps:
checker_maps[key] = {ids.get(key, ''): book_id for book_id, ids in book_id_identifiers.items()}
checker_maps[key].pop('', None)
q = book[key]
if q in checker_maps[key]:
duplicates.append((book, checker_maps[key][q]))
else:
ok.append(book)
if duplicates:
det_msg = '\n'.join(f'{book["isbn"]}: {db.field_for("title", book_id)}' for book, book_id in duplicates)
det_msg = '\n'.join(f'{book[book[""]]}: {db.field_for("title", book_id)}' for book, book_id in duplicates)
if question_dialog(self.gui, _('Duplicates found'), _(
'Books with some of the specified ISBNs already exist in the calibre library.'
' Click "Show details" for the full list. Do you want to add them anyway?'), det_msg=det_msg
@ -416,7 +428,10 @@ class AddAction(InterfaceAction):
return
mi = MetaInformation(None)
if x[''] == 'isbn':
mi.isbn = x['isbn']
else:
mi.set_identifiers({x['']:x[x['']]})
if self.isbn_add_tags:
mi.tags = list(self.isbn_add_tags)
fmts = [] if x['path'] is None else [x['path']]

View File

@ -55,7 +55,11 @@ class AddFromISBN(QDialog):
"<p>Any invalid ISBNs in the list will be ignored.</p>\n"
"<p>You can also specify a file that will be added with each ISBN. To do this enter the full"
" path to the file after a <code>&gt;&gt;</code>. For example:</p>\n"
"<p><code>9788842915232 &gt;&gt; %s</code></p>"), self)
"<p><code>9788842915232 &gt;&gt; %s</code></p>"
"<p>To use identifiers other than ISBN use key:value syntax, For example:</p>\n"
"<p><code>amazon:B001JK9C72</code></p>"
), self)
l.addWidget(la), la.setWordWrap(True)
l.addSpacing(20)
self.la2 = la = QLabel(_("&Tags to set on created book entries:"), self)
@ -100,18 +104,26 @@ class AddFromISBN(QDialog):
parts = [x.strip() for x in parts]
if not parts[0]:
continue
if ':' in parts[0]:
prefix, val = parts[0].partition(':')[::2]
else:
prefix, val = 'isbn', parts[0]
path = None
if len(parts) > 1 and parts[1] and os.access(parts[1], os.R_OK) and os.path.isfile(parts[1]):
path = parts[1]
if prefix == 'isbn':
isbn = check_isbn(parts[0])
if isbn is not None:
isbn = isbn.upper()
if isbn not in self.isbns:
self.isbns.append(isbn)
book = {'isbn': isbn, 'path': None}
if len(parts) > 1 and parts[1] and \
os.access(parts[1], os.R_OK) and os.path.isfile(parts[1]):
book['path'] = parts[1]
self.books.append(book)
self.books.append({'isbn': isbn, 'path': path, '': 'isbn'})
else:
bad.add(parts[0])
else:
if prefix != 'path':
self.books.append({prefix: val, 'path': path, '':prefix})
if bad:
if self.books:
if not question_dialog(self, _('Some invalid ISBNs'),