2 from .common
import InfoExtractor
10 from ..utils
.traversal
import traverse_obj
13 class XiaoHongShuIE(InfoExtractor
):
14 _VALID_URL
= r
'https?://www\.xiaohongshu\.com/(?:explore|discovery/item)/(?P<id>[\da-f]+)'
17 'url': 'https://www.xiaohongshu.com/explore/6411cf99000000001300b6d9',
18 'md5': '2a87a77ddbedcaeeda8d7eae61b61228',
20 'id': '6411cf99000000001300b6d9',
22 'uploader_id': '5c31698d0000000007018a31',
23 'description': '#今日快乐今日发[话题]# #吃货薯看这里[话题]# #香妃蛋糕[话题]# #小五卷蛋糕[话题]# #新手蛋糕卷[话题]#',
24 'title': '香妃蛋糕也太香了吧🔥不需要卷❗️绝对的友好',
25 'tags': ['今日快乐今日发', '吃货薯看这里', '香妃蛋糕', '小五卷蛋糕', '新手蛋糕卷'],
27 'thumbnail': r
're:https?://sns-webpic-qc\.xhscdn\.com/\d+/[a-z0-9]+/[\w]+',
30 'url': 'https://www.xiaohongshu.com/discovery/item/674051740000000007027a15?xsec_token=CBgeL8Dxd1ZWBhwqRd568gAZ_iwG-9JIf9tnApNmteU2E=',
32 'id': '674051740000000007027a15',
35 'uploader_id': '63439913000000001901f49a',
37 'description': '#广州[话题]# #深圳[话题]# #香港[话题]# #街头采访[话题]# #是你喜欢的类型[话题]#',
38 'thumbnail': r
're:https?://sns-webpic-qc\.xhscdn\.com/\d+/[\da-f]+/[^/]+',
39 'tags': ['广州', '深圳', '香港', '街头采访', '是你喜欢的类型'],
43 def _real_extract(self
, url
):
44 display_id
= self
._match
_id
(url
)
45 webpage
= self
._download
_webpage
(url
, display_id
)
46 initial_state
= self
._search
_json
(
47 r
'window\.__INITIAL_STATE__\s*=', webpage
, 'initial state', display_id
, transform_source
=js_to_json
)
49 note_info
= traverse_obj(initial_state
, ('note', 'noteDetailMap', display_id
, 'note'))
50 video_info
= traverse_obj(note_info
, ('video', 'media', 'stream', ..., ...))
53 for info
in video_info
:
54 format_info
= traverse_obj(info
, {
55 'fps': ('fps', {int_or_none}
),
56 'width': ('width', {int_or_none}
),
57 'height': ('height', {int_or_none}
),
58 'vcodec': ('videoCodec', {str}
),
59 'acodec': ('audioCodec', {str}
),
60 'abr': ('audioBitrate', {int_or_none(scale
=1000)}),
61 'vbr': ('videoBitrate', {int_or_none(scale
=1000)}),
62 'audio_channels': ('audioChannels', {int_or_none}
),
63 'tbr': ('avgBitrate', {int_or_none(scale
=1000)}),
64 'format': ('qualityType', {str}
),
65 'filesize': ('size', {int_or_none}
),
66 'duration': ('duration', {float_or_none(scale
=1000)}),
69 formats
.extend(traverse_obj(info
, (('masterUrl', ('backupUrls', ...)), {
70 lambda u
: url_or_none(u
) and {'url': u
, **format_info
}})))
72 if origin_key
:= traverse_obj(note_info
, ('video', 'consumer', 'originVideoKey', {str}
)):
73 # Not using a head request because of false negatives
74 urlh
= self
._request
_webpage
(
75 f
'https://sns-video-bd.xhscdn.com/{origin_key}', display_id
,
76 'Checking original video availability', 'Original video is not available', fatal
=False)
79 'format_id': 'direct',
80 'ext': urlhandle_detect_ext(urlh
, default
='mp4'),
81 'filesize': int_or_none(urlh
.get_header('Content-Length')),
87 for image_info
in traverse_obj(note_info
, ('imageList', ...)):
88 thumbnail_info
= traverse_obj(image_info
, {
89 'height': ('height', {int_or_none}
),
90 'width': ('width', {int_or_none}
),
92 for thumb_url
in traverse_obj(image_info
, (('urlDefault', 'urlPre'), {url_or_none}
)):
101 'thumbnails': thumbnails
,
102 'title': self
._html
_search
_meta
(['og:title'], webpage
, default
=None),
103 **traverse_obj(note_info
, {
104 'title': ('title', {str}
),
105 'description': ('desc', {str}
),
106 'tags': ('tagList', ..., 'name', {str}
),
107 'uploader_id': ('user', 'userId', {str}
),