mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Google metadata plugin: When searching by ISBN if no results are found retry using an alternate query syntax. Fixes #1531 (Google books metadata plugin: Workaround for API bug)
This commit is contained in:
parent
85a7df7daa
commit
f7452f5211
@ -176,11 +176,11 @@ def to_metadata(browser, log, entry_, timeout): # {{{
|
|||||||
class GoogleBooks(Source):
|
class GoogleBooks(Source):
|
||||||
|
|
||||||
name = 'Google'
|
name = 'Google'
|
||||||
version = (1, 0, 2)
|
version = (1, 0, 3)
|
||||||
minimum_calibre_version = (2, 80, 0)
|
minimum_calibre_version = (2, 80, 0)
|
||||||
description = _('Downloads metadata and covers from Google Books')
|
description = _('Downloads metadata and covers from Google Books')
|
||||||
|
|
||||||
capabilities = frozenset({'identify', 'cover'})
|
capabilities = frozenset({'identify'})
|
||||||
touched_fields = frozenset({
|
touched_fields = frozenset({
|
||||||
'title', 'authors', 'tags', 'pubdate', 'comments', 'publisher',
|
'title', 'authors', 'tags', 'pubdate', 'comments', 'publisher',
|
||||||
'identifier:isbn', 'identifier:google', 'languages'
|
'identifier:isbn', 'identifier:google', 'languages'
|
||||||
@ -201,7 +201,7 @@ class GoogleBooks(Source):
|
|||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
def create_query(self, log, title=None, authors=None, identifiers={}): # {{{
|
def create_query(self, title=None, authors=None, identifiers={}, capitalize_isbn=False): # {{{
|
||||||
try:
|
try:
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@ -210,7 +210,7 @@ class GoogleBooks(Source):
|
|||||||
isbn = check_isbn(identifiers.get('isbn', None))
|
isbn = check_isbn(identifiers.get('isbn', None))
|
||||||
q = ''
|
q = ''
|
||||||
if isbn is not None:
|
if isbn is not None:
|
||||||
q += 'isbn:' + isbn
|
q += ('ISBN:' if capitalize_isbn else 'isbn:') + isbn
|
||||||
elif title or authors:
|
elif title or authors:
|
||||||
|
|
||||||
def build_term(prefix, parts):
|
def build_term(prefix, parts):
|
||||||
@ -360,28 +360,39 @@ class GoogleBooks(Source):
|
|||||||
entry = XPath('//atom:entry')
|
entry = XPath('//atom:entry')
|
||||||
|
|
||||||
query = self.create_query(
|
query = self.create_query(
|
||||||
log, title=title, authors=authors, identifiers=identifiers
|
title=title, authors=authors, identifiers=identifiers
|
||||||
)
|
)
|
||||||
if not query:
|
if not query:
|
||||||
log.error('Insufficient metadata to construct query')
|
log.error('Insufficient metadata to construct query')
|
||||||
return
|
return
|
||||||
|
alternate_query = self.create_query(title=title, authors=authors, identifiers=identifiers, capitalize_isbn=True)
|
||||||
br = self.browser
|
br = self.browser
|
||||||
log('Making query:', query)
|
|
||||||
try:
|
|
||||||
raw = br.open_novisit(query, timeout=timeout).read()
|
|
||||||
except Exception as e:
|
|
||||||
log.exception('Failed to make identify query: %r' % query)
|
|
||||||
return as_unicode(e)
|
|
||||||
|
|
||||||
try:
|
def make_query(query):
|
||||||
feed = etree.fromstring(
|
log('Making query:', query)
|
||||||
xml_to_unicode(clean_ascii_chars(raw), strip_encoding_pats=True)[0],
|
try:
|
||||||
parser=etree.XMLParser(recover=True, no_network=True, resolve_entities=False)
|
raw = br.open_novisit(query, timeout=timeout).read()
|
||||||
)
|
except Exception as e:
|
||||||
entries = entry(feed)
|
log.exception('Failed to make identify query: %r' % query)
|
||||||
except Exception as e:
|
return False, as_unicode(e)
|
||||||
log.exception('Failed to parse identify results')
|
|
||||||
return as_unicode(e)
|
try:
|
||||||
|
feed = etree.fromstring(
|
||||||
|
xml_to_unicode(clean_ascii_chars(raw), strip_encoding_pats=True)[0],
|
||||||
|
parser=etree.XMLParser(recover=True, no_network=True, resolve_entities=False)
|
||||||
|
)
|
||||||
|
return True, entry(feed)
|
||||||
|
except Exception as e:
|
||||||
|
log.exception('Failed to parse identify results')
|
||||||
|
return False, as_unicode(e)
|
||||||
|
ok, entries = make_query(query)
|
||||||
|
if not ok:
|
||||||
|
return entries
|
||||||
|
if not entries and alternate_query != query and not abort.is_set():
|
||||||
|
log('No results found, retrying with capitalized ISBN')
|
||||||
|
ok, entries = make_query(alternate_query)
|
||||||
|
if not ok:
|
||||||
|
return entries
|
||||||
|
|
||||||
if not entries and title and not abort.is_set():
|
if not entries and title and not abort.is_set():
|
||||||
if identifiers:
|
if identifiers:
|
||||||
@ -419,7 +430,19 @@ if __name__ == '__main__': # tests {{{
|
|||||||
from calibre.ebooks.metadata.sources.test import (
|
from calibre.ebooks.metadata.sources.test import (
|
||||||
test_identify_plugin, title_test, authors_test
|
test_identify_plugin, title_test, authors_test
|
||||||
)
|
)
|
||||||
tests = [({
|
tests = [
|
||||||
|
({
|
||||||
|
'identifiers': {
|
||||||
|
'isbn': '978-0-7869-5437-7' # needs capitalized ISBN to find results
|
||||||
|
},
|
||||||
|
'title': 'Dragons of Autumn Twilight',
|
||||||
|
'authors': ['Margaret Weis', 'Tracy Hickman']
|
||||||
|
}, [
|
||||||
|
title_test('The great gatsby', exact=True),
|
||||||
|
authors_test(['F. Scott Fitzgerald'])
|
||||||
|
]),
|
||||||
|
|
||||||
|
({
|
||||||
'identifiers': {
|
'identifiers': {
|
||||||
'isbn': '0743273567'
|
'isbn': '0743273567'
|
||||||
},
|
},
|
||||||
@ -428,14 +451,18 @@ if __name__ == '__main__': # tests {{{
|
|||||||
}, [
|
}, [
|
||||||
title_test('The great gatsby', exact=True),
|
title_test('The great gatsby', exact=True),
|
||||||
authors_test(['F. Scott Fitzgerald'])
|
authors_test(['F. Scott Fitzgerald'])
|
||||||
]), ({
|
]),
|
||||||
|
({
|
||||||
'title': 'Flatland',
|
'title': 'Flatland',
|
||||||
'authors': ['Abbott']
|
'authors': ['Abbott']
|
||||||
}, [title_test('Flatland', exact=False)]), ({
|
}, [title_test('Flatland', exact=False)]),
|
||||||
|
({
|
||||||
'title':
|
'title':
|
||||||
'The Blood Red Indian Summer: A Berger and Mitry Mystery',
|
'The Blood Red Indian Summer: A Berger and Mitry Mystery',
|
||||||
'authors': ['David Handler'],
|
'authors': ['David Handler'],
|
||||||
}, [title_test('The Blood Red Indian Summer: A Berger and Mitry Mystery')])]
|
}, [title_test('The Blood Red Indian Summer: A Berger and Mitry Mystery')
|
||||||
|
])
|
||||||
|
]
|
||||||
test_identify_plugin(GoogleBooks.name, tests[:])
|
test_identify_plugin(GoogleBooks.name, tests[:])
|
||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user