diff --git a/src/calibre/ebooks/metadata/sources/search_engines.py b/src/calibre/ebooks/metadata/sources/search_engines.py index 31375a663f..90eea8b7de 100644 --- a/src/calibre/ebooks/metadata/sources/search_engines.py +++ b/src/calibre/ebooks/metadata/sources/search_engines.py @@ -365,16 +365,26 @@ def google_parse_results(root, raw, log=prints, ignore_uncached=True): return ans +def google_consent_cookies(): + # See https://github.com/benbusby/whoogle-search/pull/1054 for cookies + from datetime import date + from base64 import standard_b64encode + base = {'domain': '.google.com', 'path': '/'} + b = base.copy() + b['name'], b['value'] = 'CONSENT', 'PENDING+987' + yield b + template = b'\x08\x01\x128\x08\x14\x12+boq_identityfrontenduiserver_20231107.05_p0\x1a\x05en-US \x03\x1a\x06\x08\x80\xf1\xca\xaa\x06' + template.replace(b'20231107', date.today().strftime('%Y%m%d').encode('ascii')) + b = base.copy() + b['name'], b['value'] = 'SOCS', standard_b64encode(template).decode('ascii').rstrip('=') + yield b + + def google_specialize_browser(br): with webcache_lock: if not hasattr(br, 'google_consent_cookie_added'): - # See https://github.com/benbusby/whoogle-search/pull/1054 for cookies - br.set_simple_cookie('CONSENT', 'PENDING+987', '.google.com', path='/') - template = b'\x08\x01\x128\x08\x14\x12+boq_identityfrontenduiserver_20231107.05_p0\x1a\x05en-US \x03\x1a\x06\x08\x80\xf1\xca\xaa\x06' - from datetime import date - from base64 import standard_b64encode - template.replace(b'20231107', date.today().strftime('%Y%m%d').encode('ascii')) - br.set_simple_cookie('SOCS', standard_b64encode(template).decode('ascii').rstrip('='), '.google.com', path='/') + for c in google_consent_cookies(): + br.set_simple_cookie(c['name'], c['value'], c['domain'], path=c['path']) br.google_consent_cookie_added = True return br diff --git a/src/calibre/gui2/viewer/lookup.py b/src/calibre/gui2/viewer/lookup.py index 1dff97ae92..0af86a2104 100644 --- a/src/calibre/gui2/viewer/lookup.py +++ b/src/calibre/gui2/viewer/lookup.py @@ -6,15 +6,17 @@ import sys import textwrap from functools import lru_cache from qt.core import ( - QAbstractItemView, QApplication, QCheckBox, QComboBox, QDialog, QDialogButtonBox, - QFormLayout, QHBoxLayout, QIcon, QLabel, QLineEdit, QListWidget, QListWidgetItem, - QPalette, QPushButton, QSize, Qt, QTimer, QUrl, QVBoxLayout, QWidget, pyqtSignal, + QAbstractItemView, QApplication, QCheckBox, QComboBox, QDateTime, QDialog, + QDialogButtonBox, QFormLayout, QHBoxLayout, QIcon, QLabel, QLineEdit, QListWidget, + QListWidgetItem, QNetworkCookie, QPalette, QPushButton, QSize, Qt, QTimer, QUrl, + QVBoxLayout, QWidget, pyqtSignal, ) from qt.webengine import ( QWebEnginePage, QWebEngineProfile, QWebEngineScript, QWebEngineView, ) from calibre import prints, random_user_agent +from calibre.ebooks.metadata.sources.search_engines import google_consent_cookies from calibre.gui2 import error_dialog from calibre.gui2.viewer.web_view import apply_font_settings, vprefs from calibre.gui2.widgets2 import Dialog @@ -226,6 +228,17 @@ def create_profile(): insert_scripts(ans, create_script('lookup.js', js, injection_point=QWebEngineScript.InjectionPoint.DocumentCreation)) s = ans.settings() s.setDefaultTextEncoding('utf-8') + cs = ans.cookieStore() + for c in google_consent_cookies(): + cookie = QNetworkCookie() + cookie.setName(c['name'].encode()) + cookie.setValue(c['value'].encode()) + cookie.setDomain(c['domain']) + cookie.setPath(c['path']) + cookie.setSecure(False) + cookie.setHttpOnly(False) + cookie.setExpirationDate(QDateTime()) + cs.setCookie(cookie) create_profile.ans = ans return ans