Implement #7400 (Mass importing by ISBN)

This commit is contained in:
Kovid Goyal 2010-11-26 09:46:07 -07:00
parent c996a001a7
commit 8fc411ade6
3 changed files with 46 additions and 14 deletions

View File

@ -89,14 +89,18 @@ class AddAction(InterfaceAction):
self.gui.library_view.model().db.import_book(MetaInformation(None), [])
self.gui.library_view.model().books_added(num)
def add_isbns(self, isbns):
def add_isbns(self, books):
from calibre.ebooks.metadata import MetaInformation
ids = set([])
for x in isbns:
for x in books:
mi = MetaInformation(None)
mi.isbn = x
ids.add(self.gui.library_view.model().db.import_book(mi, []))
self.gui.library_view.model().books_added(len(isbns))
mi.isbn = x['isbn']
if x['path'] is not None:
ids.add(self.gui.library_view.model().db.import_book(mi, [x['path']]))
os.remove(x['path'])
else:
ids.add(self.gui.library_view.model().db.import_book(mi, []))
self.gui.library_view.model().books_added(len(books))
self.gui.iactions['Edit Metadata'].do_download_metadata(ids)
@ -150,7 +154,7 @@ class AddAction(InterfaceAction):
from calibre.gui2.dialogs.add_from_isbn import AddFromISBN
d = AddFromISBN(self.gui)
if d.exec_() == d.Accepted:
self.add_isbns(d.isbns)
self.add_isbns(d.books)
def add_books(self, *args):
'''

View File

@ -5,10 +5,14 @@ __license__ = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import os
from PyQt4.Qt import QDialog, QApplication
from calibre.gui2.dialogs.add_from_isbn_ui import Ui_Dialog
from calibre.ebooks.metadata import check_isbn
from calibre.constants import iswindows
from calibre.ptempfile import PersistentTemporaryFile
class AddFromISBN(QDialog, Ui_Dialog):
@ -16,7 +20,12 @@ class AddFromISBN(QDialog, Ui_Dialog):
QDialog.__init__(self, parent)
self.setupUi(self)
path = r'C:\Users\kovid\e-books\some_book.epub' if iswindows else \
'/Users/kovid/e-books/some_book.epub'
self.label.setText(unicode(self.label.text())%path)
self.isbns = []
self.books = []
self.paste_button.clicked.connect(self.paste)
def paste(self, *args):
@ -30,11 +39,27 @@ class AddFromISBN(QDialog, Ui_Dialog):
def accept(self, *args):
for line in unicode(self.isbn_box.toPlainText()).strip().splitlines():
if line:
isbn = check_isbn(line)
if isbn is not None:
isbn = isbn.upper()
if isbn not in self.isbns:
self.isbns.append(isbn)
line = line.strip()
if not line:
continue
parts = line.split('>>')
if len(parts) > 2:
parts = [parts[0] + '>>'.join(parts[1:])]
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]):
pt = PersistentTemporaryFile(os.path.splitext(parts[1])[1])
pt.write(open(parts[1], 'rb').read())
pt.close()
book['path'] = pt.name
self.books.append(book)
QDialog.accept(self, *args)

View File

@ -24,7 +24,10 @@
<item row="0" column="1">
<widget class="QLabel" name="label">
<property name="text">
<string>&lt;p&gt;Enter a list of ISBNs in the box to the left, one per line. calibre will automatically create entries for books based on the ISBN and download metadata and covers for them.&lt;p&gt;Any invalid ISBNs in the list will be ignored.</string>
<string>&lt;p&gt;Enter a list of ISBNs in the box to the left, one per line. calibre will automatically create entries for books based on the ISBN and download metadata and covers for them.&lt;/p&gt;
&lt;p&gt;Any invalid ISBNs in the list will be ignored.&lt;/p&gt;
&lt;p&gt;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 &lt;code&gt;&gt;&gt;&lt;/code&gt;. For example:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;9788842915232 &gt;&gt; %s&lt;/code&gt;&lt;/p&gt;</string>
</property>
<property name="wordWrap">
<bool>true</bool>