[ie/wistia] Support password-protected videos (#11100)
[yt-dlp3.git] / yt_dlp / extractor / pinterest.py
blobf0b38893b2fa76ede64baf4619ae6b891628381c
1 import json
3 from .common import InfoExtractor
4 from ..utils import (
5 determine_ext,
6 float_or_none,
7 int_or_none,
8 str_or_none,
9 strip_or_none,
10 traverse_obj,
11 unified_timestamp,
12 url_or_none,
16 class PinterestBaseIE(InfoExtractor):
17 _VALID_URL_BASE = r'''(?x)
18 https?://(?:[^/]+\.)?pinterest\.(?:
19 com|fr|de|ch|jp|cl|ca|it|co\.uk|nz|ru|com\.au|at|pt|co\.kr|es|com\.mx|
20 dk|ph|th|com\.uy|co|nl|info|kr|ie|vn|com\.vn|ec|mx|in|pe|co\.at|hu|
21 co\.in|co\.nz|id|com\.ec|com\.py|tw|be|uk|com\.bo|com\.pe)'''
23 def _call_api(self, resource, video_id, options):
24 return self._download_json(
25 f'https://www.pinterest.com/resource/{resource}Resource/get/',
26 video_id, f'Download {resource} JSON metadata', query={
27 'data': json.dumps({'options': options}),
28 })['resource_response']
30 def _extract_video(self, data, extract_formats=True):
31 video_id = data['id']
32 thumbnails = []
33 images = data.get('images')
34 if isinstance(images, dict):
35 for thumbnail in images.values():
36 if not isinstance(thumbnail, dict):
37 continue
38 thumbnail_url = url_or_none(thumbnail.get('url'))
39 if not thumbnail_url:
40 continue
41 thumbnails.append({
42 'url': thumbnail_url,
43 'width': int_or_none(thumbnail.get('width')),
44 'height': int_or_none(thumbnail.get('height')),
47 info = {
48 'title': strip_or_none(traverse_obj(data, 'title', 'grid_title', default='')),
49 'description': traverse_obj(data, 'seo_description', 'description'),
50 'timestamp': unified_timestamp(data.get('created_at')),
51 'thumbnails': thumbnails,
52 'uploader': traverse_obj(data, ('closeup_attribution', 'full_name')),
53 'uploader_id': str_or_none(traverse_obj(data, ('closeup_attribution', 'id'))),
54 'repost_count': int_or_none(data.get('repin_count')),
55 'comment_count': int_or_none(data.get('comment_count')),
56 'categories': traverse_obj(data, ('pin_join', 'visual_annotation'), expected_type=list),
57 'tags': traverse_obj(data, 'hashtags', expected_type=list),
60 urls = []
61 formats = []
62 duration = None
63 domain = data.get('domain', '')
64 if domain.lower() != 'uploaded by user' and traverse_obj(data, ('embed', 'src')):
65 if not info['title']:
66 info['title'] = None
67 return {
68 '_type': 'url_transparent',
69 'url': data['embed']['src'],
70 **info,
73 elif extract_formats:
74 video_list = traverse_obj(
75 data, ('videos', 'video_list'),
76 ('story_pin_data', 'pages', ..., 'blocks', ..., 'video', 'video_list'),
77 expected_type=dict, get_all=False, default={})
78 for format_id, format_dict in video_list.items():
79 if not isinstance(format_dict, dict):
80 continue
81 format_url = url_or_none(format_dict.get('url'))
82 if not format_url or format_url in urls:
83 continue
84 urls.append(format_url)
85 duration = float_or_none(format_dict.get('duration'), scale=1000)
86 ext = determine_ext(format_url)
87 if 'hls' in format_id.lower() or ext == 'm3u8':
88 formats.extend(self._extract_m3u8_formats(
89 format_url, video_id, 'mp4', entry_protocol='m3u8_native',
90 m3u8_id=format_id, fatal=False))
91 else:
92 formats.append({
93 'url': format_url,
94 'format_id': format_id,
95 'width': int_or_none(format_dict.get('width')),
96 'height': int_or_none(format_dict.get('height')),
97 'duration': duration,
100 return {
101 'id': video_id,
102 'formats': formats,
103 'duration': duration,
104 'webpage_url': f'https://www.pinterest.com/pin/{video_id}/',
105 'extractor_key': PinterestIE.ie_key(),
106 'extractor': PinterestIE.IE_NAME,
107 **info,
111 class PinterestIE(PinterestBaseIE):
112 _VALID_URL = rf'{PinterestBaseIE._VALID_URL_BASE}/pin/(?:[\w-]+--)?(?P<id>\d+)'
113 _TESTS = [{
114 # formats found in data['videos']
115 'url': 'https://www.pinterest.com/pin/664281013778109217/',
116 'md5': '6550c2af85d6d9f3fe3b88954d1577fc',
117 'info_dict': {
118 'id': '664281013778109217',
119 'ext': 'mp4',
120 'title': 'Origami',
121 'description': 'md5:e29801cab7d741ea8c741bc50c8d00ab',
122 'duration': 57.7,
123 'timestamp': 1593073622,
124 'upload_date': '20200625',
125 'repost_count': int,
126 'comment_count': int,
127 'categories': list,
128 'tags': list,
129 'thumbnail': r're:^https?://.*\.(?:jpg|png)$',
131 }, {
132 # formats found in data['story_pin_data']
133 'url': 'https://www.pinterest.com/pin/1084663891475263837/',
134 'md5': '069ac19919ab9e1e13fa60de46290b03',
135 'info_dict': {
136 'id': '1084663891475263837',
137 'ext': 'mp4',
138 'title': 'Gadget, Cool products, Amazon product, technology, Kitchen gadgets',
139 'description': 'md5:d0a4b6ae996ff0c6eed83bc869598d13',
140 'uploader': 'CoolCrazyGadgets',
141 'uploader_id': '1084664028912989237',
142 'upload_date': '20211003',
143 'timestamp': 1633246654.0,
144 'duration': 14.9,
145 'comment_count': int,
146 'repost_count': int,
147 'categories': 'count:9',
148 'tags': list,
149 'thumbnail': r're:^https?://.*\.(?:jpg|png)$',
151 }, {
152 # vimeo.com embed
153 'url': 'https://www.pinterest.ca/pin/441282463481903715/',
154 'info_dict': {
155 'id': '111691128',
156 'ext': 'mp4',
157 'title': 'Tonite Let\'s All Make Love In London (1967)',
158 'description': 'md5:8190f37b3926807809ec57ec21aa77b2',
159 'uploader': 'Vimeo',
160 'uploader_id': '473792960706651251',
161 'upload_date': '20180120',
162 'timestamp': 1516409040,
163 'duration': 3404,
164 'comment_count': int,
165 'repost_count': int,
166 'categories': 'count:9',
167 'tags': [],
168 'thumbnail': r're:^https?://.*\.(?:jpg|png)$',
169 'uploader_url': 'https://vimeo.com/willardandrade',
171 'params': {
172 'skip_download': 'm3u8',
174 }, {
175 'url': 'https://co.pinterest.com/pin/824721750502199491/',
176 'only_matching': True,
179 'url': 'https://pinterest.com/pin/dive-into-serenity-blue-lagoon-pedi-nails-for-a-tranquil-and-refreshing-spa-experience-video-in-2024--2885187256207927',
180 'info_dict': {
181 'id': '2885187256207927',
182 'ext': 'mp4',
183 'title': 'Dive into Serenity: Blue Lagoon Pedi Nails for a Tranquil and Refreshing Spa Experience! 💙💅',
184 'description': 'md5:5da41c767d2317e42e49b663b0b2150f',
185 'uploader': 'Glamour Artistry |Everyday Outfits, Luxury Fashion & Nail Designs',
186 'uploader_id': '1142999717836434688',
187 'upload_date': '20240702',
188 'timestamp': 1719939156,
189 'duration': 7.967,
190 'comment_count': int,
191 'repost_count': int,
192 'categories': 'count:9',
193 'tags': ['#BlueLagoonPediNails', '#SpaExperience'],
194 'thumbnail': r're:^https?://.*\.(?:jpg|png)$',
198 def _real_extract(self, url):
199 video_id = self._match_id(url)
200 data = self._call_api(
201 'Pin', video_id, {
202 'field_set_key': 'unauth_react_main_pin',
203 'id': video_id,
204 })['data']
205 return self._extract_video(data)
208 class PinterestCollectionIE(PinterestBaseIE):
209 _VALID_URL = rf'{PinterestBaseIE._VALID_URL_BASE}/(?P<username>[^/]+)/(?P<id>[^/?#&]+)'
210 _TESTS = [{
211 'url': 'https://www.pinterest.ca/mashal0407/cool-diys/',
212 'info_dict': {
213 'id': '585890301462791043',
214 'title': 'cool diys',
216 'playlist_count': 8,
217 }, {
218 'url': 'https://www.pinterest.ca/fudohub/videos/',
219 'info_dict': {
220 'id': '682858430939307450',
221 'title': 'VIDEOS',
223 'playlist_mincount': 365,
224 'skip': 'Test with extract_formats=False',
227 @classmethod
228 def suitable(cls, url):
229 return False if PinterestIE.suitable(url) else super().suitable(url)
231 def _real_extract(self, url):
232 username, slug = self._match_valid_url(url).groups()
233 board = self._call_api(
234 'Board', slug, {
235 'slug': slug,
236 'username': username,
237 })['data']
238 board_id = board['id']
239 options = {
240 'board_id': board_id,
241 'page_size': 250,
243 bookmark = None
244 entries = []
245 while True:
246 if bookmark:
247 options['bookmarks'] = [bookmark]
248 board_feed = self._call_api('BoardFeed', board_id, options)
249 for item in (board_feed.get('data') or []):
250 if not isinstance(item, dict) or item.get('type') != 'pin':
251 continue
252 video_id = item.get('id')
253 if video_id:
254 # Some pins may not be available anonymously via pin URL
255 # video = self._extract_video(item, extract_formats=False)
256 # video.update({
257 # '_type': 'url_transparent',
258 # 'url': 'https://www.pinterest.com/pin/%s/' % video_id,
259 # })
260 # entries.append(video)
261 entries.append(self._extract_video(item))
262 bookmark = board_feed.get('bookmark')
263 if not bookmark:
264 break
265 return self.playlist_result(
266 entries, playlist_id=board_id, playlist_title=board.get('name'))