mirror of
https://github.com/xfgryujk/blivechat.git
synced 2025-01-13 13:50:10 +08:00
兼容腾讯翻译升级
This commit is contained in:
parent
888bc896a5
commit
210749fa0f
@ -406,7 +406,7 @@ class ChatHandler(tornado.websocket.WebSocketHandler):
|
||||
self._close_on_timeout_future = None
|
||||
else:
|
||||
logger.warning('Unknown cmd, client: %s, cmd: %d, body: %s', self.request.remote_ip, cmd, body)
|
||||
except:
|
||||
except Exception:
|
||||
logger.exception('on_message error, client: %s, message: %s', self.request.remote_ip, message)
|
||||
|
||||
def on_close(self):
|
||||
|
@ -125,7 +125,7 @@ async def _get_avatar_url_from_web_consumer():
|
||||
|
||||
# 限制频率,防止被B站ban
|
||||
await asyncio.sleep(0.2)
|
||||
except:
|
||||
except Exception:
|
||||
logger.exception('_get_avatar_url_from_web_consumer error:')
|
||||
|
||||
|
||||
|
@ -98,7 +98,7 @@ def _on_translate_done(key, future):
|
||||
# 缓存
|
||||
try:
|
||||
res = future.result()
|
||||
except:
|
||||
except Exception:
|
||||
return
|
||||
if res is None:
|
||||
return
|
||||
@ -135,25 +135,23 @@ class TencentTranslate(TranslateProvider):
|
||||
|
||||
async def _do_init(self):
|
||||
try:
|
||||
async with _http_session.get('https://fanyi.qq.com/') as r:
|
||||
async with _http_session.post('https://fanyi.qq.com/api/reaauth') as r:
|
||||
if r.status != 200:
|
||||
logger.warning('TencentTranslate init request failed: status=%d %s', r.status, r.reason)
|
||||
return False
|
||||
html = await r.text()
|
||||
data = await r.json()
|
||||
except (aiohttp.ClientConnectionError, asyncio.TimeoutError):
|
||||
logger.exception('TencentTranslate init error:')
|
||||
return False
|
||||
|
||||
m = re.search(r"""\bqtv\s*=\s*['"](.+?)['"]""", html)
|
||||
if m is None:
|
||||
logger.exception('TencentTranslate init failed: qtv not found')
|
||||
qtv = data.get('qtv', None)
|
||||
if qtv is None:
|
||||
logger.warning('TencentTranslate init failed: qtv not found')
|
||||
return False
|
||||
qtv = m[1]
|
||||
m = re.search(r"""\bqtk\s*=\s*['"](.+?)['"]""", html)
|
||||
if m is None:
|
||||
logger.exception('TencentTranslate init failed: qtk not found')
|
||||
qtk = data.get('qtk', None)
|
||||
if qtk is None:
|
||||
logger.warning('TencentTranslate init failed: qtk not found')
|
||||
return False
|
||||
qtk = m[1]
|
||||
|
||||
self._qtv = qtv
|
||||
self._qtk = qtk
|
||||
@ -168,7 +166,7 @@ class TencentTranslate(TranslateProvider):
|
||||
try:
|
||||
if await self._do_init():
|
||||
break
|
||||
except:
|
||||
except Exception:
|
||||
logger.exception('TencentTranslate init error:')
|
||||
await asyncio.sleep(3 * 60)
|
||||
except asyncio.CancelledError:
|
||||
@ -232,7 +230,7 @@ class TencentTranslate(TranslateProvider):
|
||||
self._cool_down_future = asyncio.ensure_future(self._cool_down())
|
||||
|
||||
async def _cool_down(self):
|
||||
logger.warning('TencentTranslate is cooling down')
|
||||
logger.info('TencentTranslate is cooling down')
|
||||
self._qtv = self._qtk = ''
|
||||
try:
|
||||
while True:
|
||||
@ -242,10 +240,10 @@ class TencentTranslate(TranslateProvider):
|
||||
if await self._do_init():
|
||||
self._fail_count = 0
|
||||
break
|
||||
except:
|
||||
except Exception:
|
||||
logger.exception('TencentTranslate init error:')
|
||||
finally:
|
||||
logger.warning('TencentTranslate finished cooling down')
|
||||
logger.info('TencentTranslate finished cooling down')
|
||||
self._cool_down_future = None
|
||||
|
||||
|
||||
@ -341,20 +339,20 @@ class YoudaoTranslate(TranslateProvider):
|
||||
}
|
||||
|
||||
async def _cool_down(self):
|
||||
logger.warning('YoudaoTranslate is cooling down')
|
||||
logger.info('YoudaoTranslate is cooling down')
|
||||
self._has_init = False
|
||||
try:
|
||||
while True:
|
||||
await asyncio.sleep(3 * 60)
|
||||
try:
|
||||
is_success = await self.init()
|
||||
except:
|
||||
except Exception:
|
||||
logger.exception('YoudaoTranslate init error:')
|
||||
continue
|
||||
if is_success:
|
||||
break
|
||||
finally:
|
||||
logger.warning('YoudaoTranslate finished cooling down')
|
||||
logger.info('YoudaoTranslate finished cooling down')
|
||||
self._cool_down_future = None
|
||||
|
||||
|
||||
@ -385,7 +383,7 @@ class BilibiliTranslate(TranslateProvider):
|
||||
asyncio.ensure_future(self._translate_coroutine(text, future))
|
||||
# 频率限制一分钟20次
|
||||
await asyncio.sleep(3.1)
|
||||
except:
|
||||
except Exception:
|
||||
logger.exception('BilibiliTranslate error:')
|
||||
|
||||
async def _translate_coroutine(self, text, future):
|
||||
|
Loading…
Reference in New Issue
Block a user