]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/pinterest.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / pinterest.py
CommitLineData
70c5802b 1import json
70c5802b 2
3from .common import InfoExtractor
4from ..compat import compat_str
5from ..utils import (
6 determine_ext,
7 float_or_none,
8 int_or_none,
9 try_get,
10 unified_timestamp,
11 url_or_none,
12)
13
14
15class PinterestBaseIE(InfoExtractor):
16 _VALID_URL_BASE = r'https?://(?:[^/]+\.)?pinterest\.(?:com|fr|de|ch|jp|cl|ca|it|co\.uk|nz|ru|com\.au|at|pt|co\.kr|es|com\.mx|dk|ph|th|com\.uy|co|nl|info|kr|ie|vn|com\.vn|ec|mx|in|pe|co\.at|hu|co\.in|co\.nz|id|com\.ec|com\.py|tw|be|uk|com\.bo|com\.pe)'
17
18 def _call_api(self, resource, video_id, options):
19 return self._download_json(
20 'https://www.pinterest.com/resource/%sResource/get/' % resource,
21 video_id, 'Download %s JSON metadata' % resource, query={
22 'data': json.dumps({'options': options})
23 })['resource_response']
24
25 def _extract_video(self, data, extract_formats=True):
26 video_id = data['id']
27
28 title = (data.get('title') or data.get('grid_title') or video_id).strip()
29
10db0d2f 30 urls = []
70c5802b 31 formats = []
32 duration = None
33 if extract_formats:
34 for format_id, format_dict in data['videos']['video_list'].items():
35 if not isinstance(format_dict, dict):
36 continue
37 format_url = url_or_none(format_dict.get('url'))
10db0d2f 38 if not format_url or format_url in urls:
70c5802b 39 continue
10db0d2f 40 urls.append(format_url)
70c5802b 41 duration = float_or_none(format_dict.get('duration'), scale=1000)
42 ext = determine_ext(format_url)
43 if 'hls' in format_id.lower() or ext == 'm3u8':
44 formats.extend(self._extract_m3u8_formats(
45 format_url, video_id, 'mp4', entry_protocol='m3u8_native',
46 m3u8_id=format_id, fatal=False))
47 else:
48 formats.append({
49 'url': format_url,
50 'format_id': format_id,
51 'width': int_or_none(format_dict.get('width')),
52 'height': int_or_none(format_dict.get('height')),
53 'duration': duration,
54 })
70c5802b 55
56 description = data.get('description') or data.get('description_html') or data.get('seo_description')
57 timestamp = unified_timestamp(data.get('created_at'))
58
59 def _u(field):
60 return try_get(data, lambda x: x['closeup_attribution'][field], compat_str)
61
62 uploader = _u('full_name')
63 uploader_id = _u('id')
64
65 repost_count = int_or_none(data.get('repin_count'))
66 comment_count = int_or_none(data.get('comment_count'))
67 categories = try_get(data, lambda x: x['pin_join']['visual_annotation'], list)
68 tags = data.get('hashtags')
69
70 thumbnails = []
71 images = data.get('images')
72 if isinstance(images, dict):
73 for thumbnail_id, thumbnail in images.items():
74 if not isinstance(thumbnail, dict):
75 continue
76 thumbnail_url = url_or_none(thumbnail.get('url'))
77 if not thumbnail_url:
78 continue
79 thumbnails.append({
80 'url': thumbnail_url,
81 'width': int_or_none(thumbnail.get('width')),
82 'height': int_or_none(thumbnail.get('height')),
83 })
84
85 return {
86 'id': video_id,
87 'title': title,
88 'description': description,
89 'duration': duration,
90 'timestamp': timestamp,
91 'thumbnails': thumbnails,
92 'uploader': uploader,
93 'uploader_id': uploader_id,
94 'repost_count': repost_count,
95 'comment_count': comment_count,
96 'categories': categories,
97 'tags': tags,
98 'formats': formats,
99 'extractor_key': PinterestIE.ie_key(),
100 }
101
102
103class PinterestIE(PinterestBaseIE):
104 _VALID_URL = r'%s/pin/(?P<id>\d+)' % PinterestBaseIE._VALID_URL_BASE
105 _TESTS = [{
106 'url': 'https://www.pinterest.com/pin/664281013778109217/',
107 'md5': '6550c2af85d6d9f3fe3b88954d1577fc',
108 'info_dict': {
109 'id': '664281013778109217',
110 'ext': 'mp4',
111 'title': 'Origami',
112 'description': 'md5:b9d90ddf7848e897882de9e73344f7dd',
113 'duration': 57.7,
114 'timestamp': 1593073622,
115 'upload_date': '20200625',
116 'uploader': 'Love origami -I am Dafei',
117 'uploader_id': '586523688879454212',
118 'repost_count': 50,
119 'comment_count': 0,
120 'categories': list,
121 'tags': list,
122 },
123 }, {
124 'url': 'https://co.pinterest.com/pin/824721750502199491/',
125 'only_matching': True,
126 }]
127
128 def _real_extract(self, url):
129 video_id = self._match_id(url)
130 data = self._call_api(
131 'Pin', video_id, {
132 'field_set_key': 'unauth_react_main_pin',
133 'id': video_id,
134 })['data']
135 return self._extract_video(data)
136
137
138class PinterestCollectionIE(PinterestBaseIE):
139 _VALID_URL = r'%s/(?P<username>[^/]+)/(?P<id>[^/?#&]+)' % PinterestBaseIE._VALID_URL_BASE
140 _TESTS = [{
141 'url': 'https://www.pinterest.ca/mashal0407/cool-diys/',
142 'info_dict': {
143 'id': '585890301462791043',
144 'title': 'cool diys',
145 },
146 'playlist_count': 8,
147 }, {
148 'url': 'https://www.pinterest.ca/fudohub/videos/',
149 'info_dict': {
150 'id': '682858430939307450',
151 'title': 'VIDEOS',
152 },
153 'playlist_mincount': 365,
154 'skip': 'Test with extract_formats=False',
155 }]
156
157 @classmethod
158 def suitable(cls, url):
159 return False if PinterestIE.suitable(url) else super(
160 PinterestCollectionIE, cls).suitable(url)
161
162 def _real_extract(self, url):
5ad28e7f 163 username, slug = self._match_valid_url(url).groups()
70c5802b 164 board = self._call_api(
165 'Board', slug, {
166 'slug': slug,
167 'username': username
168 })['data']
169 board_id = board['id']
170 options = {
171 'board_id': board_id,
172 'page_size': 250,
173 }
174 bookmark = None
175 entries = []
176 while True:
177 if bookmark:
178 options['bookmarks'] = [bookmark]
179 board_feed = self._call_api('BoardFeed', board_id, options)
180 for item in (board_feed.get('data') or []):
181 if not isinstance(item, dict) or item.get('type') != 'pin':
182 continue
183 video_id = item.get('id')
184 if video_id:
185 # Some pins may not be available anonymously via pin URL
186 # video = self._extract_video(item, extract_formats=False)
187 # video.update({
188 # '_type': 'url_transparent',
189 # 'url': 'https://www.pinterest.com/pin/%s/' % video_id,
190 # })
191 # entries.append(video)
192 entries.append(self._extract_video(item))
193 bookmark = board_feed.get('bookmark')
194 if not bookmark:
195 break
196 return self.playlist_result(
197 entries, playlist_id=board_id, playlist_title=board.get('name'))