feat: integrate creator hub douyin workflows
This commit is contained in:
@@ -590,19 +590,6 @@ class Gateway:
|
||||
)
|
||||
raise
|
||||
|
||||
def set_douyin_cookies(self, alias: str, input: dict) -> None:
|
||||
cookies_value = input.get("cookies")
|
||||
if not valid_douyin_generation(input) or not valid_cookies(cookies_value):
|
||||
raise RequestError("invalid restricted browser request", 400)
|
||||
cookies = cast(list[dict], cookies_value)
|
||||
with self._alias_lock(alias):
|
||||
self._require_douyin_generation(alias, input)
|
||||
try:
|
||||
self.browser.set_cookies(alias, cookies)
|
||||
self._require_douyin_generation(alias, input)
|
||||
except DouyinError as exc:
|
||||
raise RequestError("restricted browser operation failed") from exc
|
||||
|
||||
def get_douyin(self, alias: str, input: dict) -> dict:
|
||||
if not valid_douyin_generation(input) or not valid_douyin_url(
|
||||
input.get("url", "")
|
||||
@@ -626,6 +613,28 @@ class Gateway:
|
||||
"challenge": response.challenge,
|
||||
}
|
||||
|
||||
def get_douyin_media(self, alias: str, input: dict) -> dict:
|
||||
target = input.get("url", "")
|
||||
if not valid_douyin_generation(input) or not isinstance(target, str):
|
||||
raise RequestError("invalid restricted browser request", 400)
|
||||
with self._alias_lock(alias):
|
||||
self._require_douyin_generation(alias, input)
|
||||
try:
|
||||
response = self.browser.get_media(alias, target)
|
||||
self._require_douyin_generation(alias, input)
|
||||
except DouyinError as exc:
|
||||
LOG.warning(
|
||||
"Douyin media download failed alias=%s reason=%s",
|
||||
alias,
|
||||
str(exc),
|
||||
)
|
||||
raise RequestError("restricted browser media download failed") from exc
|
||||
return {
|
||||
"status": response.status,
|
||||
"content_type": response.content_type,
|
||||
"body_base64": response.body_base64,
|
||||
}
|
||||
|
||||
def douyin_identity(self, alias: str, input: dict) -> dict:
|
||||
expected_account_key = input.get("expected_account_key", "")
|
||||
if (
|
||||
@@ -678,6 +687,8 @@ class Gateway:
|
||||
or action not in ACTIONS
|
||||
):
|
||||
raise RequestError("invalid Douyin action request", 400)
|
||||
if action == "repost":
|
||||
raise RequestError("ACTION_UNAVAILABLE", 409)
|
||||
with self._alias_lock(alias):
|
||||
self._require_douyin_generation(alias, input)
|
||||
self._claim_action(alias)
|
||||
@@ -1120,16 +1131,15 @@ class GatewayHandler(BaseHTTPRequestHandler):
|
||||
gateway.restore_proxy(alias, body)
|
||||
return None
|
||||
match = re.fullmatch(
|
||||
r"/v1/browsers/([a-z0-9][a-z0-9-]{0,31})/douyin/(cookies|get|identity|action|events)",
|
||||
r"/v1/browsers/([a-z0-9][a-z0-9-]{0,31})/douyin/(get|media|identity|action|events)",
|
||||
path,
|
||||
)
|
||||
if match:
|
||||
alias, action = match.groups()
|
||||
if action == "cookies" and method == "POST":
|
||||
gateway.set_douyin_cookies(alias, body)
|
||||
return None
|
||||
if action == "get" and method == "POST":
|
||||
return gateway.get_douyin(alias, body)
|
||||
if action == "media" and method == "POST":
|
||||
return gateway.get_douyin_media(alias, body)
|
||||
if action == "identity" and method == "POST":
|
||||
return gateway.douyin_identity(alias, body)
|
||||
if action == "action" and method == "POST":
|
||||
@@ -1409,57 +1419,6 @@ def valid_douyin_generation(value: dict) -> bool:
|
||||
)
|
||||
|
||||
|
||||
def valid_cookies(cookies: object) -> bool:
|
||||
if not isinstance(cookies, list) or not 1 <= len(cookies) <= 64:
|
||||
return False
|
||||
for cookie in cookies:
|
||||
if not isinstance(cookie, dict):
|
||||
return False
|
||||
allowed = {
|
||||
"name",
|
||||
"value",
|
||||
"domain",
|
||||
"path",
|
||||
"secure",
|
||||
"http_only",
|
||||
"same_site",
|
||||
"expires",
|
||||
}
|
||||
if set(cookie) - allowed:
|
||||
return False
|
||||
name = cookie.get("name")
|
||||
value = cookie.get("value")
|
||||
domain = cookie.get("domain")
|
||||
path_value = cookie.get("path")
|
||||
path = "/" if path_value is None else path_value
|
||||
if (
|
||||
not isinstance(name, str)
|
||||
or not isinstance(value, str)
|
||||
or not isinstance(domain, str)
|
||||
or not isinstance(path, str)
|
||||
):
|
||||
return False
|
||||
if (
|
||||
not name
|
||||
or len(name) > 256
|
||||
or len(value) > 4096
|
||||
or len(domain) > 256
|
||||
or len(path) > 256
|
||||
or domain != domain.lower().strip()
|
||||
or (domain != "douyin.com" and not domain.endswith(".douyin.com"))
|
||||
or not path.startswith("/")
|
||||
or has_control(name)
|
||||
or has_control(value)
|
||||
or has_control(path)
|
||||
or (cookie.get("same_site", "") not in {"", "Lax", "Strict", "None"})
|
||||
):
|
||||
return False
|
||||
expires = cookie.get("expires", 0)
|
||||
if type(expires) not in (int, float) or expires < 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def valid_douyin_url(raw: object) -> bool:
|
||||
if not isinstance(raw, str):
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user