Skip to content

Commit fb392c0

Browse files
Added proxy field and fix interaction with DOM (#288)
* Added proxy field * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * docs: use_dom_submit is obsolete * core: use better selector for error handling --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f47d691 commit fb392c0

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

deepl/deepl.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from install_playwright import install
1010
from playwright._impl._errors import Error as PlaywrightError
11-
from playwright.async_api import async_playwright
11+
from playwright.async_api import ProxySettings, async_playwright
1212
from playwright.async_api._generated import Browser, Playwright
1313

1414
from deepl.languages import FR_LANGS, TO_LANGS
@@ -46,14 +46,16 @@ def __init__(
4646
fr_lang: str,
4747
to_lang: str,
4848
timeout: int = 15000,
49+
proxy: ProxySettings | None = None,
50+
4951
) -> None:
5052
"""Initialize DeepLCLI.
5153
5254
Args:
5355
fr_lang (str): Source language.
5456
to_lang (str): Target language.
5557
timeout (int): Timeout in milliseconds. Default is 15000ms.
56-
use_dom_submit (bool): Use DOM submit instead of URL. Default is False.
58+
proxy (ProxySettings): Use a proxy to access deepl.
5759
5860
Raises:
5961
DeepLCLIError: If the language is not valid.
@@ -73,6 +75,7 @@ def __init__(
7375
self.translated_to_lang: str | None = None
7476
self.max_length = 1500
7577
self.timeout = timeout
78+
self.proxy = proxy
7679

7780
def translate(self, script: str) -> str:
7881
"""Translate script.
@@ -125,29 +128,24 @@ async def __translate(self, script: str) -> str:
125128
)
126129

127130
url = "https://www.deepl.com/en/translator"
128-
await page.goto(url)
131+
132+
async with page.expect_response(lambda resp: resp.url == url and resp.request.method == "GET") as resp_info:
133+
await page.goto(url)
134+
135+
response = await resp_info.value
136+
137+
if not response.ok:
138+
error_text = await page.inner_text("body > main > div > p")
139+
140+
msg = f"Page loading failed with status code {response.status}: {error_text}"
141+
raise DeepLCLIError(msg)
129142

130143
try:
131144
page.get_by_role("main")
132145
except PlaywrightError as e:
133146
msg = f"Maybe Time limit exceeded. ({self.timeout} ms)"
134147
raise DeepLCLIPageLoadError(msg) from e
135148

136-
with contextlib.suppress(PlaywrightError):
137-
await page.click("button[id=cookie-banner-lax-close-button]")
138-
await page.wait_for_function(
139-
"""
140-
() => document.querySelector('div[data-testid="chrome-extension-toast"]')
141-
""",
142-
)
143-
await page.evaluate(
144-
"""
145-
document.querySelector(
146-
'div[data-testid="chrome-extension-toast"]',
147-
).querySelector('button').click()
148-
""",
149-
)
150-
151149
await page.locator(
152150
"button[data-testid=translator-source-lang-btn]",
153151
).dispatch_event("click")
@@ -265,4 +263,5 @@ async def __get_browser(self, p: Playwright) -> Browser:
265263
"--no-zygote",
266264
"--window-size=1920,1080",
267265
],
266+
proxy=self.proxy,
268267
)

0 commit comments

Comments
 (0)