Skip to content

Commit ded4273

Browse files
authored
Merge pull request #998 from kevin9327/fix/douyin-note-url
fix(douyin): 解析图文作品 /note/ 链接中的作品 ID
2 parents 4357891 + efcf8ab commit ded4273

2 files changed

Lines changed: 94 additions & 5 deletions

File tree

‎media_platform/douyin/help.py‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ def parse_video_info_from_url(url: str) -> VideoUrlInfo:
106106
2. Link with modal_id parameter:
107107
- https://www.douyin.com/user/MS4wLjABAAAATJPY7LAlaa5X-c8uNdWkvz0jUGgpw4eeXIwu_8BhvqE?modal_id=7525082444551310602
108108
- https://www.douyin.com/root/search/python?modal_id=7471165520058862848
109-
3. Short link: https://v.douyin.com/iF12345ABC/ (requires client parsing)
110-
4. Pure ID: 7525082444551310602
109+
3. Image post (图文) link: https://www.douyin.com/note/7525082444551310602
110+
4. Short link: https://v.douyin.com/iF12345ABC/ (requires client parsing)
111+
5. Pure ID: 7525082444551310602
111112
112113
Args:
113114
url: Douyin video link or ID
@@ -119,7 +120,7 @@ def parse_video_info_from_url(url: str) -> VideoUrlInfo:
119120
return VideoUrlInfo(aweme_id=url, url_type="normal")
120121

121122
# Check if it's a short link (v.douyin.com)
122-
if "v.douyin.com" in url or url.startswith("http") and len(url) < 50 and "video" not in url:
123+
if "v.douyin.com" in url:
123124
return VideoUrlInfo(aweme_id="", url_type="short") # Requires client parsing
124125

125126
# Try to extract modal_id from URL parameters
@@ -128,13 +129,18 @@ def parse_video_info_from_url(url: str) -> VideoUrlInfo:
128129
if modal_id:
129130
return VideoUrlInfo(aweme_id=modal_id, url_type="modal")
130131

131-
# Extract ID from standard video URL: /video/number
132-
video_pattern = r'/video/(\d+)'
132+
# Extract ID from a video or image-post (图文) URL: /video/number or /note/number,
133+
# including the iesdouyin.com /share/video/ and /share/note/ pages short links resolve to
134+
video_pattern = r'/(?:video|note)/(\d+)'
133135
match = re.search(video_pattern, url)
134136
if match:
135137
aweme_id = match.group(1)
136138
return VideoUrlInfo(aweme_id=aweme_id, url_type="normal")
137139

140+
# Other short http links without an ID in the path still need client parsing
141+
if url.startswith("http") and len(url) < 50:
142+
return VideoUrlInfo(aweme_id="", url_type="short")
143+
138144
raise ValueError(f"Unable to parse video ID from URL: {url}")
139145

140146

‎tests/test_douyin_url_parse.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (c) 2025 relakkes@gmail.com
3+
#
4+
# This file is part of MediaCrawler project.
5+
# Repository: https://github.com/NanmiCoder/MediaCrawler/blob/main/tests/test_douyin_url_parse.py
6+
# GitHub: https://github.com/NanmiCoder
7+
# Licensed under NON-COMMERCIAL LEARNING LICENSE 1.1
8+
#
9+
10+
"""抖音作品输入解析的回归测试。
11+
12+
图文作品的链接是 ``/note/<aweme_id>``,短链跳转后落到 iesdouyin.com 的
13+
``/share/note/<aweme_id>/``。它们和视频一样都带作品 ID,应直接解析出来,
14+
而不是被当成短链再去请求一次。
15+
16+
这里只测纯函数,不发网络请求。
17+
"""
18+
19+
from __future__ import annotations
20+
21+
import pytest
22+
23+
from media_platform.douyin.help import parse_video_info_from_url
24+
25+
26+
def test_pure_aweme_id_is_normal():
27+
info = parse_video_info_from_url("7525082444551310602")
28+
29+
assert info.aweme_id == "7525082444551310602"
30+
assert info.url_type == "normal"
31+
32+
33+
def test_video_url_is_normal():
34+
info = parse_video_info_from_url("https://www.douyin.com/video/7525082444551310602")
35+
36+
assert info.aweme_id == "7525082444551310602"
37+
assert info.url_type == "normal"
38+
39+
40+
def test_modal_id_url():
41+
info = parse_video_info_from_url(
42+
"https://www.douyin.com/root/search/python?modal_id=7471165520058862848"
43+
)
44+
45+
assert info.aweme_id == "7471165520058862848"
46+
assert info.url_type == "modal"
47+
48+
49+
def test_note_url_is_normal():
50+
"""图文作品页 /note/<id> 不能被当成短链"""
51+
info = parse_video_info_from_url("https://www.douyin.com/note/7525082444551310602")
52+
53+
assert info.aweme_id == "7525082444551310602"
54+
assert info.url_type == "normal"
55+
56+
57+
@pytest.mark.parametrize(
58+
"url",
59+
[
60+
"https://www.iesdouyin.com/share/note/7525082444551310602/?region=CN&mid=7525082500000000000",
61+
"https://www.iesdouyin.com/share/video/7525082444551310602/?region=CN&mid=7525082500000000000",
62+
],
63+
)
64+
def test_resolved_share_url_is_normal(url):
65+
"""短链跳转后的 share 页(视频或图文)都要能取到作品 ID"""
66+
info = parse_video_info_from_url(url)
67+
68+
assert info.aweme_id == "7525082444551310602"
69+
assert info.url_type == "normal"
70+
71+
72+
def test_share_short_link_is_marked_short():
73+
info = parse_video_info_from_url("https://v.douyin.com/iF12345ABC/")
74+
75+
assert info.aweme_id == ""
76+
assert info.url_type == "short"
77+
78+
79+
def test_unknown_long_url_raises():
80+
with pytest.raises(ValueError):
81+
parse_video_info_from_url(
82+
"https://www.douyin.com/user/MS4wLjABAAAATJPY7LAlaa5X-c8uNdWkvz0jUGgpw4eeXIwu_8BhvqE"
83+
)

0 commit comments

Comments
 (0)