优化错误码体系并支持不可重试状态与无媒体内容精细化识别

This commit is contained in:
ucmao
2026-09-10 06:49:24 +08:00
parent ecab06ddb5
commit 9077b030fa
6 changed files with 73 additions and 19 deletions
+11 -3
View File
@@ -134,12 +134,20 @@ def _execute_parse(text, access):
if platform in ('视频号', '微信视频号'):
response, status = make_response(400, '解析失败:该链接需要配置腾讯元宝 YUANBAO_COOKIE 凭证后重试', None, False, 'WECHAT_CHANNELS_COOKIE_REQUIRED'), 400
return response, status
terminal_detail = getattr(parser, '_terminal_filter_detail', None)
terminal_detail = getattr(parser, 'terminal_error', None) or getattr(parser, '_terminal_filter_detail', None)
if isinstance(terminal_detail, dict):
detail_msg = terminal_detail.get('detail_msg') or terminal_detail.get('notice')
detail_msg = terminal_detail.get('detail_msg') or terminal_detail.get('notice') or '该内容可能为私密/日常作品或已被作者删除'
if isinstance(detail_msg, str) and detail_msg.strip():
response, status = make_response(400, detail_msg.strip(), None, False, 'MEDIA_NOT_FOUND'), 400
response, status = make_response(400, detail_msg.strip(), None, False, 'MEDIA_DELETED_OR_PRIVATE'), 400
return response, status
is_no_media = getattr(parser, 'no_media_in_content', False)
if is_no_media is True or (
type(is_no_media).__name__ not in ('Mock', 'MagicMock')
and platform in ('豆包', '通义千问', '腾讯元宝', '夸克AI', '小云雀AI')
and (content_data.get('title') or content_data.get('desc'))
):
response, status = make_response(400, '该分享内容仅包含文本对话,未包含图片或视频资源', None, False, 'NO_MEDIA_IN_CONTENT'), 400
return response, status
response, status = make_response(400, '提取媒体内容失败,请检查链接或稍后重试', None, False, 'MEDIA_NOT_FOUND'), 400
return response, status
+2
View File
@@ -10,6 +10,8 @@ class BaseParser:
self.headers = None
self.html_content = None
self.session = requests.Session()
self.terminal_error = None
self.no_media_in_content = False
def get_real_video_url(self):
raise NotImplementedError
+3
View File
@@ -133,6 +133,9 @@ class DoubaoParser(BaseParser):
if clean_video_urls:
video_urls = clean_video_urls
if not video_urls and not image_urls:
self.no_media_in_content = True
return {
"title": self._find_title(roots) or "豆包对话分享",
"video_url": video_urls[0] if video_urls else None,
+1
View File
@@ -256,6 +256,7 @@ class DouyinParser(BaseParser):
filter_reason = (data.get('filter_detail') or {}).get('filter_reason') or "terminal_status"
logger.info(f"抖音接口返回明确的不可重试终端状态 ({filter_reason}: {filter_msg}),立即终止重试: {base_api.split('?')[0]}")
self._terminal_filter_detail = data.get('filter_detail') or {"filter_reason": filter_reason, "detail_msg": filter_msg}
self.terminal_error = self._terminal_filter_detail
return None
except Exception as e:
logger.debug(f"请求抖音接口异常 (第 {attempt + 1}/{attempts} 次): {e}")
+41 -16
View File
@@ -132,19 +132,24 @@
<td class="px-3.5 py-2.5 text-slate-600">请求是否成功:<code class="text-emerald-700 bg-emerald-50 px-1.5 py-0.5 rounded font-mono border border-emerald-200/60">true</code> 成功;<code class="text-rose-700 bg-rose-50 px-1.5 py-0.5 rounded font-mono border border-rose-200/60">false</code> 失败。</td>
</tr>
<tr>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">code</code></td>
<td class="px-3.5 py-2.5 text-slate-500">Integer / String</td>
<td class="px-3.5 py-2.5 text-slate-600">状态码。成功时固定为 <code class="font-mono text-xs px-1.5 py-0.5 rounded bg-slate-100 text-slate-800 border border-slate-200/50">200</code>,失败时为对应错误标识。</td>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">retcode</code></td>
<td class="px-3.5 py-2.5 text-slate-500">Integer</td>
<td class="px-3.5 py-2.5 text-slate-600">业务响应状态码。成功时为 <code class="font-mono text-xs px-1.5 py-0.5 rounded bg-slate-100 text-slate-800 border border-slate-200/50">200</code>,失败时为对应 HTTP 状态(如 400, 401, 402, 429, 503 等)。</td>
</tr>
<tr>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">message</code></td>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">retdesc</code></td>
<td class="px-3.5 py-2.5 text-slate-500">String</td>
<td class="px-3.5 py-2.5 text-slate-600">提示信息或异常错误原因。</td>
<td class="px-3.5 py-2.5 text-slate-600">提示信息或官方/系统异常原因说明。</td>
</tr>
<tr>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">error_code</code></td>
<td class="px-3.5 py-2.5 text-slate-500">String</td>
<td class="px-3.5 py-2.5 text-slate-600">失败时返回的机器可读标准错误标识(如 <code class="font-mono text-[11px]">MEDIA_DELETED_OR_PRIVATE</code>、<code class="font-mono text-[11px]">NO_MEDIA_IN_CONTENT</code>、<code class="font-mono text-[11px]">RATE_LIMITED</code> 等,详见第 5 节)。</td>
</tr>
<tr>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">data</code></td>
<td class="px-3.5 py-2.5 text-slate-500">Object</td>
<td class="px-3.5 py-2.5 text-slate-600">解析成功返回的媒体实体数据对象。</td>
<td class="px-3.5 py-2.5 text-slate-500">Object / null</td>
<td class="px-3.5 py-2.5 text-slate-600">解析成功返回的媒体实体数据对象,失败时为 <code class="font-mono text-xs text-slate-400">null</code>。</td>
</tr>
</tbody>
</table>
@@ -267,42 +272,62 @@
<tr>
<td class="px-3.5 py-2.5"><span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-mono font-medium bg-rose-50 text-rose-700 border border-rose-200/60">400</span></td>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">PLATFORM_NOT_SUPPORTED</code></td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">目标平台域名不在支持列表中,或属于仅好友可见的私密作品。</td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">目标平台域名不在支持列表中。</td>
</tr>
<tr>
<td class="px-3.5 py-2.5"><span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-mono font-medium bg-rose-50 text-rose-700 border border-rose-200/60">400</span></td>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">MEDIA_DELETED_OR_PRIVATE</code></td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">目标内容为私密作品、仅好友可见的日常/动态,或已被作者删除下架。</td>
</tr>
<tr>
<td class="px-3.5 py-2.5"><span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-mono font-medium bg-rose-50 text-rose-700 border border-rose-200/60">400</span></td>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">NO_MEDIA_IN_CONTENT</code></td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">目标分享内容(如 AI 问答对话)仅包含文本,未包含图片或视频多媒体资源。</td>
</tr>
<tr>
<td class="px-3.5 py-2.5"><span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-mono font-medium bg-rose-50 text-rose-700 border border-rose-200/60">400</span></td>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">MEDIA_NOT_FOUND</code></td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">源视频已被创作者删除、设为私密,或触发了平台风控验证。</td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">未能从该链接提取到媒体内容,请检查链接有效性或稍后重试。</td>
</tr>
<tr>
<td class="px-3.5 py-2.5"><span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-mono font-medium bg-rose-50 text-rose-700 border border-rose-200/60">400</span></td>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">COOKIE_REQUIRED</code></td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">目标平台需要服务端 Cookie。请在管理后台「系统设置 - Cookie管理」中配置有效凭据。</td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">目标平台需要服务端 Cookie 凭据校验(如小红书 <code class="font-mono text-[11px]">XIAOHONGSHU_COOKIE_REQUIRED</code>、拼多多 <code class="font-mono text-[11px]">PINDUODUO_COOKIE_REQUIRED</code>、微信视频号 <code class="font-mono text-[11px]">WECHAT_CHANNELS_COOKIE_REQUIRED</code> 等)。请在管理后台「系统设置 - Cookie管理」中配置有效凭据。</td>
</tr>
<tr>
<td class="px-3.5 py-2.5"><span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-mono font-medium bg-amber-50 text-amber-700 border border-amber-200/60">401</span></td>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">INVALID_KEY</code></td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">未提供 API Key,或 Key 已被禁用、删除。</td>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">API_KEY_REQUIRED</code></td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">请求头或查询参数中未提供 API Key 凭证。</td>
</tr>
<tr>
<td class="px-3.5 py-2.5"><span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-mono font-medium bg-amber-50 text-amber-700 border border-amber-200/60">401</span></td>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">EXPIRED_USER</code></td>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">INVALID_API_KEY</code></td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">提供的 API Key 不存在或已被删除。</td>
</tr>
<tr>
<td class="px-3.5 py-2.5"><span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-mono font-medium bg-amber-50 text-amber-700 border border-amber-200/60">403</span></td>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">API_KEY_DISABLED</code></td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">该 API Key 或关联的客户账号已被管理员停用。</td>
</tr>
<tr>
<td class="px-3.5 py-2.5"><span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-mono font-medium bg-amber-50 text-amber-700 border border-amber-200/60">403</span></td>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">ACCOUNT_EXPIRED</code></td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">账号使用期限已到期,请联系管理员续期。</td>
</tr>
<tr>
<td class="px-3.5 py-2.5"><span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-mono font-medium bg-amber-50 text-amber-700 border border-amber-200/60">402</span></td>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">INSUFFICIENT_CREDITS</code></td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">账号可用点数已耗尽(剩余 0 点),请充值或调整额度。</td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">账号可用解析积分已耗尽(剩余 0 点),请充值或调整额度。</td>
</tr>
<tr>
<td class="px-3.5 py-2.5"><span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-mono font-medium bg-yellow-50 text-yellow-800 border border-yellow-200/60">429</span></td>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">RATE_LIMITED</code></td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">超出并发 QPS 阈值,请在客户端增加重试与指数退避。</td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">超出当前账号、API Key 或平台的并发 QPS 阈值,请在客户端增加重试与指数退避。</td>
</tr>
<tr>
<td class="px-3.5 py-2.5"><span class="inline-flex items-center px-1.5 py-0.5 rounded text-xs font-mono font-medium bg-slate-100 text-slate-700 border border-slate-200/60">503</span></td>
<td class="px-3.5 py-2.5"><code class="font-mono text-xs font-semibold text-slate-800 bg-slate-100/90 px-2 py-0.5 rounded border border-slate-200/60">API_DISABLED</code></td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">系统全局 API 服务被超级管理员临时维护关闭。</td>
<td class="px-3.5 py-2.5 text-slate-600 leading-relaxed">系统全局 API 服务或特定平台被管理员临时维护关闭。</td>
</tr>
</tbody>
</table>
+15
View File
@@ -124,6 +124,21 @@ class ApiContractTest(unittest.TestCase):
with self.subTest(url=url):
self.assert_bad_request(self.post_with_parser(empty_parser, url), message, error_code)
def test_terminal_deleted_or_private_error_contract(self):
terminal_parser = self.parser(video_url=None, video_list=[], image_list=[])
terminal_parser.terminal_error = {
"filter_reason": "status_self_see",
"detail_msg": "因作品权限或已被删除,无法观看,去看看其他作品吧"
}
response = self.post_with_parser(terminal_parser, "https://www.douyin.com/video/1")
self.assert_bad_request(response, "因作品权限或已被删除,无法观看,去看看其他作品吧", "MEDIA_DELETED_OR_PRIVATE")
def test_no_media_in_content_error_contract(self):
text_parser = self.parser(video_url=None, video_list=[], image_list=[], title="豆包对话分享")
text_parser.no_media_in_content = True
response = self.post_with_parser(text_parser, "https://www.doubao.com/thread/123")
self.assert_bad_request(response, "该分享内容仅包含文本对话,未包含图片或视频资源", "NO_MEDIA_IN_CONTENT")
def test_normalizes_and_deduplicates_media_urls(self):
parser = self.parser(
video_url="http://cdn.example/main.mp4",