Identifiers to URLs

This commit is contained in:
Kovid Goyal 2011-04-09 17:43:33 -06:00
parent eaf6060421
commit a3e8c2560b
6 changed files with 66 additions and 10 deletions

View File

@ -453,12 +453,15 @@ def epub_fixers():
# Metadata sources2 {{{ # Metadata sources2 {{{
def metadata_plugins(capabilities): def metadata_plugins(capabilities):
capabilities = frozenset(capabilities) capabilities = frozenset(capabilities)
for plugin in _initialized_plugins: for plugin in all_metadata_plugins():
if isinstance(plugin, Source) and \ if plugin.capabilities.intersection(capabilities) and \
plugin.capabilities.intersection(capabilities) and \
not is_disabled(plugin): not is_disabled(plugin):
yield plugin yield plugin
def all_metadata_plugins():
for plugin in _initialized_plugins:
if isinstance(plugin, Source):
yield plugin
# }}} # }}}
# Initialize plugins {{{ # Initialize plugins {{{

View File

@ -295,6 +295,14 @@ class Amazon(Source):
'uk' : _('UK'), '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={}): # {{{ def create_query(self, log, title=None, authors=None, identifiers={}): # {{{
domain = self.prefs.get('domain', 'com') domain = self.prefs.get('domain', 'com')

View File

@ -301,6 +301,13 @@ class Source(Plugin):
# Metadata API {{{ # 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): def get_cached_cover_url(self, identifiers):
''' '''
Return cached cover URL for the book identified by Return cached cover URL for the book identified by

View File

@ -167,6 +167,12 @@ class GoogleBooks(Source):
GOOGLE_COVER = 'http://books.google.com/books?id=%s&printsec=frontcover&img=1' 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={}): # {{{ def create_query(self, log, title=None, authors=None, identifiers={}): # {{{
BASE_URL = 'http://books.google.com/books/feeds/volumes?' BASE_URL = 'http://books.google.com/books/feeds/volumes?'
isbn = check_isbn(identifiers.get('isbn', None)) isbn = check_isbn(identifiers.get('isbn', None))

View File

@ -14,7 +14,7 @@ from threading import Thread
from io import BytesIO from io import BytesIO
from operator import attrgetter 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.sources.base import create_log, msprefs
from calibre.ebooks.metadata.xisbn import xisbn from calibre.ebooks.metadata.xisbn import xisbn
from calibre.ebooks.metadata.book.base import Metadata from calibre.ebooks.metadata.book.base import Metadata
@ -366,6 +366,18 @@ def identify(log, abort, # {{{
return results 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 {{{ if __name__ == '__main__': # tests {{{
# To run these test use: calibre-debug -e # To run these test use: calibre-debug -e
# src/calibre/ebooks/metadata/sources/identify.py # src/calibre/ebooks/metadata/sources/identify.py

View File

@ -7,6 +7,9 @@ __license__ = 'GPL v3'
__copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
DEBUG_DIALOG = False
# Imports {{{
from threading import Thread, Event from threading import Thread, Event
from operator import attrgetter from operator import attrgetter
from Queue import Queue, Empty from Queue import Queue, Empty
@ -21,14 +24,14 @@ from PyQt4.QtWebKit import QWebView
from calibre.customize.ui import metadata_plugins from calibre.customize.ui import metadata_plugins
from calibre.ebooks.metadata import authors_to_string from calibre.ebooks.metadata import authors_to_string
from calibre.utils.logging import GUILog as Log 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.ebooks.metadata.book.base import Metadata
from calibre.gui2 import error_dialog, NONE from calibre.gui2 import error_dialog, NONE
from calibre.utils.date import utcnow, fromordinal, format_date from calibre.utils.date import utcnow, fromordinal, format_date
from calibre.library.comments import comments_to_html from calibre.library.comments import comments_to_html
from calibre import force_unicode from calibre import force_unicode
# }}}
DEBUG_DIALOG = False
class RichTextDelegate(QStyledItemDelegate): # {{{ class RichTextDelegate(QStyledItemDelegate): # {{{
@ -41,7 +44,11 @@ class RichTextDelegate(QStyledItemDelegate): # {{{
return doc return doc
def sizeHint(self, option, index): 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) ans.setHeight(ans.height()+10)
return ans return ans
@ -234,6 +241,11 @@ class ResultsView(QTableView): # {{{
if not book.is_null('rating'): if not book.is_null('rating'):
parts.append('<div>%s</div>'%('\u2605'*int(book.rating))) parts.append('<div>%s</div>'%('\u2605'*int(book.rating)))
parts.append('</center>') parts.append('</center>')
if book.identifiers:
urls = urls_from_identifiers(book.identifiers)
ids = ['<a href="%s">%s</a>'%(url, name) for name, url in urls]
if ids:
parts.append('<div><b>%s:</b> %s</div><br>'%(_('See at'), ', '.join(ids)))
if book.tags: if book.tags:
parts.append('<div>%s</div><div>\u00a0</div>'%', '.join(book.tags)) parts.append('<div>%s</div><div>\u00a0</div>'%', '.join(book.tags))
if book.comments: if book.comments:
@ -265,6 +277,14 @@ class Comments(QWebView): # {{{
self.page().setPalette(palette) self.page().setPalette(palette)
self.setAttribute(Qt.WA_OpaquePaintEvent, False) 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): def turnoff_scrollbar(self, *args):
self.page().mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff) self.page().mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)
@ -382,7 +402,7 @@ class IdentifyWidget(QWidget): # {{{
self.query.setWordWrap(True) self.query.setWordWrap(True)
l.addWidget(self.query, 2, 0, 1, 2) l.addWidget(self.query, 2, 0, 1, 2)
self.comments_view.show_data('<h2>'+_('Downloading')+ self.comments_view.show_data('<h2>'+_('Please wait')+
'<br><span id="dots">.</span></h2>'+ '<br><span id="dots">.</span></h2>'+
''' '''
<script type="text/javascript"> <script type="text/javascript">
@ -848,7 +868,7 @@ class FullFetch(QDialog): # {{{
# }}} # }}}
if __name__ == '__main__': if __name__ == '__main__':
DEBUG_DIALOG = True #DEBUG_DIALOG = True
app = QApplication([]) app = QApplication([])
d = FullFetch(Log()) d = FullFetch(Log())
d.start(title='great gatsby', authors=['Fitzgerald']) d.start(title='great gatsby', authors=['Fitzgerald'])