Skip to content

Commit

Permalink
🔥 Remove code about thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Dec 19, 2023
1 parent daf04c6 commit 3af2ce3
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 124 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env -S pip install -r

aiohttp
tomli; python_version < "3.11"
11 changes: 2 additions & 9 deletions scripts/test-bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.. code:: sh
$ ./test_bench.py --translators=google,bing,haici,stardict block
$ ./test-bench.py --translators=google,bing,haici,stardict block
input text: block
target lang zh_CN
Expand All @@ -14,12 +14,6 @@
coroutine:
time: 1.8392638560035266
results: 4
threading:
time: 0.6133544620242901
results: 4
serial run:
time: 1.6182543189497665
results: 4
"""
import time
from timeit import timeit
Expand All @@ -45,7 +39,7 @@
args.translators = args.translators.split(",")

translationss = {}
for use in ["coroutine", "threading", "serial run"]:
for use in ["coroutine"]:
print(
f"{use}:\n\ttime:\t",
timeit(
Expand All @@ -55,7 +49,6 @@
args.target_lang,
args.source_lang,
args.translators,
use={use!r},
)
""",
globals=globals(),
Expand Down
53 changes: 6 additions & 47 deletions src/translate_shell/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import asyncio
import logging
from dataclasses import dataclass, field
from inspect import iscoroutine
from threading import Thread
from typing import Any, Callable

Expand All @@ -25,7 +26,7 @@ class Translations:
results: list[Translation] = field(default_factory=list)


def translate_once(
async def translate_once(
translator: Translator, translations: Translations, option: dict[str, Any]
) -> None:
"""Translate once.
Expand All @@ -44,27 +45,13 @@ def translate_once(
translations.from_lang,
option,
)
if iscoroutine(res):
res = await res
if res:
translations.results.append(res)
translations.status = 1


async def _translate_once(
translator: Translator, translations: Translations, option: dict[str, Any]
) -> None:
"""Translate once.
:param translator:
:type translator: Translator
:param translations:
:type translations: Translations
:param option:
:type option: dict[str, Any]
:rtype: None
"""
translate_once(translator, translations, option)


async def translate_many(
translators: list[Translator],
translations: Translations,
Expand All @@ -84,7 +71,7 @@ async def translate_many(
for translator in translators:
tasks += [
asyncio.create_task(
_translate_once(
translate_once(
translator, translations, options.get(translator.name, {})
)
)
Expand All @@ -99,7 +86,6 @@ def translate(
source_lang: str = "auto",
translators: list[Callable[[], Translator]] | list[str] | None = None,
options: dict[str, dict[str, Any]] | None = None,
use: str = "coroutine",
) -> Translations:
"""Translate.
Expand All @@ -113,8 +99,6 @@ def translate(
:type translators: list[Callable[[], Translator]] | list[str] | None
:param options:
:type options: dict[str, dict[str, Any]] | None
:param use:
:type use: str
:rtype: Translations
"""
if translators is None:
Expand All @@ -133,30 +117,5 @@ def translate(
continue
true_translators += [translator]

if len(true_translators) == 1:
translator = true_translators[0]
translate_once(
translator, translations, options.get(translator.name, {})
)
elif use == "threading":
threads = []
for translator in true_translators:
thread = Thread(
target=translate_once,
args=(
translator,
translations,
options.get(translator.name, {}),
),
)
threads += [thread]
list(map(lambda x: x.start(), threads))
list(map(lambda x: x.join(), threads))
elif use == "coroutine":
asyncio.run(translate_many(true_translators, translations, options))
else:
for translator in true_translators:
translate_once(
translator, translations, options.get(translator.name, {})
)
asyncio.run(translate_many(true_translators, translations, options))
return translations
67 changes: 31 additions & 36 deletions src/translate_shell/translators/online/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
====================
"""
import logging
from contextlib import suppress
from dataclasses import dataclass
from typing import Any
from urllib.error import HTTPError, URLError
from urllib.parse import urlencode
from urllib.request import Request, urlopen

from aiohttp import ClientSession

from .. import Translator

Expand All @@ -21,44 +18,42 @@ class OnlineTranslator(Translator):
name: str
timeout: int = 5

def http_get(
self, url: str, data: Any = None, header: dict[str, str] | None = None
async def http_get(
self,
url: str,
session: ClientSession | None,
params: dict[str, str] | None = None,
headers: dict[str, str] | None = None,
) -> str:
"""Http get.
:param url:
:type url: str
:param data:
:type data: Any
:param header:
:type header: dict[str, str] | None
:param session:
:type session: ClientSession | None
:param params:
:type params: dict[str, str] | None
:param headers:
:type headers: dict[str, str] | None
:rtype: str
"""
if header is None:
header = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)"
" AppleWebKit/537.36 (KHTML, like Gecko)"
" Chrome/75.0.3770.100 Safari/537.36"
}

if data:
query_string = urlencode(data)
url = url + "?" + query_string

with suppress(HTTPError, URLError):
with urlopen(
Request(url, None, header), timeout=self.timeout
) as r: # skipcq: BAN-B310
charset = r.headers.get_param("charset") or "utf-8"

r = r.read().decode(charset)
return r

logger.warning(
"Translator %s timed out, please check your network",
self.name,
)
return ""
if session is None:
_session = ClientSession()
else:
_session = session
text = ""
try:
async with _session.get(
url, params=params, headers=headers
) as resp:
text = await resp.text()
except Exception:
logger.warning(
"Translator %s timed out, please check your network", self.name
)
if session is None:
await _session.close()
return text

@staticmethod
def md5sum(text: str | bytes) -> str:
Expand Down
7 changes: 4 additions & 3 deletions src/translate_shell/translators/online/bing.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __post_init__(self) -> None:
r'<span class="ht_trs">(.*?)</span>'
)

def __call__(
async def __call__(
self, text: str, tl: str, sl: str, option: dict[str, Any]
) -> Translation | None:
"""Call.
Expand All @@ -52,7 +52,7 @@ def __call__(
res = self.create_translation(text, tl, sl)
tl, sl = self.convert_langs(tl, sl)
url = self.cnurl if "zh" in tl else self.url
url = url + "?q=" + quote_plus(text)
params = {"q": text}
headers = {
"Accept": (
"text/html,application/xhtml+xml,application/xml;"
Expand All @@ -61,7 +61,8 @@ def __call__(
"Accept-Language": "en-US,en;q=0.5",
"Cookie": "_EDGE_S=mkt=" + tl,
}
resp = self.http_get(url, None, headers)
session = option.get(self.name, {}).get("session", None)
resp = await self.http_get(url, session, params, headers)
if not resp:
return None
res.phonetic = self.get_phonetic(resp)
Expand Down
33 changes: 12 additions & 21 deletions src/translate_shell/translators/online/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import json
from dataclasses import dataclass
from typing import Any
from urllib.parse import quote_plus

from .. import Translation
from . import OnlineTranslator
Expand All @@ -21,24 +20,7 @@ class GoogleTranslator(OnlineTranslator):
host: str = "translate.googleapis.com"
cnhost: str = "translate.google.com.hk"

def get_url(self, sl: str, tl: str, qry: str) -> str:
"""Get url.
:param sl:
:type sl: str
:param tl:
:type tl: str
:param qry:
:type qry: str
:rtype: str
"""
http_host = self.cnhost if "zh" in tl else self.host
qry = quote_plus(qry)
url = f"https://{http_host}/translate_a/single?client=gtx&sl={sl}\
&tl={tl}&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&q={qry}"
return url

def __call__(
async def __call__(
self, text: str, tl: str, sl: str, option: dict[str, Any]
) -> Translation | None:
"""Call.
Expand All @@ -55,8 +37,17 @@ def __call__(
"""
res = self.create_translation(text, tl, sl)
tl, sl = self.convert_langs(tl, sl)
url = self.get_url(sl, tl, text)
resp = self.http_get(url)
http_host = self.cnhost if "zh" in tl else self.host
url = f"https://{http_host}/translate_a/single"
params = {
"client": "gtx",
"sl": sl,
"tl": tl,
"dt": ["at", "bd", "ex", "ld", "md", "qca", "rw", "rm", "ss", "t"],
"q": text,
}
session = option.get(self.name, {}).get("session", None)
resp = await self.http_get(url, session, params)
if not resp:
return None
obj = json.loads(resp)
Expand Down
9 changes: 4 additions & 5 deletions src/translate_shell/translators/online/haici.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import re
from dataclasses import dataclass
from typing import Any
from urllib.parse import quote_plus

from .. import Translation
from . import OnlineTranslator
Expand All @@ -20,7 +19,7 @@ class HaiciTranslator(OnlineTranslator):
name: str = "haici"
timeout: int = 15

def __call__(
async def __call__(
self, text: str, tl: str, sl: str, option: dict[str, Any]
) -> Translation | None:
"""Call.
Expand All @@ -38,9 +37,9 @@ def __call__(
res = self.create_translation(text, tl, sl)
tl, sl = self.convert_langs(tl, sl)
url = "http://dict.cn/mini.php"
req = {}
req["q"] = quote_plus(text)
resp = self.http_get(url, req)
params = {"q": text}
session = option.get(self.name, {}).get("session", None)
resp = await self.http_get(url, session, params)
if not resp:
return None

Expand Down
7 changes: 4 additions & 3 deletions src/translate_shell/translators/online/youdaozhiyun.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def sign(self, text: str, salt: str) -> str:
s = self.app_id + text + salt + self.app_sec
return self.md5sum(s)

def __call__(
async def __call__(
self, text: str, tl: str, sl: str, option: dict[str, Any]
) -> Translation | None:
"""Call.
Expand All @@ -86,15 +86,16 @@ def __call__(
to = "zh-CHT"
else:
to = tl
data = {
params = {
"appKey": self.app_id,
"q": text,
"from": sl,
"to": to,
"salt": salt,
"sign": sign,
}
resp = self.http_get(self.url, data, None)
session = option.get(self.name, {}).get("session", None)
resp = await self.http_get(self.url, session, params)

if isinstance(resp, str):
try:
Expand Down

0 comments on commit 3af2ce3

Please sign in to comment.