]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/pinterest.py
[extractor/FranceCulture] Fix extractor (#3874)
[yt-dlp.git] / yt_dlp / extractor / pinterest.py
1 import json
2
3 from .common import InfoExtractor
4 from ..compat import compat_str
5 from ..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
15 class 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
30 urls = []
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'))
38 if not format_url or format_url in urls:
39 continue
40 urls.append(format_url)
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 })
55 self._sort_formats(formats)
56
57 description = data.get('description') or data.get('description_html') or data.get('seo_description')
58 timestamp = unified_timestamp(data.get('created_at'))
59
60 def _u(field):
61 return try_get(data, lambda x: x['closeup_attribution'][field], compat_str)
62
63 uploader = _u('full_name')
64 uploader_id = _u('id')
65
66 repost_count = int_or_none(data.get('repin_count'))
67 comment_count = int_or_none(data.get('comment_count'))
68 categories = try_get(data, lambda x: x['pin_join']['visual_annotation'], list)
69 tags = data.get('hashtags')
70
71 thumbnails = []
72 images = data.get('images')
73 if isinstance(images, dict):
74 for thumbnail_id, thumbnail in images.items():
75 if not isinstance(thumbnail, dict):
76 continue
77 thumbnail_url = url_or_none(thumbnail.get('url'))
78 if not thumbnail_url:
79 continue
80 thumbnails.append({
81 'url': thumbnail_url,
82 'width': int_or_none(thumbnail.get('width')),
83 'height': int_or_none(thumbnail.get('height')),
84 })
85
86 return {
87 'id': video_id,
88 'title': title,
89 'description': description,
90 'duration': duration,
91 'timestamp': timestamp,
92 'thumbnails': thumbnails,
93 'uploader': uploader,
94 'uploader_id': uploader_id,
95 'repost_count': repost_count,
96 'comment_count': comment_count,
97 'categories': categories,
98 'tags': tags,
99 'formats': formats,
100 'extractor_key': PinterestIE.ie_key(),
101 }
102
103
104 class PinterestIE(PinterestBaseIE):
105 _VALID_URL = r'%s/pin/(?P<id>\d+)' % PinterestBaseIE._VALID_URL_BASE
106 _TESTS = [{
107 'url': 'https://www.pinterest.com/pin/664281013778109217/',
108 'md5': '6550c2af85d6d9f3fe3b88954d1577fc',
109 'info_dict': {
110 'id': '664281013778109217',
111 'ext': 'mp4',
112 'title': 'Origami',
113 'description': 'md5:b9d90ddf7848e897882de9e73344f7dd',
114 'duration': 57.7,
115 'timestamp': 1593073622,
116 'upload_date': '20200625',
117 'uploader': 'Love origami -I am Dafei',
118 'uploader_id': '586523688879454212',
119 'repost_count': 50,
120 'comment_count': 0,
121 'categories': list,
122 'tags': list,
123 },
124 }, {
125 'url': 'https://co.pinterest.com/pin/824721750502199491/',
126 'only_matching': True,
127 }]
128
129 def _real_extract(self, url):
130 video_id = self._match_id(url)
131 data = self._call_api(
132 'Pin', video_id, {
133 'field_set_key': 'unauth_react_main_pin',
134 'id': video_id,
135 })['data']
136 return self._extract_video(data)
137
138
139 class PinterestCollectionIE(PinterestBaseIE):
140 _VALID_URL = r'%s/(?P<username>[^/]+)/(?P<id>[^/?#&]+)' % PinterestBaseIE._VALID_URL_BASE
141 _TESTS = [{
142 'url': 'https://www.pinterest.ca/mashal0407/cool-diys/',
143 'info_dict': {
144 'id': '585890301462791043',
145 'title': 'cool diys',
146 },
147 'playlist_count': 8,
148 }, {
149 'url': 'https://www.pinterest.ca/fudohub/videos/',
150 'info_dict': {
151 'id': '682858430939307450',
152 'title': 'VIDEOS',
153 },
154 'playlist_mincount': 365,
155 'skip': 'Test with extract_formats=False',
156 }]
157
158 @classmethod
159 def suitable(cls, url):
160 return False if PinterestIE.suitable(url) else super(
161 PinterestCollectionIE, cls).suitable(url)
162
163 def _real_extract(self, url):
164 username, slug = self._match_valid_url(url).groups()
165 board = self._call_api(
166 'Board', slug, {
167 'slug': slug,
168 'username': username
169 })['data']
170 board_id = board['id']
171 options = {
172 'board_id': board_id,
173 'page_size': 250,
174 }
175 bookmark = None
176 entries = []
177 while True:
178 if bookmark:
179 options['bookmarks'] = [bookmark]
180 board_feed = self._call_api('BoardFeed', board_id, options)
181 for item in (board_feed.get('data') or []):
182 if not isinstance(item, dict) or item.get('type') != 'pin':
183 continue
184 video_id = item.get('id')
185 if video_id:
186 # Some pins may not be available anonymously via pin URL
187 # video = self._extract_video(item, extract_formats=False)
188 # video.update({
189 # '_type': 'url_transparent',
190 # 'url': 'https://www.pinterest.com/pin/%s/' % video_id,
191 # })
192 # entries.append(video)
193 entries.append(self._extract_video(item))
194 bookmark = board_feed.get('bookmark')
195 if not bookmark:
196 break
197 return self.playlist_result(
198 entries, playlist_id=board_id, playlist_title=board.get('name'))