2 from __future__ 
import unicode_literals
 
   4 from .common 
import InfoExtractor
 
   5 from ..compat 
import compat_str
 
  13 class KonserthusetPlayIE(InfoExtractor
): 
  14     _VALID_URL 
= r
'https?://(?:www\.)?(?:konserthusetplay|rspoplay)\.se/\?.*\bm=(?P<id>[^&]+)' 
  16         'url': 'http://www.konserthusetplay.se/?m=CKDDnlCY-dhWAAqiMERd-A', 
  17         'md5': 'e3fd47bf44e864bd23c08e487abe1967', 
  19             'id': 'CKDDnlCY-dhWAAqiMERd-A', 
  21             'title': 'Orkesterns instrument: Valthornen', 
  22             'description': 'md5:f10e1f0030202020396a4d712d2fa827', 
  23             'thumbnail': 're:^https?://.*$', 
  27         'url': 'http://rspoplay.se/?m=elWuEH34SMKvaO4wO_cHBw', 
  28         'only_matching': True, 
  31     def _real_extract(self
, url
): 
  32         video_id 
= self
._match
_id
(url
) 
  34         webpage 
= self
._download
_webpage
(url
, video_id
) 
  36         e 
= self
._search
_regex
( 
  37             r
'https?://csp\.picsearch\.com/rest\?.*\be=(.+?)[&"\']', webpage, 'e
') 
  39         rest = self._download_json( 
  40             'http
://csp
.picsearch
.com
/rest?e
=%s&containerId
=mediaplayer
&i
=object' % e, 
  41             video_id, transform_source=lambda s: s[s.index('{'):s.rindex('}') + 1]) 
  44         player_config = media['playerconfig
'] 
  45         playlist = player_config['playlist
'] 
  47         source = next(f for f in playlist if f.get('bitrates
') or f.get('provider
')) 
  49         FORMAT_ID_REGEX = r'_([^_
]+)_h264m\
.mp4
' 
  53         m3u8_url = source.get('url
') 
  54         if m3u8_url and determine_ext(m3u8_url) == 'm3u8
': 
  55             formats.extend(self._extract_m3u8_formats( 
  56                 m3u8_url, video_id, 'mp4
', entry_protocol='m3u8_native
', 
  57                 m3u8_id='hls
', fatal=False)) 
  59         fallback_url = source.get('fallbackUrl
') 
  60         fallback_format_id = None 
  62             fallback_format_id = self._search_regex( 
  63                 FORMAT_ID_REGEX, fallback_url, 'format 
id', default=None) 
  65         connection_url = (player_config.get('rtmp
', {}).get( 
  66             'netConnectionUrl
') or player_config.get( 
  67             'plugins
', {}).get('bwcheck
', {}).get('netConnectionUrl
')) 
  69             for f in source['bitrates
']: 
  70                 video_url = f.get('url
') 
  73                 format_id = self._search_regex( 
  74                     FORMAT_ID_REGEX, video_url, 'format 
id', default=None) 
  76                     'vbr
': int_or_none(f.get('bitrate
')), 
  77                     'width
': int_or_none(f.get('width
')), 
  78                     'height
': int_or_none(f.get('height
')), 
  82                     'url
': connection_url, 
  83                     'play_path
': video_url, 
  84                     'format_id
': 'rtmp
-%s' % format_id if format_id else 'rtmp
', 
  88                 if format_id and format_id == fallback_format_id: 
  92                         'format_id
': 'http
-%s' % format_id if format_id else 'http
', 
  96         if not formats and fallback_url: 
 101         self._sort_formats(formats) 
 103         title = player_config.get('title
') or media['title
'] 
 104         description = player_config.get('mediaInfo
', {}).get('description
') 
 105         thumbnail = media.get('image
') 
 106         duration = float_or_none(media.get('duration
'), 1000) 
 109         captions = source.get('captionsAvailableLanguages
') 
 110         if isinstance(captions, dict): 
 111             for lang, subtitle_url in captions.items(): 
 112                 if lang != 'none
' and isinstance(subtitle_url, compat_str): 
 113                     subtitles.setdefault(lang, []).append({'url
': subtitle_url}) 
 118             'description
': description, 
 119             'thumbnail
': thumbnail, 
 120             'duration
': duration, 
 122             'subtitles
': subtitles,