]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/tvplay.py
[test/download] Fallback test to `bv`
[yt-dlp.git] / yt_dlp / extractor / tvplay.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8 compat_HTTPError,
9 compat_urlparse,
10 )
11 from ..utils import (
12 determine_ext,
13 ExtractorError,
14 int_or_none,
15 parse_duration,
16 parse_iso8601,
17 qualities,
18 try_get,
19 update_url_query,
20 url_or_none,
21 urljoin,
22 )
23
24
25 class TVPlayIE(InfoExtractor):
26 IE_NAME = 'mtg'
27 IE_DESC = 'MTG services'
28 _VALID_URL = r'''(?x)
29 (?:
30 mtg:|
31 https?://
32 (?:www\.)?
33 (?:
34 tvplay(?:\.skaties)?\.lv(?:/parraides)?|
35 (?:tv3play|play\.tv3)\.lt(?:/programos)?|
36 tv3play(?:\.tv3)?\.ee/sisu|
37 (?:tv(?:3|6|8|10)play)\.se/program|
38 (?:(?:tv3play|viasat4play|tv6play)\.no|(?:tv3play)\.dk)/programmer|
39 play\.nova(?:tv)?\.bg/programi
40 )
41 /(?:[^/]+/)+
42 )
43 (?P<id>\d+)
44 '''
45 _TESTS = [
46 {
47 'url': 'http://www.tvplay.lv/parraides/vinas-melo-labak/418113?autostart=true',
48 'md5': 'a1612fe0849455423ad8718fe049be21',
49 'info_dict': {
50 'id': '418113',
51 'ext': 'mp4',
52 'title': 'Kādi ir īri? - Viņas melo labāk',
53 'description': 'Baiba apsmej īrus, kādi tie ir un ko viņi dara.',
54 'series': 'Viņas melo labāk',
55 'season': '2.sezona',
56 'season_number': 2,
57 'duration': 25,
58 'timestamp': 1406097056,
59 'upload_date': '20140723',
60 },
61 },
62 {
63 'url': 'http://play.tv3.lt/programos/moterys-meluoja-geriau/409229?autostart=true',
64 'info_dict': {
65 'id': '409229',
66 'ext': 'flv',
67 'title': 'Moterys meluoja geriau',
68 'description': 'md5:9aec0fc68e2cbc992d2a140bd41fa89e',
69 'series': 'Moterys meluoja geriau',
70 'episode_number': 47,
71 'season': '1 sezonas',
72 'season_number': 1,
73 'duration': 1330,
74 'timestamp': 1403769181,
75 'upload_date': '20140626',
76 },
77 'params': {
78 # rtmp download
79 'skip_download': True,
80 },
81 },
82 {
83 'url': 'http://www.tv3play.ee/sisu/kodu-keset-linna/238551?autostart=true',
84 'info_dict': {
85 'id': '238551',
86 'ext': 'flv',
87 'title': 'Kodu keset linna 398537',
88 'description': 'md5:7df175e3c94db9e47c0d81ffa5d68701',
89 'duration': 1257,
90 'timestamp': 1292449761,
91 'upload_date': '20101215',
92 },
93 'params': {
94 # rtmp download
95 'skip_download': True,
96 },
97 },
98 {
99 'url': 'http://www.tv3play.se/program/husraddarna/395385?autostart=true',
100 'info_dict': {
101 'id': '395385',
102 'ext': 'mp4',
103 'title': 'Husräddarna S02E07',
104 'description': 'md5:f210c6c89f42d4fc39faa551be813777',
105 'duration': 2574,
106 'timestamp': 1400596321,
107 'upload_date': '20140520',
108 },
109 'params': {
110 'skip_download': True,
111 },
112 },
113 {
114 'url': 'http://www.tv6play.se/program/den-sista-dokusapan/266636?autostart=true',
115 'info_dict': {
116 'id': '266636',
117 'ext': 'mp4',
118 'title': 'Den sista dokusåpan S01E08',
119 'description': 'md5:295be39c872520221b933830f660b110',
120 'duration': 1492,
121 'timestamp': 1330522854,
122 'upload_date': '20120229',
123 'age_limit': 18,
124 },
125 'params': {
126 'skip_download': True,
127 },
128 },
129 {
130 'url': 'http://www.tv8play.se/program/antikjakten/282756?autostart=true',
131 'info_dict': {
132 'id': '282756',
133 'ext': 'mp4',
134 'title': 'Antikjakten S01E10',
135 'description': 'md5:1b201169beabd97e20c5ad0ad67b13b8',
136 'duration': 2646,
137 'timestamp': 1348575868,
138 'upload_date': '20120925',
139 },
140 'params': {
141 'skip_download': True,
142 },
143 },
144 {
145 'url': 'http://www.tv3play.no/programmer/anna-anka-soker-assistent/230898?autostart=true',
146 'info_dict': {
147 'id': '230898',
148 'ext': 'mp4',
149 'title': 'Anna Anka søker assistent - Ep. 8',
150 'description': 'md5:f80916bf5bbe1c5f760d127f8dd71474',
151 'duration': 2656,
152 'timestamp': 1277720005,
153 'upload_date': '20100628',
154 },
155 'params': {
156 'skip_download': True,
157 },
158 },
159 {
160 'url': 'http://www.viasat4play.no/programmer/budbringerne/21873?autostart=true',
161 'info_dict': {
162 'id': '21873',
163 'ext': 'mp4',
164 'title': 'Budbringerne program 10',
165 'description': 'md5:4db78dc4ec8a85bb04fd322a3ee5092d',
166 'duration': 1297,
167 'timestamp': 1254205102,
168 'upload_date': '20090929',
169 },
170 'params': {
171 'skip_download': True,
172 },
173 },
174 {
175 'url': 'http://www.tv6play.no/programmer/hotelinspektor-alex-polizzi/361883?autostart=true',
176 'info_dict': {
177 'id': '361883',
178 'ext': 'mp4',
179 'title': 'Hotelinspektør Alex Polizzi - Ep. 10',
180 'description': 'md5:3ecf808db9ec96c862c8ecb3a7fdaf81',
181 'duration': 2594,
182 'timestamp': 1393236292,
183 'upload_date': '20140224',
184 },
185 'params': {
186 'skip_download': True,
187 },
188 },
189 {
190 'url': 'http://play.novatv.bg/programi/zdravei-bulgariya/624952?autostart=true',
191 'info_dict': {
192 'id': '624952',
193 'ext': 'flv',
194 'title': 'Здравей, България (12.06.2015 г.) ',
195 'description': 'md5:99f3700451ac5bb71a260268b8daefd7',
196 'duration': 8838,
197 'timestamp': 1434100372,
198 'upload_date': '20150612',
199 },
200 'params': {
201 # rtmp download
202 'skip_download': True,
203 },
204 },
205 {
206 'url': 'https://play.nova.bg/programi/zdravei-bulgariya/764300?autostart=true',
207 'only_matching': True,
208 },
209 {
210 'url': 'http://tvplay.skaties.lv/parraides/vinas-melo-labak/418113?autostart=true',
211 'only_matching': True,
212 },
213 {
214 'url': 'https://tvplay.skaties.lv/vinas-melo-labak/418113/?autostart=true',
215 'only_matching': True,
216 },
217 {
218 # views is null
219 'url': 'http://tvplay.skaties.lv/parraides/tv3-zinas/760183',
220 'only_matching': True,
221 },
222 {
223 'url': 'http://tv3play.tv3.ee/sisu/kodu-keset-linna/238551?autostart=true',
224 'only_matching': True,
225 },
226 {
227 'url': 'mtg:418113',
228 'only_matching': True,
229 }
230 ]
231
232 def _real_extract(self, url):
233 video_id = self._match_id(url)
234 geo_country = self._search_regex(
235 r'https?://[^/]+\.([a-z]{2})', url,
236 'geo country', default=None)
237 if geo_country:
238 self._initialize_geo_bypass({'countries': [geo_country.upper()]})
239 video = self._download_json(
240 'http://playapi.mtgx.tv/v3/videos/%s' % video_id, video_id, 'Downloading video JSON')
241
242 title = video['title']
243
244 try:
245 streams = self._download_json(
246 'http://playapi.mtgx.tv/v3/videos/stream/%s' % video_id,
247 video_id, 'Downloading streams JSON')
248 except ExtractorError as e:
249 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
250 msg = self._parse_json(e.cause.read().decode('utf-8'), video_id)
251 raise ExtractorError(msg['msg'], expected=True)
252 raise
253
254 quality = qualities(['hls', 'medium', 'high'])
255 formats = []
256 for format_id, video_url in streams.get('streams', {}).items():
257 video_url = url_or_none(video_url)
258 if not video_url:
259 continue
260 ext = determine_ext(video_url)
261 if ext == 'f4m':
262 formats.extend(self._extract_f4m_formats(
263 update_url_query(video_url, {
264 'hdcore': '3.5.0',
265 'plugin': 'aasp-3.5.0.151.81'
266 }), video_id, f4m_id='hds', fatal=False))
267 elif ext == 'm3u8':
268 formats.extend(self._extract_m3u8_formats(
269 video_url, video_id, 'mp4', 'm3u8_native',
270 m3u8_id='hls', fatal=False))
271 else:
272 fmt = {
273 'format_id': format_id,
274 'quality': quality(format_id),
275 'ext': ext,
276 }
277 if video_url.startswith('rtmp'):
278 m = re.search(
279 r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', video_url)
280 if not m:
281 continue
282 fmt.update({
283 'ext': 'flv',
284 'url': m.group('url'),
285 'app': m.group('app'),
286 'play_path': m.group('playpath'),
287 'preference': -1,
288 })
289 else:
290 fmt.update({
291 'url': video_url,
292 })
293 formats.append(fmt)
294
295 if not formats and video.get('is_geo_blocked'):
296 self.raise_geo_restricted(
297 'This content might not be available in your country due to copyright reasons',
298 metadata_available=True)
299
300 self._sort_formats(formats)
301
302 # TODO: webvtt in m3u8
303 subtitles = {}
304 sami_path = video.get('sami_path')
305 if sami_path:
306 lang = self._search_regex(
307 r'_([a-z]{2})\.xml', sami_path, 'lang',
308 default=compat_urlparse.urlparse(url).netloc.rsplit('.', 1)[-1])
309 subtitles[lang] = [{
310 'url': sami_path,
311 }]
312
313 series = video.get('format_title')
314 episode_number = int_or_none(video.get('format_position', {}).get('episode'))
315 season = video.get('_embedded', {}).get('season', {}).get('title')
316 season_number = int_or_none(video.get('format_position', {}).get('season'))
317
318 return {
319 'id': video_id,
320 'title': title,
321 'description': video.get('description'),
322 'series': series,
323 'episode_number': episode_number,
324 'season': season,
325 'season_number': season_number,
326 'duration': int_or_none(video.get('duration')),
327 'timestamp': parse_iso8601(video.get('created_at')),
328 'view_count': try_get(video, lambda x: x['views']['total'], int),
329 'age_limit': int_or_none(video.get('age_limit', 0)),
330 'formats': formats,
331 'subtitles': subtitles,
332 }
333
334
335 class ViafreeIE(InfoExtractor):
336 _VALID_URL = r'''(?x)
337 https?://
338 (?:www\.)?
339 viafree\.(?P<country>dk|no|se|fi)
340 /(?P<id>(?:program(?:mer)?|ohjelmat)?/(?:[^/]+/)+[^/?#&]+)
341 '''
342 _TESTS = [{
343 'url': 'http://www.viafree.no/programmer/underholdning/det-beste-vorspielet/sesong-2/episode-1',
344 'info_dict': {
345 'id': '757786',
346 'ext': 'mp4',
347 'title': 'Det beste vorspielet - Sesong 2 - Episode 1',
348 'description': 'md5:b632cb848331404ccacd8cd03e83b4c3',
349 'series': 'Det beste vorspielet',
350 'season_number': 2,
351 'duration': 1116,
352 'timestamp': 1471200600,
353 'upload_date': '20160814',
354 },
355 'params': {
356 'skip_download': True,
357 },
358 }, {
359 'url': 'https://www.viafree.dk/programmer/humor/comedy-central-roast-of-charlie-sheen/film/1047660',
360 'info_dict': {
361 'id': '1047660',
362 'ext': 'mp4',
363 'title': 'Comedy Central Roast of Charlie Sheen - Comedy Central Roast of Charlie Sheen',
364 'description': 'md5:ec956d941ae9fd7c65a48fd64951dc6d',
365 'series': 'Comedy Central Roast of Charlie Sheen',
366 'season_number': 1,
367 'duration': 3747,
368 'timestamp': 1608246060,
369 'upload_date': '20201217'
370 },
371 'params': {
372 'skip_download': True
373 }
374 }, {
375 # with relatedClips
376 'url': 'http://www.viafree.se/program/reality/sommaren-med-youtube-stjarnorna/sasong-1/avsnitt-1',
377 'only_matching': True,
378 }, {
379 # Different og:image URL schema
380 'url': 'http://www.viafree.se/program/reality/sommaren-med-youtube-stjarnorna/sasong-1/avsnitt-2',
381 'only_matching': True,
382 }, {
383 'url': 'http://www.viafree.se/program/livsstil/husraddarna/sasong-2/avsnitt-2',
384 'only_matching': True,
385 }, {
386 'url': 'http://www.viafree.dk/programmer/reality/paradise-hotel/saeson-7/episode-5',
387 'only_matching': True,
388 }, {
389 'url': 'http://www.viafree.se/program/underhallning/i-like-radio-live/sasong-1/676869',
390 'only_matching': True,
391 }, {
392 'url': 'https://www.viafree.fi/ohjelmat/entertainment/amazing-makeovers/kausi-7/jakso-2',
393 'only_matching': True,
394 }]
395 _GEO_BYPASS = False
396
397 def _real_extract(self, url):
398 country, path = self._match_valid_url(url).groups()
399 content = self._download_json(
400 'https://viafree-content.mtg-api.com/viafree-content/v1/%s/path/%s' % (country, path), path)
401 program = content['_embedded']['viafreeBlocks'][0]['_embedded']['program']
402 guid = program['guid']
403 meta = content['meta']
404 title = meta['title']
405
406 try:
407 stream_href = self._download_json(
408 program['_links']['streamLink']['href'], guid,
409 headers=self.geo_verification_headers())['embedded']['prioritizedStreams'][0]['links']['stream']['href']
410 except ExtractorError as e:
411 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
412 self.raise_geo_restricted(countries=[country])
413 raise
414
415 formats, subtitles = self._extract_m3u8_formats_and_subtitles(stream_href, guid, 'mp4')
416 self._sort_formats(formats)
417 episode = program.get('episode') or {}
418 return {
419 'id': guid,
420 'title': title,
421 'thumbnail': meta.get('image'),
422 'description': meta.get('description'),
423 'series': episode.get('seriesTitle'),
424 'subtitles': subtitles,
425 'episode_number': int_or_none(episode.get('episodeNumber')),
426 'season_number': int_or_none(episode.get('seasonNumber')),
427 'duration': int_or_none(try_get(program, lambda x: x['video']['duration']['milliseconds']), 1000),
428 'timestamp': parse_iso8601(try_get(program, lambda x: x['availability']['start'])),
429 'formats': formats,
430 }
431
432
433 class TVPlayHomeIE(InfoExtractor):
434 _VALID_URL = r'https?://(?:tv3?)?play\.(?:tv3\.lt|skaties\.lv|tv3\.ee)/(?:[^/]+/)*[^/?#&]+-(?P<id>\d+)'
435 _TESTS = [{
436 'url': 'https://tvplay.tv3.lt/aferistai-n-7/aferistai-10047125/',
437 'info_dict': {
438 'id': '366367',
439 'ext': 'mp4',
440 'title': 'Aferistai',
441 'description': 'Aferistai. Kalėdinė pasaka.',
442 'series': 'Aferistai [N-7]',
443 'season': '1 sezonas',
444 'season_number': 1,
445 'duration': 464,
446 'timestamp': 1394209658,
447 'upload_date': '20140307',
448 'age_limit': 18,
449 },
450 'params': {
451 'skip_download': True,
452 },
453 }, {
454 'url': 'https://tvplay.skaties.lv/vinas-melo-labak/vinas-melo-labak-10280317/',
455 'only_matching': True,
456 }, {
457 'url': 'https://tvplay.tv3.ee/cool-d-ga-mehhikosse/cool-d-ga-mehhikosse-10044354/',
458 'only_matching': True,
459 }, {
460 'url': 'https://play.tv3.lt/aferistai-10047125',
461 'only_matching': True,
462 }, {
463 'url': 'https://tv3play.skaties.lv/vinas-melo-labak-10280317',
464 'only_matching': True,
465 }, {
466 'url': 'https://play.tv3.ee/cool-d-ga-mehhikosse-10044354',
467 'only_matching': True,
468 }]
469
470 def _real_extract(self, url):
471 video_id = self._match_id(url)
472
473 asset = self._download_json(
474 urljoin(url, '/sb/public/asset/' + video_id), video_id)
475
476 m3u8_url = asset['movie']['contentUrl']
477 video_id = asset['assetId']
478 asset_title = asset['title']
479 title = asset_title['title']
480
481 formats = self._extract_m3u8_formats(
482 m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls')
483 self._sort_formats(formats)
484
485 thumbnails = None
486 image_url = asset.get('imageUrl')
487 if image_url:
488 thumbnails = [{
489 'url': urljoin(url, image_url),
490 'ext': 'jpg',
491 }]
492
493 metadata = asset.get('metadata') or {}
494
495 return {
496 'id': video_id,
497 'title': title,
498 'description': asset_title.get('summaryLong') or asset_title.get('summaryShort'),
499 'thumbnails': thumbnails,
500 'duration': parse_duration(asset_title.get('runTime')),
501 'series': asset.get('tvSeriesTitle'),
502 'season': asset.get('tvSeasonTitle'),
503 'season_number': int_or_none(metadata.get('seasonNumber')),
504 'episode': asset_title.get('titleBrief'),
505 'episode_number': int_or_none(metadata.get('episodeNumber')),
506 'formats': formats,
507 }