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