Skip to content

Commit

Permalink
perf: 优化tiktok的utils代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnserf-Seed committed May 23, 2024
1 parent 4266c1d commit 78eb9f0
Showing 1 changed file with 20 additions and 34 deletions.
54 changes: 20 additions & 34 deletions f2/apps/tiktok/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,15 @@ class TokenManager(BaseCrawler):
ttwid_conf = ClientConfManager.ttwid()
odin_tt_conf = ClientConfManager.odin_tt()
proxies = ClientConfManager.proxies()
user_agent = ClientConfManager.user_agent()
mstoken_headers = {
"Content-Type": "application/json",
"User-Agent": ClientConfManager.user_agent(),
"User-Agent": user_agent,
}
ttwid_headers = {
"Cookie": ttwid_conf.get("cookie"),
"Content-Type": "text/plain",
"User-Agent": ClientConfManager.user_agent(),
"User-Agent": user_agent,
}

def __init__(self):
Expand Down Expand Up @@ -204,7 +205,6 @@ def gen_real_msToken(cls) -> str:
_("{0}。链接:{1} 代理:{2},异常类名:{3},异常详细信息:{4}").format(
_("状态码错误"),
instance.token_conf["url"],
cls.token_conf["proxies"],
cls.proxies,
cls.__name__,
exc,
Expand Down Expand Up @@ -309,22 +309,15 @@ def gen_ttwid(cls) -> str:

except httpx.HTTPStatusError as exc:
logger.error(traceback.format_exc())
if exc.response.status_code == 401:
raise APIUnauthorizedError(
_(
"参数验证失败,请更新 F2 配置文件中的 {0},以匹配 {1} 新规则"
).format("ttwid", "tiktok")
)
elif exc.response.status_code == 404:
raise APINotFoundError(_("{0} 无法找到API端点").format("ttwid"))
else:
raise APIResponseError(
_("链接:{0},状态码 {1}:{2} ").format(
exc.response.url,
exc.response.status_code,
exc.response.text,
)
raise APIResponseError(
_("{0}。链接:{1} 代理:{2},异常类名:{3},异常详细信息:{4}").format(
_("状态码错误"),
instance.ttwid_conf["url"],
cls.proxies,
cls.__name__,
exc,
)
)

@classmethod
def gen_odin_tt(cls) -> str:
Expand Down Expand Up @@ -404,22 +397,15 @@ def gen_odin_tt(cls) -> str:

except httpx.HTTPStatusError as exc:
logger.error(traceback.format_exc())
if exc.response.status_code == 401:
raise APIUnauthorizedError(
_(
"参数验证失败,请更新 F2 配置文件中的 {0},以匹配 {1} 新规则"
).format("odin_tt", "tiktok")
)
elif exc.response.status_code == 404:
raise APINotFoundError(_("{0} 无法找到API端点").format("odin_tt"))
else:
raise APIResponseError(
_("链接:{0},状态码 {1}:{2} ").format(
exc.response.url,
exc.response.status_code,
exc.response.text,
)
raise APIResponseError(
_("{0}。链接:{1} 代理:{2},异常类名:{3},异常详细信息:{4}").format(
_("状态码错误"),
instance.odin_tt_conf["url"],
cls.proxies,
cls.__name__,
exc,
)
)


class XBogusManager:
Expand Down Expand Up @@ -1016,7 +1002,7 @@ async def get_all_aweme_id(cls, urls: list) -> list:

def format_file_name(
naming_template: str,
aweme_data: dict = {},
aweme_data: dict = ...,
custom_fields: dict = {},
) -> str:
"""
Expand Down

0 comments on commit 78eb9f0

Please sign in to comment.