From b67c5a21040f658ca7995344822042a4ec625d20 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 18 Jan 2023 21:58:57 +0530 Subject: [PATCH] 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) --- src/calibre/gui2/actions/add.py | 27 +++++++++++++---- src/calibre/gui2/dialogs/add_from_isbn.py | 36 +++++++++++++++-------- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/calibre/gui2/actions/add.py b/src/calibre/gui2/actions/add.py index d2bc9bcd53..fdc1e4ce07 100644 --- a/src/calibre/gui2/actions/add.py +++ b/src/calibre/gui2/actions/add.py @@ -371,14 +371,26 @@ class AddAction(InterfaceAction): existing_isbns.pop('', None) ok = [] duplicates = [] + checker_maps = {} for book in books: - q = normalize_isbn(book['isbn']) - if q and q in existing_isbns: - duplicates.append((book, existing_isbns[q])) + 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: - ok.append(book) + 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) - mi.isbn = x['isbn'] + 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']] diff --git a/src/calibre/gui2/dialogs/add_from_isbn.py b/src/calibre/gui2/dialogs/add_from_isbn.py index f72d37e4b3..83302f326a 100644 --- a/src/calibre/gui2/dialogs/add_from_isbn.py +++ b/src/calibre/gui2/dialogs/add_from_isbn.py @@ -55,7 +55,11 @@ class AddFromISBN(QDialog): "

Any invalid ISBNs in the list will be ignored.

\n" "

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 >>. For example:

\n" - "

9788842915232 >> %s

"), self) + "

9788842915232 >> %s

" + "

To use identifiers other than ISBN use key:value syntax, For example:

\n" + "

amazon:B001JK9C72

" + ), 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 - 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) + if ':' in parts[0]: + prefix, val = parts[0].partition(':')[::2] else: - bad.add(parts[0]) + 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) + 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'),