[ie/cwtv:movie] Add extractor (#12227)
[yt-dlp.git] / yt_dlp / extractor / xiaohongshu.py
blob4a6129ba6ac5216191c073c8ac651b6f204a411b
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,
8 urlhandle_detect_ext,
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]+)'
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]+',
29 }, {
30 'url': 'https://www.xiaohongshu.com/discovery/item/674051740000000007027a15?xsec_token=CBgeL8Dxd1ZWBhwqRd568gAZ_iwG-9JIf9tnApNmteU2E=',
31 'info_dict': {
32 'id': '674051740000000007027a15',
33 'ext': 'mp4',
34 'title': '相互喜欢就可以了',
35 'uploader_id': '63439913000000001901f49a',
36 'duration': 28.073,
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', ..., ...))
52 formats = []
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)
77 if urlh:
78 formats.append({
79 'format_id': 'direct',
80 'ext': urlhandle_detect_ext(urlh, default='mp4'),
81 'filesize': int_or_none(urlh.get_header('Content-Length')),
82 'url': urlh.url,
83 'quality': 1,
86 thumbnails = []
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})):
93 thumbnails.append({
94 'url': thumb_url,
95 **thumbnail_info,
98 return {
99 'id': display_id,
100 'formats': formats,
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}),