]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/konserthusetplay.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / konserthusetplay.py
CommitLineData
8628d26f 1from .common import InfoExtractor
5a7699bb 2from ..utils import (
74af9c70 3 determine_ext,
5a7699bb
S
4 float_or_none,
5 int_or_none,
3052a30d 6 url_or_none,
5a7699bb 7)
8628d26f 8
9
10class KonserthusetPlayIE(InfoExtractor):
99a0baf3
AS
11 _VALID_URL = r'https?://(?:www\.)?(?:konserthusetplay|rspoplay)\.se/\?.*\bm=(?P<id>[^&]+)'
12 _TESTS = [{
8628d26f 13 'url': 'http://www.konserthusetplay.se/?m=CKDDnlCY-dhWAAqiMERd-A',
99a0baf3 14 'md5': 'e3fd47bf44e864bd23c08e487abe1967',
8628d26f 15 'info_dict': {
16 'id': 'CKDDnlCY-dhWAAqiMERd-A',
99a0baf3 17 'ext': 'mp4',
8628d26f 18 'title': 'Orkesterns instrument: Valthornen',
19 'description': 'md5:f10e1f0030202020396a4d712d2fa827',
5a7699bb 20 'thumbnail': 're:^https?://.*$',
99a0baf3 21 'duration': 398.76,
5a7699bb 22 },
99a0baf3
AS
23 }, {
24 'url': 'http://rspoplay.se/?m=elWuEH34SMKvaO4wO_cHBw',
25 'only_matching': True,
26 }]
8628d26f 27
28 def _real_extract(self, url):
29 video_id = self._match_id(url)
5a7699bb 30
8628d26f 31 webpage = self._download_webpage(url, video_id)
5a7699bb
S
32
33 e = self._search_regex(
34 r'https?://csp\.picsearch\.com/rest\?.*\be=(.+?)[&"\']', webpage, 'e')
35
36 rest = self._download_json(
37 'http://csp.picsearch.com/rest?e=%s&containerId=mediaplayer&i=object' % e,
38 video_id, transform_source=lambda s: s[s.index('{'):s.rindex('}') + 1])
39
40 media = rest['media']
41 player_config = media['playerconfig']
42 playlist = player_config['playlist']
43
74af9c70 44 source = next(f for f in playlist if f.get('bitrates') or f.get('provider'))
5a7699bb 45
5a7699bb
S
46 FORMAT_ID_REGEX = r'_([^_]+)_h264m\.mp4'
47
48 formats = []
49
74af9c70
S
50 m3u8_url = source.get('url')
51 if m3u8_url and determine_ext(m3u8_url) == 'm3u8':
52 formats.extend(self._extract_m3u8_formats(
53 m3u8_url, video_id, 'mp4', entry_protocol='m3u8_native',
54 m3u8_id='hls', fatal=False))
55
5a7699bb
S
56 fallback_url = source.get('fallbackUrl')
57 fallback_format_id = None
58 if fallback_url:
59 fallback_format_id = self._search_regex(
60 FORMAT_ID_REGEX, fallback_url, 'format id', default=None)
61
b6bfa6fb
S
62 connection_url = (player_config.get('rtmp', {}).get(
63 'netConnectionUrl') or player_config.get(
64 'plugins', {}).get('bwcheck', {}).get('netConnectionUrl'))
5a7699bb
S
65 if connection_url:
66 for f in source['bitrates']:
67 video_url = f.get('url')
68 if not video_url:
69 continue
70 format_id = self._search_regex(
71 FORMAT_ID_REGEX, video_url, 'format id', default=None)
72 f_common = {
73 'vbr': int_or_none(f.get('bitrate')),
74 'width': int_or_none(f.get('width')),
75 'height': int_or_none(f.get('height')),
76 }
77 f = f_common.copy()
78 f.update({
79 'url': connection_url,
80 'play_path': video_url,
81 'format_id': 'rtmp-%s' % format_id if format_id else 'rtmp',
82 'ext': 'flv',
83 })
84 formats.append(f)
85 if format_id and format_id == fallback_format_id:
86 f = f_common.copy()
87 f.update({
88 'url': fallback_url,
89 'format_id': 'http-%s' % format_id if format_id else 'http',
90 })
91 formats.append(f)
92
93 if not formats and fallback_url:
94 formats.append({
95 'url': fallback_url,
96 })
97
5a7699bb
S
98 title = player_config.get('title') or media['title']
99 description = player_config.get('mediaInfo', {}).get('description')
100 thumbnail = media.get('image')
101 duration = float_or_none(media.get('duration'), 1000)
102
af59bddc
S
103 subtitles = {}
104 captions = source.get('captionsAvailableLanguages')
105 if isinstance(captions, dict):
106 for lang, subtitle_url in captions.items():
3052a30d
S
107 subtitle_url = url_or_none(subtitle_url)
108 if lang != 'none' and subtitle_url:
af59bddc
S
109 subtitles.setdefault(lang, []).append({'url': subtitle_url})
110
8628d26f 111 return {
112 'id': video_id,
113 'title': title,
114 'description': description,
5a7699bb
S
115 'thumbnail': thumbnail,
116 'duration': duration,
117 'formats': formats,
af59bddc 118 'subtitles': subtitles,
8628d26f 119 }