Skip to content

Commit

Permalink
1.去掉一些依赖
Browse files Browse the repository at this point in the history
2.优化代码
  • Loading branch information
lemisky committed Aug 21, 2021
1 parent 858483e commit 968d0a5
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ badge.svg
# Installer logs
pip-log.txt
pip-delete-this-directory.txt

main.py
# Unit test / coverage reports
htmlcov/
.tox/
Expand Down
26 changes: 20 additions & 6 deletions pygtrans/ApiKeyTranslate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import math
from typing import List, Union, Dict, overload

import pxy
import requests

from pygtrans.DetectResponse import DetectResponse
Expand All @@ -18,7 +17,22 @@
from pygtrans.TranslateResponse import TranslateResponse


def split_list(obj_list: List, sub_size: int = 128) -> List[list]:
"""
split list
:param obj_list: list object
:param sub_size: sub list size
:return: List[list]
"""
if not isinstance(obj_list, list):
return [[obj_list]]
if sub_size < 1:
sub_size = 1
return [obj_list[i:i + sub_size] for i in range(0, len(obj_list), sub_size)]


def split_list_by_content_size(obj_list: List[str], content_size: int = 102400) -> List[List[str]]:
"""..."""
if content_size < 1:
content_size = 1
if len(obj_list) == 1 or len(''.join(obj_list)) <= content_size:
Expand Down Expand Up @@ -89,7 +103,7 @@ def __init__(
if proxies is not None:
self.session.proxies = proxies

def languages(self, target: str = None, model: str = None) -> List[LanguageResponse]:
def languages(self, target: str = None, model: str = None) -> Union[List[LanguageResponse], Null]:
"""语言支持列表"""
if target is None:
target = self.target
Expand All @@ -108,7 +122,7 @@ def detect(self, q: str) -> DetectResponse:
def detect(self, q: List[str]) -> List[DetectResponse]:
"""..."""

def detect(self, q: Union[str, List[str]]) -> Union[DetectResponse, List[DetectResponse]]:
def detect(self, q: Union[str, List[str]]) -> Union[DetectResponse, List[DetectResponse], Null]:
"""语言检测, 支持批量
:param q: 字符串或字符串列表
Expand All @@ -130,7 +144,7 @@ def detect(self, q: Union[str, List[str]]) -> Union[DetectResponse, List[DetectR
"""
ll = []
for ql in pxy.split_list(q):
for ql in split_list(q):
for qli in split_list_by_content_size(ql):
response = self.session.post(self._DETECT_URL, params={
'key': self.api_key
Expand Down Expand Up @@ -159,7 +173,7 @@ def translate(
def translate(
self, q: Union[str, List[str]], target: str = None, source: str = None, _format: str = None,
model: str = None
) -> Union[TranslateResponse, List[TranslateResponse]]:
) -> Union[TranslateResponse, List[TranslateResponse], Null]:
"""文本翻译, 支持批量
:param q: str: 字符串或字符串列表
Expand Down Expand Up @@ -204,7 +218,7 @@ def translate(
model = self.model

ll = []
for ql in pxy.split_list(q):
for ql in split_list(q):
for qli in split_list_by_content_size(ql):
response = self.session.post(self._BASE_URL, params={
'key': self.api_key, 'target': target, 'source': source, 'format': _format, 'model': model
Expand Down
5 changes: 5 additions & 0 deletions pygtrans/DetectResponse.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
"""DetectResponse"""


class DetectResponse:
"""DetectResponse"""

def __init__(self, language: str, isReliable: bool = True, confidence: float = 1.0):
"""
Expand Down
5 changes: 5 additions & 0 deletions pygtrans/LanguageResponse.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
"""LanguageResponse"""


class LanguageResponse:
"""LanguageResponse"""

def __init__(self, language: str, name: str = None):
"""
Expand Down
14 changes: 7 additions & 7 deletions pygtrans/Translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __init__(
self.format = _format

if user_agent is None:
user_agent = f'GoogleTranslate/6.{random.randint(10,100)}.0.06.{random.randint(111111111, 999999999)} (Linux; U; Android {random.randint(5, 11)}; {base64.b64encode(str(random.random())[2:].encode()).decode()})'
user_agent = f'GoogleTranslate/6.{random.randint(10, 100)}.0.06.{random.randint(111111111, 999999999)} (Linux; U; Android {random.randint(5, 11)}; {base64.b64encode(str(random.random())[2:].encode()).decode()})'

self.session = requests.Session()
self.session.headers = {
Expand Down Expand Up @@ -114,7 +114,7 @@ def __init__(
# return [LanguageResponse(language=i[0], name=i[1]) for i in response.json()['sl'].items()]
# # return [LanguageResponse(language=i[0], name=i[1]) for i in response.json()['tl'].items()]

def detect(self, q: str) -> DetectResponse:
def detect(self, q: str) -> Union[DetectResponse, Null]:
"""语言检测
:param q: 需要检测的内容, 不支持批量, 如需批量, 请参阅: :func:`translate_and_detect`.
Expand Down Expand Up @@ -149,7 +149,7 @@ def translate(

def translate(
self, q: Union[str, List[str]], target: str = None, source: str = None, _format: str = None
) -> Union[TranslateResponse, List[TranslateResponse]]:
) -> Union[TranslateResponse, List[TranslateResponse], Null]:
"""翻译文本, 支持批量, 支持 html
:param q: str: 字符串或字符串列表
Expand Down Expand Up @@ -178,7 +178,7 @@ def translate(

if isinstance(q, str):
if q == '':
return ''
return TranslateResponse('')

response = self._translate(q=q, target=target, source=source, _format=_format, v='1.0')

Expand All @@ -204,7 +204,7 @@ def translate_and_detect(

def translate_and_detect(
self, q: Union[str, List[str]], target: str = None, source: str = None, _format: str = None
) -> Union[TranslateResponse, List[TranslateResponse]]:
) -> Union[TranslateResponse, List[TranslateResponse], Null]:
"""与 :class:`translate` 相同, 区别是 ``TranslateResponse`` 对象的 ``detectedSourceLanguage`` 属性可用
基本用法:
Expand All @@ -227,7 +227,7 @@ def translate_and_detect(

if isinstance(q, str):
if q == '':
return ''
return TranslateResponse('')

response = self._translate(q=q, target=target, source=source, _format=_format)

Expand Down Expand Up @@ -258,7 +258,7 @@ def _translate(
)
return response

def tts(self, q: str, target: str = None) -> bytes:
def tts(self, q: str, target: str = None) -> Union[bytes, Null]:
"""语音: 实验功能
:param q: 只支持短语字符串
Expand Down
5 changes: 5 additions & 0 deletions pygtrans/TranslateResponse.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
"""TranslateResponse"""


class TranslateResponse:
"""TranslateResponse"""

def __init__(self, translatedText: str, detectedSourceLanguage: str = None, model: str = None):
"""
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
requests
pytest
pxy
pytest
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ packages = find:
python_requires = >=3.6.*
install_requires =
requests
pxy
[options.packages.find]
where = .
exclude =
Expand Down

0 comments on commit 968d0a5

Please sign in to comment.