]>
jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/pinterest.py
3 from .common
import InfoExtractor
4 from ..compat
import compat_str
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)'
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']
25 def _extract_video(self
, data
, extract_formats
=True):
28 title
= (data
.get('title') or data
.get('grid_title') or video_id
).strip()
34 for format_id
, format_dict
in data
['videos']['video_list'].items():
35 if not isinstance(format_dict
, dict):
37 format_url
= url_or_none(format_dict
.get('url'))
38 if not format_url
or format_url
in urls
:
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
_m
3u8_formats
(
45 format_url
, video_id
, 'mp4', entry_protocol
='m3u8_native',
46 m3u8_id
=format_id
, fatal
=False))
50 'format_id': format_id
,
51 'width': int_or_none(format_dict
.get('width')),
52 'height': int_or_none(format_dict
.get('height')),
56 description
= data
.get('description') or data
.get('description_html') or data
.get('seo_description')
57 timestamp
= unified_timestamp(data
.get('created_at'))
60 return try_get(data
, lambda x
: x
['closeup_attribution'][field
], compat_str
)
62 uploader
= _u('full_name')
63 uploader_id
= _u('id')
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')
71 images
= data
.get('images')
72 if isinstance(images
, dict):
73 for thumbnail_id
, thumbnail
in images
.items():
74 if not isinstance(thumbnail
, dict):
76 thumbnail_url
= url_or_none(thumbnail
.get('url'))
81 'width': int_or_none(thumbnail
.get('width')),
82 'height': int_or_none(thumbnail
.get('height')),
88 'description': description
,
90 'timestamp': timestamp
,
91 'thumbnails': thumbnails
,
93 'uploader_id': uploader_id
,
94 'repost_count': repost_count
,
95 'comment_count': comment_count
,
96 'categories': categories
,
99 'extractor_key': PinterestIE
.ie_key(),
103 class PinterestIE(PinterestBaseIE
):
104 _VALID_URL
= r
'%s/pin/(?P<id>\d+)' % PinterestBaseIE
._VALID
_URL
_BASE
106 'url': 'https://www.pinterest.com/pin/664281013778109217/',
107 'md5': '6550c2af85d6d9f3fe3b88954d1577fc',
109 'id': '664281013778109217',
112 'description': 'md5:b9d90ddf7848e897882de9e73344f7dd',
114 'timestamp': 1593073622,
115 'upload_date': '20200625',
116 'uploader': 'Love origami -I am Dafei',
117 'uploader_id': '586523688879454212',
124 'url': 'https://co.pinterest.com/pin/824721750502199491/',
125 'only_matching': True,
128 def _real_extract(self
, url
):
129 video_id
= self
._match
_id
(url
)
130 data
= self
._call
_api
(
132 'field_set_key': 'unauth_react_main_pin',
135 return self
._extract
_video
(data
)
138 class PinterestCollectionIE(PinterestBaseIE
):
139 _VALID_URL
= r
'%s/(?P<username>[^/]+)/(?P<id>[^/?#&]+)' % PinterestBaseIE
._VALID
_URL
_BASE
141 'url': 'https://www.pinterest.ca/mashal0407/cool-diys/',
143 'id': '585890301462791043',
144 'title': 'cool diys',
148 'url': 'https://www.pinterest.ca/fudohub/videos/',
150 'id': '682858430939307450',
153 'playlist_mincount': 365,
154 'skip': 'Test with extract_formats=False',
158 def suitable(cls
, url
):
159 return False if PinterestIE
.suitable(url
) else super(
160 PinterestCollectionIE
, cls
).suitable(url
)
162 def _real_extract(self
, url
):
163 username
, slug
= self
._match
_valid
_url
(url
).groups()
164 board
= self
._call
_api
(
169 board_id
= board
['id']
171 'board_id': board_id
,
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':
183 video_id
= item
.get('id')
185 # Some pins may not be available anonymously via pin URL
186 # video = self._extract_video(item, extract_formats=False)
188 # '_type': 'url_transparent',
189 # 'url': 'https://www.pinterest.com/pin/%s/' % video_id,
191 # entries.append(video)
192 entries
.append(self
._extract
_video
(item
))
193 bookmark
= board_feed
.get('bookmark')
196 return self
.playlist_result(
197 entries
, playlist_id
=board_id
, playlist_title
=board
.get('name'))