diff --git a/BookerTrans/__init__.py b/BookerTrans/__init__.py index dcf89ff59db28c6aeba7261d6639cec9398b2db3..059e9e44ebb1a63146cef5e6a03819f60b0998c4 100644 --- a/BookerTrans/__init__.py +++ b/BookerTrans/__init__.py @@ -10,7 +10,7 @@ from . import config __author__ = "ApacheCN" __email__ = "apachecn@163.com" __license__ = "SATA" -__version__ = "2020.04.29" +__version__ = "2020.04.29.1" RE_CODE = r'<(pre|code)[^>]*?>[\s\S]*?' RE_TAG = r'<[^>]*?>' diff --git a/BookerTrans/__main__.py b/BookerTrans/__main__.py index e196d9dfeb2064271e88257cf022518e60d91660..8c2cf06d600797581072e7a4ce6effb1b11a4ced 100644 --- a/BookerTrans/__main__.py +++ b/BookerTrans/__main__.py @@ -1,5 +1,6 @@ # coding: utf-8 +import os from os import path from argparse import ArgumentParser from . import trans_html, config, api, __version__ @@ -29,6 +30,7 @@ def main(): parser.add_argument('-v', '--version', action="version", version=__version__) parser.add_argument('-H', '--host', default='translate.google.cn', help="host for google translator") parser.add_argument('-P', '--proxy', help=f'proxy with format \d+\.\d+\.\d+\.\d+:\d+ or empty') + parser.add_argument('-t', '--timeout', type=float, help=f'timeout in second') parser.add_argument('-w', '--wait-sec', type=float, default=0.5, help='delay in second between two times of translation') parser.add_argument('-r', '--retry', type=int, default=10, help='count of retrying') parser.add_argument('-s', '--src', default='auto', help='src language') @@ -40,6 +42,7 @@ def main(): args.proxy = {'http': p, 'https': p} api.host = args.host api.proxy = args.proxy + api.timeout = args.timeout config.wait_sec = args.wait_sec config.retry = args.retry config.src = args.src diff --git a/BookerTrans/api.py b/BookerTrans/api.py index 81d3a022ac57deae805aaf93f6ea9dfe80497aff..91d9177cf989acbb06faa012230bf737ad8ef838 100644 --- a/BookerTrans/api.py +++ b/BookerTrans/api.py @@ -16,6 +16,7 @@ class GoTransApi: self.tkk = None self.host = 'translate.google.cn' self.proxy = None + self.timeout = 8 @staticmethod def _shr32(x, bits): @@ -111,7 +112,8 @@ class GoTransApi: res = requests.get( f'https://{self.host}/', headers=GoTransApi.headers, - proxies=self.proxy + proxies=self.proxy, + timeout=self.timeout, ).text self.tkk = re.search(r"tkk:'(\d+\.\d+)", res).group(1) return self.tkk @@ -125,7 +127,8 @@ class GoTransApi: res = requests.get( url, headers=GoTransApi.headers, - proxies=self.proxy + proxies=self.proxy, + timeout=self.timeout, ).text j = json.loads(res) trans = ' '.join([o[0] for o in j[0]]) diff --git a/README.md b/README.md index d02cbf3d2b9b9149422b294265970dba9e277d79..44db947653023340ee27165da3bf1641edfd2912 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,13 @@ pip install git+https://github.com/apachecn/BookerTrans ## 使用说明 ``` -btrans [-h] [-v] [-H HOST] [-P PROXY] [-w WAIT_SEC] [-r RETRY] - [-s SRC] [-d DST] +btrans [-h] [-v] [-H HOST] [-P PROXY] [-t TIMEOUT] [-w WAIT_SEC] + [-r RETRY] [-s SRC] [-d DST] fname -H HOST: 域名,默认为 translate.google.com -P PROXY: 代理,格式为 \d+\.\d+\.\d+\.\d+:\d+,默认为空 +-t TIMEOUT: 超时时间,以秒为单位,默认为 8 -w WAIT_SEC: 两次翻译之间的延迟(以秒为单位),默认为 0.5 -r RETRY: 重试次数,默认为 10 -s SRC: 源语言,默认为 auto diff --git a/history.md b/history.md new file mode 100644 index 0000000000000000000000000000000000000000..d822d16ab539e38f4ec1a9d07c123b5d30a889c1 --- /dev/null +++ b/history.md @@ -0,0 +1,5 @@ +# 历史记录 + +v2020.4.29 + ++ 建立项目 \ No newline at end of file