mirror of
https://github.com/xfgryujk/blivechat.git
synced 2025-04-04 00:21:12 +08:00
修复启动长时间后自动翻译失效的问题
This commit is contained in:
parent
6e8b04ef4f
commit
d7df071595
@ -121,13 +121,19 @@ class TranslateProvider:
|
|||||||
|
|
||||||
class TencentTranslate(TranslateProvider):
|
class TencentTranslate(TranslateProvider):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
# 过期时间1小时
|
||||||
self._qtv = ''
|
self._qtv = ''
|
||||||
self._qtk = ''
|
self._qtk = ''
|
||||||
|
self._reinit_future = None
|
||||||
# 连续失败的次数
|
# 连续失败的次数
|
||||||
self._fail_count = 0
|
self._fail_count = 0
|
||||||
self._cool_down_future = None
|
self._cool_down_future = None
|
||||||
|
|
||||||
async def init(self):
|
async def init(self):
|
||||||
|
if self._reinit_future is not None:
|
||||||
|
self._reinit_future.cancel()
|
||||||
|
self._reinit_future = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with _http_session.get('https://fanyi.qq.com/') as r:
|
async with _http_session.get('https://fanyi.qq.com/') as r:
|
||||||
if r.status != 200:
|
if r.status != 200:
|
||||||
@ -142,14 +148,25 @@ class TencentTranslate(TranslateProvider):
|
|||||||
if m is None:
|
if m is None:
|
||||||
logger.exception('TencentTranslate init failed: qtv not found')
|
logger.exception('TencentTranslate init failed: qtv not found')
|
||||||
return False
|
return False
|
||||||
self._qtv = m[1]
|
qtv = m[1]
|
||||||
m = re.search(r"""\bqtk\s*=\s*['"](.+?)['"]""", html)
|
m = re.search(r"""\bqtk\s*=\s*['"](.+?)['"]""", html)
|
||||||
if m is None:
|
if m is None:
|
||||||
logger.exception('TencentTranslate init failed: qtk not found')
|
logger.exception('TencentTranslate init failed: qtk not found')
|
||||||
return False
|
return False
|
||||||
self._qtk = m[1]
|
qtk = m[1]
|
||||||
|
|
||||||
|
self._qtk = qtv
|
||||||
|
self._qtv = qtk
|
||||||
|
self._reinit_future = asyncio.ensure_future(self._reinit_coroutine())
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
async def _reinit_coroutine(self):
|
||||||
|
try:
|
||||||
|
await asyncio.sleep(55 * 60)
|
||||||
|
await self.init()
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
pass
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_available(self):
|
def is_available(self):
|
||||||
return self._qtv != '' and self._qtk != ''
|
return self._qtv != '' and self._qtk != ''
|
||||||
@ -194,7 +211,12 @@ class TencentTranslate(TranslateProvider):
|
|||||||
if data['errCode'] != 0:
|
if data['errCode'] != 0:
|
||||||
logger.warning('TencentTranslate failed: %d %s', data['errCode'], data['errMsg'])
|
logger.warning('TencentTranslate failed: %d %s', data['errCode'], data['errMsg'])
|
||||||
return None
|
return None
|
||||||
return ''.join(record['targetText'] for record in data['translate']['records'])
|
res = ''.join(record['targetText'] for record in data['translate']['records'])
|
||||||
|
if res == '' and text.strip() != '':
|
||||||
|
# qtv、qtk过期
|
||||||
|
logger.warning('TencentTranslate result is empty %s', data)
|
||||||
|
return None
|
||||||
|
return res
|
||||||
|
|
||||||
def _on_fail(self):
|
def _on_fail(self):
|
||||||
self._fail_count += 1
|
self._fail_count += 1
|
||||||
@ -209,7 +231,7 @@ class TencentTranslate(TranslateProvider):
|
|||||||
while True:
|
while True:
|
||||||
await asyncio.sleep(3 * 60)
|
await asyncio.sleep(3 * 60)
|
||||||
try:
|
try:
|
||||||
is_success = self.init()
|
is_success = await self.init()
|
||||||
except:
|
except:
|
||||||
logger.exception('TencentTranslate init error:')
|
logger.exception('TencentTranslate init error:')
|
||||||
continue
|
continue
|
||||||
@ -319,7 +341,7 @@ class YoudaoTranslate(TranslateProvider):
|
|||||||
while True:
|
while True:
|
||||||
await asyncio.sleep(3 * 60)
|
await asyncio.sleep(3 * 60)
|
||||||
try:
|
try:
|
||||||
is_success = self.init()
|
is_success = await self.init()
|
||||||
except:
|
except:
|
||||||
logger.exception('YoudaoTranslate init error:')
|
logger.exception('YoudaoTranslate init error:')
|
||||||
continue
|
continue
|
||||||
|
Loading…
Reference in New Issue
Block a user