Skip to content

Commit

Permalink
E-book viewer: When looking up words via Google in Europe pre-approve…
Browse files Browse the repository at this point in the history
… the GDPR consent cookie. Fixes #2047181 [Cannot use search google dictionary](https://bugs.launchpad.net/calibre/+bug/2047181)
  • Loading branch information
kovidgoyal committed Dec 22, 2023
1 parent 2338919 commit 7f3ccb3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
24 changes: 17 additions & 7 deletions src/calibre/ebooks/metadata/sources/search_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 16 additions & 3 deletions src/calibre/gui2/viewer/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 7f3ccb3

Please sign in to comment.