Fetch book summary alongwith the rest of the metadata

This commit is contained in:
Kovid Goyal 2008-03-10 17:35:57 +00:00
parent f5e86cc2a8
commit 27a2b7e61d
4 changed files with 40 additions and 2 deletions

View File

@ -23,7 +23,7 @@ from libprs500 import setup_cli_handlers, OptionParser
from libprs500.ebooks.metadata import MetaInformation
from libprs500.ebooks.BeautifulSoup import BeautifulStoneSoup
BASE_URL = 'http://isbndb.com/api/books.xml?access_key=%(key)s&page_number=1&results=subjects,authors&'
BASE_URL = 'http://isbndb.com/api/books.xml?access_key=%(key)s&page_number=1&results=subjects,authors,texts&'
class ISBNDBError(Exception):
pass
@ -56,6 +56,7 @@ def fetch_metadata(url, max=100, timeout=5.):
class ISBNDBMetadata(MetaInformation):
def __init__(self, book):
MetaInformation.__init__(self, None, [])
@ -77,6 +78,11 @@ class ISBNDBMetadata(MetaInformation):
pass
self.publisher = book.find('publishertext').string
summ = book.find('summary')
if summ and hasattr(summ, 'string') and summ.string:
self.comments = 'SUMMARY:\n'+summ.string
def build_isbn(base_url, opts):
return base_url + 'index1=isbn&value1='+opts.isbn

View File

@ -30,6 +30,7 @@ class Matches(QAbstractTableModel):
def __init__(self, matches):
self.matches = matches
self.matches.sort(cmp=lambda b, a: cmp(len(a.comments if a.comments else ''), len(b.comments if b.comments else '')))
QAbstractTableModel.__init__(self)
def rowCount(self, *args):
@ -53,6 +54,9 @@ class Matches(QAbstractTableModel):
else:
return QVariant(section+1)
def summary(self, row):
return self.matches[row].comments
def data(self, index, role):
row, col = index.row(), index.column()
if role == Qt.DisplayRole:
@ -91,6 +95,16 @@ class FetchMetadata(QDialog, Ui_FetchMetadata):
self.title = title
self.author = author.strip()
self.publisher = publisher
self.previous_row = None
def show_summary(self, current, previous):
row = current.row()
if row != self.previous_row:
summ = self.model.summary(row)
self.summary.setText(summ if summ else '')
self.previous_row = row
def fetch_metadata(self):
key = str(self.key.text())
@ -132,6 +146,8 @@ class FetchMetadata(QDialog, Ui_FetchMetadata):
self.model = Matches(books)
self.matches.setModel(self.model)
QObject.connect(self.matches.selectionModel(), SIGNAL('currentRowChanged(QModelIndex, QModelIndex)'),
self.show_summary)
self.model.reset()
self.matches.selectionModel().select(self.model.index(0, 0),
QItemSelectionModel.Select | QItemSelectionModel.Rows)
@ -141,6 +157,7 @@ class FetchMetadata(QDialog, Ui_FetchMetadata):
self.matches.resizeColumnsToContents()
def selected_book(self):
try:
return self.matches.model().matches[self.matches.currentIndex().row()]

View File

@ -9,7 +9,7 @@
<x>0</x>
<y>0</y>
<width>830</width>
<height>622</height>
<height>700</height>
</rect>
</property>
<property name="windowTitle" >
@ -81,6 +81,12 @@
</item>
<item>
<widget class="QTableView" name="matches" >
<property name="sizePolicy" >
<sizepolicy vsizetype="Expanding" hsizetype="Expanding" >
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="alternatingRowColors" >
<bool>true</bool>
</property>
@ -92,6 +98,9 @@
</property>
</widget>
</item>
<item>
<widget class="QTextBrowser" name="summary" />
</item>
</layout>
</widget>
</item>

View File

@ -287,6 +287,12 @@ class MetadataSingleDialog(QDialog, Ui_MetadataSingleDialog):
if book.author_sort: self.author_sort.setText(book.author_sort)
self.publisher.setText(book.publisher)
self.isbn.setText(book.isbn)
summ = book.comments
if summ:
prefix = qstring_to_unicode(self.comments.text())
if prefix:
prefix += '\n'
self.comments.setText(prefix + summ)
else:
error_dialog(self, 'Cannot fetch metadata', 'You must specify at least one of ISBN, Title, Authors or Publisher')