Prevent google search spelling correction when search terms include ISBN/ASIN

This commit is contained in:
Kovid Goyal 2024-01-03 20:07:44 +05:30
parent 5749cc02a5
commit dd33bd079a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -351,7 +351,21 @@ def google_specialize_browser(br):
return br return br
def is_probably_book_asin(t):
return t and len(t) == 10 and t.startswith('B') and t.upper() == t
def is_asin_or_isbn(t):
from calibre.ebooks.metadata import check_isbn
return bool(check_isbn(t) or is_probably_book_asin(t))
def google_format_query(terms, site=None, tbm=None): def google_format_query(terms, site=None, tbm=None):
prevent_spelling_correction = False
for t in terms:
if is_asin_or_isbn(t):
prevent_spelling_correction = True
break
terms = [quote_term(google_term(t)) for t in terms] terms = [quote_term(google_term(t)) for t in terms]
if site is not None: if site is not None:
terms.append(quote_term(('site:' + site))) terms.append(quote_term(('site:' + site)))
@ -359,6 +373,8 @@ def google_format_query(terms, site=None, tbm=None):
url = 'https://www.google.com/search?q={q}'.format(q=q) url = 'https://www.google.com/search?q={q}'.format(q=q)
if tbm: if tbm:
url += '&tbm=' + tbm url += '&tbm=' + tbm
if prevent_spelling_correction:
url += '&nfpr=1'
return url return url