]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/tvplay.py
[fc2] Fix extraction (#2572)
[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_iso8601,
16 qualities,
17 traverse_obj,
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'''(?x)
435 https?://
436 (?:tv3?)?
437 play\.(?:tv3|skaties)\.(?P<country>lv|lt|ee)/
438 (?P<live>lives/)?
439 [^?#&]+(?:episode|programme|clip)-(?P<id>\d+)
440 '''
441 _TESTS = [{
442 'url': 'https://play.tv3.lt/series/gauju-karai-karveliai,serial-2343791/serija-8,episode-2343828',
443 'info_dict': {
444 'id': '2343828',
445 'ext': 'mp4',
446 'title': 'Gaujų karai. Karveliai (2021) | S01E08: Serija 8',
447 'description': 'md5:f6fcfbb236429f05531131640dfa7c81',
448 'duration': 2710,
449 'season': 'Gaujų karai. Karveliai',
450 'season_number': 1,
451 'release_year': 2021,
452 'episode': 'Serija 8',
453 'episode_number': 8,
454 },
455 'params': {
456 'skip_download': 'm3u8',
457 },
458 }, {
459 'url': 'https://play.tv3.lt/series/moterys-meluoja-geriau-n-7,serial-2574652/serija-25,episode-3284937',
460 'info_dict': {
461 'id': '3284937',
462 'ext': 'mp4',
463 'season': 'Moterys meluoja geriau [N-7]',
464 'season_number': 14,
465 'release_year': 2021,
466 'episode': 'Serija 25',
467 'episode_number': 25,
468 'title': 'Moterys meluoja geriau [N-7] (2021) | S14|E25: Serija 25',
469 'description': 'md5:c6926e9710f1a126f028fbe121eddb79',
470 'duration': 2440,
471 },
472 'skip': '404'
473 }, {
474 'url': 'https://play.tv3.lt/lives/tv6-lt,live-2838694/optibet-a-lygos-rungtynes-marijampoles-suduva--vilniaus-riteriai,programme-3422014',
475 'only_matching': True,
476 }, {
477 'url': 'https://tv3play.skaties.lv/series/women-lie-better-lv,serial-1024464/women-lie-better-lv,episode-1038762',
478 'only_matching': True,
479 }, {
480 'url': 'https://play.tv3.ee/series/_,serial-2654462/_,episode-2654474',
481 'only_matching': True,
482 }, {
483 'url': 'https://tv3play.skaties.lv/clips/tv3-zinas-valsti-lidz-15novembrim-bus-majsede,clip-3464509',
484 'only_matching': True,
485 }]
486
487 def _real_extract(self, url):
488 country, is_live, video_id = self._match_valid_url(url).groups()
489
490 api_path = 'lives/programmes' if is_live else 'vods'
491 data = self._download_json(
492 urljoin(url, f'/api/products/{api_path}/{video_id}?platform=BROWSER&lang={country.upper()}'),
493 video_id)
494
495 video_type = 'CATCHUP' if is_live else 'MOVIE'
496 stream_id = data['programRecordingId'] if is_live else video_id
497 stream = self._download_json(
498 urljoin(url, f'/api/products/{stream_id}/videos/playlist?videoType={video_type}&platform=BROWSER'), video_id)
499 formats, subtitles = self._extract_m3u8_formats_and_subtitles(
500 stream['sources']['HLS'][0]['src'], video_id, 'mp4', 'm3u8_native', m3u8_id='hls')
501 self._sort_formats(formats)
502
503 thumbnails = set(traverse_obj(
504 data, (('galary', 'images', 'artworks'), ..., ..., ('miniUrl', 'mainUrl')), expected_type=url_or_none))
505
506 return {
507 'id': video_id,
508 'title': self._resolve_title(data),
509 'description': traverse_obj(data, 'description', 'lead'),
510 'duration': int_or_none(data.get('duration')),
511 'season': traverse_obj(data, ('season', 'serial', 'title')),
512 'season_number': int_or_none(traverse_obj(data, ('season', 'number'))),
513 'episode': data.get('title'),
514 'episode_number': int_or_none(data.get('episode')),
515 'release_year': int_or_none(traverse_obj(data, ('season', 'serial', 'year'))),
516 'thumbnails': [{'url': url, 'ext': 'jpg'} for url in thumbnails],
517 'formats': formats,
518 'subtitles': subtitles,
519 }
520
521 @staticmethod
522 def _resolve_title(data):
523 return try_get(data, lambda x: (
524 f'{data["season"]["serial"]["title"]} ({data["season"]["serial"]["year"]}) | '
525 f'S{data["season"]["number"]:02d}E{data["episode"]:02d}: {data["title"]}'
526 )) or data.get('title')