]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/vice.py
[extractors] Use new framework for existing embeds (#4307)
[yt-dlp.git] / yt_dlp / extractor / vice.py
1 import functools
2 import hashlib
3 import json
4 import random
5 import time
6
7 from .adobepass import AdobePassIE
8 from .common import InfoExtractor
9 from .youtube import YoutubeIE
10 from ..compat import (
11 compat_HTTPError,
12 compat_str,
13 )
14 from ..utils import (
15 clean_html,
16 ExtractorError,
17 int_or_none,
18 OnDemandPagedList,
19 parse_age_limit,
20 str_or_none,
21 try_get,
22 )
23
24
25 class ViceBaseIE(InfoExtractor):
26 def _call_api(self, resource, resource_key, resource_id, locale, fields, args=''):
27 return self._download_json(
28 'https://video.vice.com/api/v1/graphql', resource_id, query={
29 'query': '''{
30 %s(locale: "%s", %s: "%s"%s) {
31 %s
32 }
33 }''' % (resource, locale, resource_key, resource_id, args, fields),
34 })['data'][resource]
35
36
37 class ViceIE(ViceBaseIE, AdobePassIE):
38 IE_NAME = 'vice'
39 _VALID_URL = r'https?://(?:(?:video|vms)\.vice|(?:www\.)?vice(?:land|tv))\.com/(?P<locale>[^/]+)/(?:video/[^/]+|embed)/(?P<id>[\da-f]{24})'
40 _EMBED_REGEX = [r'<iframe\b[^>]+\bsrc=["\'](?P<url>(?:https?:)?//video\.vice\.com/[^/]+/embed/[\da-f]{24})']
41 _TESTS = [{
42 'url': 'https://video.vice.com/en_us/video/pet-cremator/58c69e38a55424f1227dc3f7',
43 'info_dict': {
44 'id': '58c69e38a55424f1227dc3f7',
45 'ext': 'mp4',
46 'title': '10 Questions You Always Wanted To Ask: Pet Cremator',
47 'description': 'md5:fe856caacf61fe0e74fab15ce2b07ca5',
48 'uploader': 'vice',
49 'uploader_id': '57a204088cb727dec794c67b',
50 'timestamp': 1489664942,
51 'upload_date': '20170316',
52 'age_limit': 14,
53 },
54 'params': {
55 # m3u8 download
56 'skip_download': True,
57 },
58 }, {
59 # geo restricted to US
60 'url': 'https://video.vice.com/en_us/video/the-signal-from-tolva/5816510690b70e6c5fd39a56',
61 'info_dict': {
62 'id': '5816510690b70e6c5fd39a56',
63 'ext': 'mp4',
64 'uploader': 'vice',
65 'title': 'The Signal From Tölva',
66 'description': 'md5:3927e3c79f9e8094606a2b3c5b5e55d5',
67 'uploader_id': '57a204088cb727dec794c67b',
68 'timestamp': 1477941983,
69 'upload_date': '20161031',
70 },
71 'params': {
72 # m3u8 download
73 'skip_download': True,
74 },
75 }, {
76 'url': 'https://video.vice.com/alps/video/ulfs-wien-beruchtigste-grafitti-crew-part-1/581b12b60a0e1f4c0fb6ea2f',
77 'info_dict': {
78 'id': '581b12b60a0e1f4c0fb6ea2f',
79 'ext': 'mp4',
80 'title': 'ULFs - Wien berüchtigste Grafitti Crew - Part 1',
81 'description': 'Zwischen Hinterzimmer-Tattoos und U-Bahnschächten erzählen uns die Ulfs, wie es ist, "süchtig nach Sachbeschädigung" zu sein.',
82 'uploader': 'vice',
83 'uploader_id': '57a204088cb727dec794c67b',
84 'timestamp': 1485368119,
85 'upload_date': '20170125',
86 'age_limit': 14,
87 },
88 'params': {
89 # AES-encrypted m3u8
90 'skip_download': True,
91 },
92 }, {
93 'url': 'https://video.vice.com/en_us/video/pizza-show-trailer/56d8c9a54d286ed92f7f30e4',
94 'only_matching': True,
95 }, {
96 'url': 'https://video.vice.com/en_us/embed/57f41d3556a0a80f54726060',
97 'only_matching': True,
98 }, {
99 'url': 'https://vms.vice.com/en_us/video/preplay/58c69e38a55424f1227dc3f7',
100 'only_matching': True,
101 }, {
102 'url': 'https://www.viceland.com/en_us/video/thursday-march-1-2018/5a8f2d7ff1cdb332dd446ec1',
103 'only_matching': True,
104 }]
105
106 def _real_extract(self, url):
107 locale, video_id = self._match_valid_url(url).groups()
108
109 video = self._call_api('videos', 'id', video_id, locale, '''body
110 locked
111 rating
112 thumbnail_url
113 title''')[0]
114 title = video['title'].strip()
115 rating = video.get('rating')
116
117 query = {}
118 if video.get('locked'):
119 resource = self._get_mvpd_resource(
120 'VICELAND', title, video_id, rating)
121 query['tvetoken'] = self._extract_mvpd_auth(
122 url, video_id, 'VICELAND', resource)
123
124 # signature generation algorithm is reverse engineered from signatureGenerator in
125 # webpack:///../shared/~/vice-player/dist/js/vice-player.js in
126 # https://www.viceland.com/assets/common/js/web.vendor.bundle.js
127 # new JS is located here https://vice-web-statics-cdn.vice.com/vice-player/player-embed.js
128 exp = int(time.time()) + 1440
129
130 query.update({
131 'exp': exp,
132 'sign': hashlib.sha512(('%s:GET:%d' % (video_id, exp)).encode()).hexdigest(),
133 'skipadstitching': 1,
134 'platform': 'desktop',
135 'rn': random.randint(10000, 100000),
136 })
137
138 try:
139 preplay = self._download_json(
140 'https://vms.vice.com/%s/video/preplay/%s' % (locale, video_id),
141 video_id, query=query)
142 except ExtractorError as e:
143 if isinstance(e.cause, compat_HTTPError) and e.cause.code in (400, 401):
144 error = json.loads(e.cause.read().decode())
145 error_message = error.get('error_description') or error['details']
146 raise ExtractorError('%s said: %s' % (
147 self.IE_NAME, error_message), expected=True)
148 raise
149
150 video_data = preplay['video']
151 formats = self._extract_m3u8_formats(
152 preplay['playURL'], video_id, 'mp4', 'm3u8_native')
153 self._sort_formats(formats)
154 episode = video_data.get('episode') or {}
155 channel = video_data.get('channel') or {}
156 season = video_data.get('season') or {}
157
158 subtitles = {}
159 for subtitle in preplay.get('subtitleURLs', []):
160 cc_url = subtitle.get('url')
161 if not cc_url:
162 continue
163 language_code = try_get(subtitle, lambda x: x['languages'][0]['language_code'], compat_str) or 'en'
164 subtitles.setdefault(language_code, []).append({
165 'url': cc_url,
166 })
167
168 return {
169 'formats': formats,
170 'id': video_id,
171 'title': title,
172 'description': clean_html(video.get('body')),
173 'thumbnail': video.get('thumbnail_url'),
174 'duration': int_or_none(video_data.get('video_duration')),
175 'timestamp': int_or_none(video_data.get('created_at'), 1000),
176 'age_limit': parse_age_limit(video_data.get('video_rating') or rating),
177 'series': try_get(video_data, lambda x: x['show']['base']['display_title'], compat_str),
178 'episode_number': int_or_none(episode.get('episode_number')),
179 'episode_id': str_or_none(episode.get('id') or video_data.get('episode_id')),
180 'season_number': int_or_none(season.get('season_number')),
181 'season_id': str_or_none(season.get('id') or video_data.get('season_id')),
182 'uploader': channel.get('name'),
183 'uploader_id': str_or_none(channel.get('id')),
184 'subtitles': subtitles,
185 }
186
187
188 class ViceShowIE(ViceBaseIE):
189 IE_NAME = 'vice:show'
190 _VALID_URL = r'https?://(?:video\.vice|(?:www\.)?vice(?:land|tv))\.com/(?P<locale>[^/]+)/show/(?P<id>[^/?#&]+)'
191 _PAGE_SIZE = 25
192 _TESTS = [{
193 'url': 'https://video.vice.com/en_us/show/fck-thats-delicious',
194 'info_dict': {
195 'id': '57a2040c8cb727dec794c901',
196 'title': 'F*ck, That’s Delicious',
197 'description': 'The life and eating habits of rap’s greatest bon vivant, Action Bronson.',
198 },
199 'playlist_mincount': 64,
200 }, {
201 'url': 'https://www.vicetv.com/en_us/show/fck-thats-delicious',
202 'only_matching': True,
203 }]
204
205 def _fetch_page(self, locale, show_id, page):
206 videos = self._call_api('videos', 'show_id', show_id, locale, '''body
207 id
208 url''', ', page: %d, per_page: %d' % (page + 1, self._PAGE_SIZE))
209 for video in videos:
210 yield self.url_result(
211 video['url'], ViceIE.ie_key(), video.get('id'))
212
213 def _real_extract(self, url):
214 locale, display_id = self._match_valid_url(url).groups()
215 show = self._call_api('shows', 'slug', display_id, locale, '''dek
216 id
217 title''')[0]
218 show_id = show['id']
219
220 entries = OnDemandPagedList(
221 functools.partial(self._fetch_page, locale, show_id),
222 self._PAGE_SIZE)
223
224 return self.playlist_result(
225 entries, show_id, show.get('title'), show.get('dek'))
226
227
228 class ViceArticleIE(ViceBaseIE):
229 IE_NAME = 'vice:article'
230 _VALID_URL = r'https://(?:www\.)?vice\.com/(?P<locale>[^/]+)/article/(?:[0-9a-z]{6}/)?(?P<id>[^?#]+)'
231
232 _TESTS = [{
233 'url': 'https://www.vice.com/en_us/article/on-set-with-the-woman-making-mormon-porn-in-utah',
234 'info_dict': {
235 'id': '58dc0a3dee202d2a0ccfcbd8',
236 'ext': 'mp4',
237 'title': 'Mormon War on Porn',
238 'description': 'md5:1c5d91fe25fa8aa304f9def118b92dbf',
239 'uploader': 'vice',
240 'uploader_id': '57a204088cb727dec794c67b',
241 'timestamp': 1491883129,
242 'upload_date': '20170411',
243 'age_limit': 17,
244 },
245 'params': {
246 # AES-encrypted m3u8
247 'skip_download': True,
248 },
249 'add_ie': [ViceIE.ie_key()],
250 }, {
251 'url': 'https://www.vice.com/en_us/article/how-to-hack-a-car',
252 'md5': '13010ee0bc694ea87ec40724397c2349',
253 'info_dict': {
254 'id': '3jstaBeXgAs',
255 'ext': 'mp4',
256 'title': 'How to Hack a Car: Phreaked Out (Episode 2)',
257 'description': 'md5:ee95453f7ff495db8efe14ae8bf56f30',
258 'uploader': 'Motherboard',
259 'uploader_id': 'MotherboardTV',
260 'upload_date': '20140529',
261 },
262 'add_ie': [YoutubeIE.ie_key()],
263 }, {
264 'url': 'https://www.vice.com/en_us/article/znm9dx/karley-sciortino-slutever-reloaded',
265 'md5': 'a7ecf64ee4fa19b916c16f4b56184ae2',
266 'info_dict': {
267 'id': '57f41d3556a0a80f54726060',
268 'ext': 'mp4',
269 'title': "Making The World's First Male Sex Doll",
270 'description': 'md5:19b00b215b99961cf869c40fbe9df755',
271 'uploader': 'vice',
272 'uploader_id': '57a204088cb727dec794c67b',
273 'timestamp': 1476919911,
274 'upload_date': '20161019',
275 'age_limit': 17,
276 },
277 'params': {
278 'skip_download': True,
279 },
280 'add_ie': [ViceIE.ie_key()],
281 }, {
282 'url': 'https://www.vice.com/en_us/article/cowboy-capitalists-part-1',
283 'only_matching': True,
284 }, {
285 'url': 'https://www.vice.com/ru/article/big-night-out-ibiza-clive-martin-229',
286 'only_matching': True,
287 }]
288
289 def _real_extract(self, url):
290 locale, display_id = self._match_valid_url(url).groups()
291
292 article = self._call_api('articles', 'slug', display_id, locale, '''body
293 embed_code''')[0]
294 body = article['body']
295
296 def _url_res(video_url, ie_key):
297 return {
298 '_type': 'url_transparent',
299 'url': video_url,
300 'display_id': display_id,
301 'ie_key': ie_key,
302 }
303
304 vice_url = ViceIE._extract_url(body)
305 if vice_url:
306 return _url_res(vice_url, ViceIE.ie_key())
307
308 embed_code = self._search_regex(
309 r'embedCode=([^&\'"]+)', body,
310 'ooyala embed code', default=None)
311 if embed_code:
312 return _url_res('ooyala:%s' % embed_code, 'Ooyala')
313
314 youtube_url = YoutubeIE._extract_url(body)
315 if youtube_url:
316 return _url_res(youtube_url, YoutubeIE.ie_key())
317
318 video_url = self._html_search_regex(
319 r'data-video-url="([^"]+)"',
320 article['embed_code'], 'video URL')
321
322 return _url_res(video_url, ViceIE.ie_key())