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