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