提交 7b53e032 编写于 作者: W wizardforcel

add timeout

上级 1ff8aa90
......@@ -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]*?</\1>'
RE_TAG = r'<[^>]*?>'
......
# 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
......
......@@ -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]])
......
......@@ -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
......
# 历史记录
v2020.4.29
+ 建立项目
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册