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