]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/konserthusetplay.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / konserthusetplay.py
1 from .common import InfoExtractor
2 from ..utils import (
3 determine_ext,
4 float_or_none,
5 int_or_none,
6 url_or_none,
7 )
8
9
10 class KonserthusetPlayIE(InfoExtractor):
11 _VALID_URL = r'https?://(?:www\.)?(?:konserthusetplay|rspoplay)\.se/\?.*\bm=(?P<id>[^&]+)'
12 _TESTS = [{
13 'url': 'http://www.konserthusetplay.se/?m=CKDDnlCY-dhWAAqiMERd-A',
14 'md5': 'e3fd47bf44e864bd23c08e487abe1967',
15 'info_dict': {
16 'id': 'CKDDnlCY-dhWAAqiMERd-A',
17 'ext': 'mp4',
18 'title': 'Orkesterns instrument: Valthornen',
19 'description': 'md5:f10e1f0030202020396a4d712d2fa827',
20 'thumbnail': 're:^https?://.*$',
21 'duration': 398.76,
22 },
23 }, {
24 'url': 'http://rspoplay.se/?m=elWuEH34SMKvaO4wO_cHBw',
25 'only_matching': True,
26 }]
27
28 def _real_extract(self, url):
29 video_id = self._match_id(url)
30
31 webpage = self._download_webpage(url, video_id)
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
44 source = next(f for f in playlist if f.get('bitrates') or f.get('provider'))
45
46 FORMAT_ID_REGEX = r'_([^_]+)_h264m\.mp4'
47
48 formats = []
49
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
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
62 connection_url = (player_config.get('rtmp', {}).get(
63 'netConnectionUrl') or player_config.get(
64 'plugins', {}).get('bwcheck', {}).get('netConnectionUrl'))
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
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
103 subtitles = {}
104 captions = source.get('captionsAvailableLanguages')
105 if isinstance(captions, dict):
106 for lang, subtitle_url in captions.items():
107 subtitle_url = url_or_none(subtitle_url)
108 if lang != 'none' and subtitle_url:
109 subtitles.setdefault(lang, []).append({'url': subtitle_url})
110
111 return {
112 'id': video_id,
113 'title': title,
114 'description': description,
115 'thumbnail': thumbnail,
116 'duration': duration,
117 'formats': formats,
118 'subtitles': subtitles,
119 }