Release 2024.12.23
[yt-dlp.git] / yt_dlp / extractor / xiaohongshu.py
blob1280ca6a9c1f0f72620cb849897eb5696d427646
2 from .common import InfoExtractor
3 from ..utils import (
4 float_or_none,
5 int_or_none,
6 js_to_json,
7 url_or_none,
9 from ..utils.traversal import traverse_obj
12 class XiaoHongShuIE(InfoExtractor):
13 _VALID_URL = r'https?://www\.xiaohongshu\.com/explore/(?P<id>[\da-f]+)'
14 IE_DESC = '小红书'
15 _TESTS = [{
16 'url': 'https://www.xiaohongshu.com/explore/6411cf99000000001300b6d9',
17 'md5': '2a87a77ddbedcaeeda8d7eae61b61228',
18 'info_dict': {
19 'id': '6411cf99000000001300b6d9',
20 'ext': 'mp4',
21 'uploader_id': '5c31698d0000000007018a31',
22 'description': '#今日快乐今日发[话题]# #吃货薯看这里[话题]# #香妃蛋糕[话题]# #小五卷蛋糕[话题]# #新手蛋糕卷[话题]#',
23 'title': '香妃蛋糕也太香了吧🔥不需要卷❗️绝对的友好',
24 'tags': ['今日快乐今日发', '吃货薯看这里', '香妃蛋糕', '小五卷蛋糕', '新手蛋糕卷'],
25 'duration': 101.726,
26 'thumbnail': r're:https?://sns-webpic-qc\.xhscdn\.com/\d+/[a-z0-9]+/[\w]+',
30 def _real_extract(self, url):
31 display_id = self._match_id(url)
32 webpage = self._download_webpage(url, display_id)
33 initial_state = self._search_json(
34 r'window\.__INITIAL_STATE__\s*=', webpage, 'initial state', display_id, transform_source=js_to_json)
36 note_info = traverse_obj(initial_state, ('note', 'noteDetailMap', display_id, 'note'))
37 video_info = traverse_obj(note_info, ('video', 'media', 'stream', ('h264', 'av1', 'h265'), ...))
39 formats = []
40 for info in video_info:
41 format_info = traverse_obj(info, {
42 'fps': ('fps', {int_or_none}),
43 'width': ('width', {int_or_none}),
44 'height': ('height', {int_or_none}),
45 'vcodec': ('videoCodec', {str}),
46 'acodec': ('audioCodec', {str}),
47 'abr': ('audioBitrate', {int_or_none}),
48 'vbr': ('videoBitrate', {int_or_none}),
49 'audio_channels': ('audioChannels', {int_or_none}),
50 'tbr': ('avgBitrate', {int_or_none}),
51 'format': ('qualityType', {str}),
52 'filesize': ('size', {int_or_none}),
53 'duration': ('duration', {float_or_none(scale=1000)}),
56 formats.extend(traverse_obj(info, (('mediaUrl', ('backupUrls', ...)), {
57 lambda u: url_or_none(u) and {'url': u, **format_info}})))
59 thumbnails = []
60 for image_info in traverse_obj(note_info, ('imageList', ...)):
61 thumbnail_info = traverse_obj(image_info, {
62 'height': ('height', {int_or_none}),
63 'width': ('width', {int_or_none}),
65 for thumb_url in traverse_obj(image_info, (('urlDefault', 'urlPre'), {url_or_none})):
66 thumbnails.append({
67 'url': thumb_url,
68 **thumbnail_info,
71 return {
72 'id': display_id,
73 'formats': formats,
74 'thumbnails': thumbnails,
75 'title': self._html_search_meta(['og:title'], webpage, default=None),
76 **traverse_obj(note_info, {
77 'title': ('title', {str}),
78 'description': ('desc', {str}),
79 'tags': ('tagList', ..., 'name', {str}),
80 'uploader_id': ('user', 'userId', {str}),
81 }),