Commit 99460f8
Changed files (2)
src
src/asr/tecent_asr.py
@@ -6,12 +6,11 @@ import base64
import hashlib
import hmac
import time
-from json import JSONDecodeError
-from httpx import AsyncClient, RequestError
from loguru import logger
from config import PROXY
+from networking import hx_req
# 录音识别极速版
@@ -147,19 +146,11 @@ class FlashRecognizer:
header = self._build_header()
query_arr = self._create_query_arr(req)
req_url = self._build_req_with_signature(self.credential.secret_key, query_arr, header)
- async with AsyncClient(http2=True, proxy=PROXY.TENCENT, follow_redirects=True) as hx:
- try:
- resp = await hx.post(req_url, headers=header, data=data, timeout=30)
- resp.raise_for_status()
- if resp.json().get("code") != 0:
- logger.warning(f"ASR failed: {resp.json()}")
- except JSONDecodeError:
- logger.error(f"ASR Json decode failed: {resp}")
- except RequestError as exc:
- logger.warning(f"ASR failed while requesting {exc.request.url!r}.")
- except Exception as e:
- logger.warning(f"ASR failed: {e}")
- return resp.json()
+ resp = await hx_req(req_url, method="POST", headers=header, merge_headers=False, post_data=data, timeout=30, proxy=PROXY.TENCENT, check_kv={"code": 0})
+ if resp.get("hx_error"):
+ logger.error(f"ASR failed: {resp.get('hx_error')}")
+ return {}
+ return resp
class Credential:
src/networking.py
@@ -53,6 +53,7 @@ async def hx_req(
cookies: dict | None = None,
params: dict | None = None,
post_json: dict | None = None,
+ post_data: dict | None = None,
transport: AsyncHTTPTransport | None = None,
proxy: str | None = None,
follow_redirects: bool = True,
@@ -74,6 +75,7 @@ async def hx_req(
cookies (dict, optional): The cookies to use for the request.
params (dict, optional): The parameters to use for the request.
post_json (dict, optional): The JSON data to use for the request.
+ post_data (dict, optional): The form data to use for the request.
transport (AsyncHTTPTransport, optional): The transport to use for the request.
proxy (str, optional): The proxy to use for the request.
follow_redirects (bool, optional): Whether to follow redirects.
@@ -113,7 +115,7 @@ async def hx_req(
if method == "GET":
response = await client.get(url, cookies=cookies, headers=headers, params=params)
else:
- response = await client.post(url, cookies=cookies, headers=headers, json=post_json, params=params)
+ response = await client.post(url, cookies=cookies, headers=headers, json=post_json, data=post_data, params=params)
response.raise_for_status()
data = response.text
check_data(data, check_keys=check_keys, check_kv=check_kv)