]>
jfr.im git - yt-dlp.git/blob - youtube_dlc/extractor/pinterest.py
2 from __future__
import unicode_literals
7 from .common
import InfoExtractor
8 from ..compat
import compat_str
19 class 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)'
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']
29 def _extract_video(self
, data
, extract_formats
=True):
32 title
= (data
.get('title') or data
.get('grid_title') or video_id
).strip()
37 for format_id
, format_dict
in data
['videos']['video_list'].items():
38 if not isinstance(format_dict
, dict):
40 format_url
= url_or_none(format_dict
.get('url'))
43 duration
= float_or_none(format_dict
.get('duration'), scale
=1000)
44 ext
= determine_ext(format_url
)
45 if 'hls' in format_id
.lower() or ext
== 'm3u8':
46 formats
.extend(self
._extract
_m
3u8_formats
(
47 format_url
, video_id
, 'mp4', entry_protocol
='m3u8_native',
48 m3u8_id
=format_id
, fatal
=False))
52 'format_id': format_id
,
53 'width': int_or_none(format_dict
.get('width')),
54 'height': int_or_none(format_dict
.get('height')),
57 self
._sort
_formats
(formats
)
59 description
= data
.get('description') or data
.get('description_html') or data
.get('seo_description')
60 timestamp
= unified_timestamp(data
.get('created_at'))
63 return try_get(data
, lambda x
: x
['closeup_attribution'][field
], compat_str
)
65 uploader
= _u('full_name')
66 uploader_id
= _u('id')
68 repost_count
= int_or_none(data
.get('repin_count'))
69 comment_count
= int_or_none(data
.get('comment_count'))
70 categories
= try_get(data
, lambda x
: x
['pin_join']['visual_annotation'], list)
71 tags
= data
.get('hashtags')
74 images
= data
.get('images')
75 if isinstance(images
, dict):
76 for thumbnail_id
, thumbnail
in images
.items():
77 if not isinstance(thumbnail
, dict):
79 thumbnail_url
= url_or_none(thumbnail
.get('url'))
84 'width': int_or_none(thumbnail
.get('width')),
85 'height': int_or_none(thumbnail
.get('height')),
91 'description': description
,
93 'timestamp': timestamp
,
94 'thumbnails': thumbnails
,
96 'uploader_id': uploader_id
,
97 'repost_count': repost_count
,
98 'comment_count': comment_count
,
99 'categories': categories
,
102 'extractor_key': PinterestIE
.ie_key(),
106 class PinterestIE(PinterestBaseIE
):
107 _VALID_URL
= r
'%s/pin/(?P<id>\d+)' % PinterestBaseIE
._VALID
_URL
_BASE
109 'url': 'https://www.pinterest.com/pin/664281013778109217/',
110 'md5': '6550c2af85d6d9f3fe3b88954d1577fc',
112 'id': '664281013778109217',
115 'description': 'md5:b9d90ddf7848e897882de9e73344f7dd',
117 'timestamp': 1593073622,
118 'upload_date': '20200625',
119 'uploader': 'Love origami -I am Dafei',
120 'uploader_id': '586523688879454212',
127 'url': 'https://co.pinterest.com/pin/824721750502199491/',
128 'only_matching': True,
131 def _real_extract(self
, url
):
132 video_id
= self
._match
_id
(url
)
133 data
= self
._call
_api
(
135 'field_set_key': 'unauth_react_main_pin',
138 return self
._extract
_video
(data
)
141 class PinterestCollectionIE(PinterestBaseIE
):
142 _VALID_URL
= r
'%s/(?P<username>[^/]+)/(?P<id>[^/?#&]+)' % PinterestBaseIE
._VALID
_URL
_BASE
144 'url': 'https://www.pinterest.ca/mashal0407/cool-diys/',
146 'id': '585890301462791043',
147 'title': 'cool diys',
151 'url': 'https://www.pinterest.ca/fudohub/videos/',
153 'id': '682858430939307450',
156 'playlist_mincount': 365,
157 'skip': 'Test with extract_formats=False',
161 def suitable(cls
, url
):
162 return False if PinterestIE
.suitable(url
) else super(
163 PinterestCollectionIE
, cls
).suitable(url
)
165 def _real_extract(self
, url
):
166 username
, slug
= re
.match(self
._VALID
_URL
, url
).groups()
167 board
= self
._call
_api
(
172 board_id
= board
['id']
174 'board_id': board_id
,
181 options
['bookmarks'] = [bookmark
]
182 board_feed
= self
._call
_api
('BoardFeed', board_id
, options
)
183 for item
in (board_feed
.get('data') or []):
184 if not isinstance(item
, dict) or item
.get('type') != 'pin':
186 video_id
= item
.get('id')
188 # Some pins may not be available anonymously via pin URL
189 # video = self._extract_video(item, extract_formats=False)
191 # '_type': 'url_transparent',
192 # 'url': 'https://www.pinterest.com/pin/%s/' % video_id,
194 # entries.append(video)
195 entries
.append(self
._extract
_video
(item
))
196 bookmark
= board_feed
.get('bookmark')
199 return self
.playlist_result(
200 entries
, playlist_id
=board_id
, playlist_title
=board
.get('name'))