From a3e8c2560b1d00a40defd763c017c5c05f73e441 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 9 Apr 2011 17:43:33 -0600 Subject: [PATCH 01/20] Identifiers to URLs --- src/calibre/customize/ui.py | 9 ++++-- src/calibre/ebooks/metadata/sources/amazon.py | 8 +++++ src/calibre/ebooks/metadata/sources/base.py | 7 ++++ src/calibre/ebooks/metadata/sources/google.py | 6 ++++ .../ebooks/metadata/sources/identify.py | 14 +++++++- src/calibre/gui2/metadata/single_download.py | 32 +++++++++++++++---- 6 files changed, 66 insertions(+), 10 deletions(-) diff --git a/src/calibre/customize/ui.py b/src/calibre/customize/ui.py index 9c8f80544b..b8abbf9b03 100644 --- a/src/calibre/customize/ui.py +++ b/src/calibre/customize/ui.py @@ -453,12 +453,15 @@ def epub_fixers(): # Metadata sources2 {{{ def metadata_plugins(capabilities): capabilities = frozenset(capabilities) - for plugin in _initialized_plugins: - if isinstance(plugin, Source) and \ - plugin.capabilities.intersection(capabilities) and \ + for plugin in all_metadata_plugins(): + if plugin.capabilities.intersection(capabilities) and \ not is_disabled(plugin): yield plugin +def all_metadata_plugins(): + for plugin in _initialized_plugins: + if isinstance(plugin, Source): + yield plugin # }}} # Initialize plugins {{{ diff --git a/src/calibre/ebooks/metadata/sources/amazon.py b/src/calibre/ebooks/metadata/sources/amazon.py index 4722873f77..5262ee0d1d 100644 --- a/src/calibre/ebooks/metadata/sources/amazon.py +++ b/src/calibre/ebooks/metadata/sources/amazon.py @@ -295,6 +295,14 @@ class Amazon(Source): 'uk' : _('UK'), } + def get_book_url(self, identifiers): # {{{ + asin = identifiers.get('amazon', None) + if asin is None: + asin = identifiers.get('asin', None) + if asin: + return 'http://amzn.com/%s'%asin + # }}} + def create_query(self, log, title=None, authors=None, identifiers={}): # {{{ domain = self.prefs.get('domain', 'com') diff --git a/src/calibre/ebooks/metadata/sources/base.py b/src/calibre/ebooks/metadata/sources/base.py index d4e090084c..7c7d51077b 100644 --- a/src/calibre/ebooks/metadata/sources/base.py +++ b/src/calibre/ebooks/metadata/sources/base.py @@ -301,6 +301,13 @@ class Source(Plugin): # Metadata API {{{ + def get_book_url(self, identifiers): + ''' + Return the URL for the book identified by identifiers at this source. + If no URL is found, return None. + ''' + return None + def get_cached_cover_url(self, identifiers): ''' Return cached cover URL for the book identified by diff --git a/src/calibre/ebooks/metadata/sources/google.py b/src/calibre/ebooks/metadata/sources/google.py index 47cfb823bb..c3286700f2 100644 --- a/src/calibre/ebooks/metadata/sources/google.py +++ b/src/calibre/ebooks/metadata/sources/google.py @@ -167,6 +167,12 @@ class GoogleBooks(Source): GOOGLE_COVER = 'http://books.google.com/books?id=%s&printsec=frontcover&img=1' + def get_book_url(self, identifiers): # {{{ + goog = identifiers.get('google', None) + if goog is not None: + return 'http://books.google.com/books?id=%s'%goog + # }}} + def create_query(self, log, title=None, authors=None, identifiers={}): # {{{ BASE_URL = 'http://books.google.com/books/feeds/volumes?' isbn = check_isbn(identifiers.get('isbn', None)) diff --git a/src/calibre/ebooks/metadata/sources/identify.py b/src/calibre/ebooks/metadata/sources/identify.py index 85549904e7..6775de01a6 100644 --- a/src/calibre/ebooks/metadata/sources/identify.py +++ b/src/calibre/ebooks/metadata/sources/identify.py @@ -14,7 +14,7 @@ from threading import Thread from io import BytesIO from operator import attrgetter -from calibre.customize.ui import metadata_plugins +from calibre.customize.ui import metadata_plugins, all_metadata_plugins from calibre.ebooks.metadata.sources.base import create_log, msprefs from calibre.ebooks.metadata.xisbn import xisbn from calibre.ebooks.metadata.book.base import Metadata @@ -366,6 +366,18 @@ def identify(log, abort, # {{{ return results # }}} +def urls_from_identifiers(identifiers): # {{{ + ans = [] + for plugin in all_metadata_plugins(): + try: + url = plugin.get_book_url(identifiers) + if url is not None: + ans.append((plugin.name, url)) + except: + pass + return ans +# }}} + if __name__ == '__main__': # tests {{{ # To run these test use: calibre-debug -e # src/calibre/ebooks/metadata/sources/identify.py diff --git a/src/calibre/gui2/metadata/single_download.py b/src/calibre/gui2/metadata/single_download.py index 35c66340c6..cae6ede7b4 100644 --- a/src/calibre/gui2/metadata/single_download.py +++ b/src/calibre/gui2/metadata/single_download.py @@ -7,6 +7,9 @@ __license__ = 'GPL v3' __copyright__ = '2011, Kovid Goyal ' __docformat__ = 'restructuredtext en' +DEBUG_DIALOG = False + +# Imports {{{ from threading import Thread, Event from operator import attrgetter from Queue import Queue, Empty @@ -21,14 +24,14 @@ from PyQt4.QtWebKit import QWebView from calibre.customize.ui import metadata_plugins from calibre.ebooks.metadata import authors_to_string from calibre.utils.logging import GUILog as Log -from calibre.ebooks.metadata.sources.identify import identify +from calibre.ebooks.metadata.sources.identify import (identify, + urls_from_identifiers) from calibre.ebooks.metadata.book.base import Metadata from calibre.gui2 import error_dialog, NONE from calibre.utils.date import utcnow, fromordinal, format_date from calibre.library.comments import comments_to_html from calibre import force_unicode - -DEBUG_DIALOG = False +# }}} class RichTextDelegate(QStyledItemDelegate): # {{{ @@ -41,7 +44,11 @@ class RichTextDelegate(QStyledItemDelegate): # {{{ return doc def sizeHint(self, option, index): - ans = self.to_doc(index).size().toSize() + doc = self.to_doc(index) + ans = doc.size().toSize() + if ans.width() > 250: + doc.setTextWidth(250) + ans = doc.size().toSize() ans.setHeight(ans.height()+10) return ans @@ -234,6 +241,11 @@ class ResultsView(QTableView): # {{{ if not book.is_null('rating'): parts.append('
%s
'%('\u2605'*int(book.rating))) parts.append('') + if book.identifiers: + urls = urls_from_identifiers(book.identifiers) + ids = ['%s'%(url, name) for name, url in urls] + if ids: + parts.append('
%s: %s

'%(_('See at'), ', '.join(ids))) if book.tags: parts.append('
%s
\u00a0
'%', '.join(book.tags)) if book.comments: @@ -265,6 +277,14 @@ class Comments(QWebView): # {{{ self.page().setPalette(palette) self.setAttribute(Qt.WA_OpaquePaintEvent, False) + self.page().setLinkDelegationPolicy(self.page().DelegateAllLinks) + self.linkClicked.connect(self.link_clicked) + + def link_clicked(self, url): + from calibre.gui2 import open_url + if unicode(url.toString()).startswith('http://'): + open_url(url) + def turnoff_scrollbar(self, *args): self.page().mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff) @@ -382,7 +402,7 @@ class IdentifyWidget(QWidget): # {{{ self.query.setWordWrap(True) l.addWidget(self.query, 2, 0, 1, 2) - self.comments_view.show_data('

'+_('Downloading')+ + self.comments_view.show_data('

'+_('Please wait')+ '
.

'+ '''