mirror of
https://github.com/yihong0618/bilingual_book_maker.git
synced 2025-06-05 19:15:34 +00:00
Improved the issue of Google Translate skipping paragraphs due to connectivity problems. (#275)
This commit is contained in:
parent
eee2b10718
commit
9ba0dd4b91
@ -27,7 +27,7 @@ class Google(Base):
|
||||
|
||||
def translate(self, text):
|
||||
print(text)
|
||||
r = self.session.post(
|
||||
"""r = self.session.post(
|
||||
self.api_url,
|
||||
headers=self.headers,
|
||||
data=f"q={requests.utils.quote(text)}",
|
||||
@ -36,6 +36,24 @@ class Google(Base):
|
||||
return text
|
||||
t_text = "".join(
|
||||
[sentence.get("trans", "") for sentence in r.json()["sentences"]],
|
||||
)
|
||||
)"""
|
||||
t_text = self._retry_translate(text)
|
||||
print("[bold green]" + re.sub("\n{3,}", "\n\n", t_text) + "[/bold green]")
|
||||
return t_text
|
||||
|
||||
def _retry_translate(self, text, timeout=3):
|
||||
time = 0
|
||||
while time <= timeout:
|
||||
time += 1
|
||||
r = self.session.post(
|
||||
self.api_url,
|
||||
headers=self.headers,
|
||||
data=f"q={requests.utils.quote(text)}",
|
||||
timeout=3,
|
||||
)
|
||||
if r.ok:
|
||||
t_text = "".join(
|
||||
[sentence.get("trans", "") for sentence in r.json()["sentences"]],
|
||||
)
|
||||
return t_text
|
||||
return text
|
||||
|
Loading…
x
Reference in New Issue
Block a user