2 from __future__ 
import unicode_literals
 
   7 from .common 
import InfoExtractor
 
  20 class ZattooBaseIE(InfoExtractor
): 
  21     _NETRC_MACHINE 
= 'zattoo' 
  22     _HOST_URL 
= 'https://zattoo.com' 
  24     _power_guide_hash 
= None 
  27         username
, password 
= self
._get
_login
_info
() 
  28         if not username 
or not password
: 
  29             self
.raise_login_required( 
  30                 'A valid %s account is needed to access this media.' 
  31                 % self
._NETRC
_MACHINE
) 
  34             data 
= self
._download
_json
( 
  35                 '%s/zapi/v2/account/login' % self
._HOST
_URL
, None, 'Logging in', 
  36                 data
=urlencode_postdata({ 
  41                     'Referer': '%s/login' % self
._HOST
_URL
, 
  42                     'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 
  44         except ExtractorError 
as e
: 
  45             if isinstance(e
.cause
, compat_HTTPError
) and e
.cause
.code 
== 400: 
  47                     'Unable to login: incorrect username and/or password', 
  51         self
._power
_guide
_hash 
= data
['session']['power_guide_hash'] 
  53     def _real_initialize(self
): 
  54         webpage 
= self
._download
_webpage
( 
  55             self
._HOST
_URL
, None, 'Downloading app token') 
  56         app_token 
= self
._html
_search
_regex
( 
  57             r
'appToken\s*=\s*(["\'])(?P
<token
>(?
:(?
!\
1).)+?
)\
1', 
  58             webpage, 'app token
', group='token
') 
  59         app_version = self._html_search_regex( 
  60             r'<!--\w
+-(.+?
)-', webpage, 'app version
', default='2.8.2') 
  62         # Will setup appropriate cookies 
  63         self._request_webpage( 
  64             '%s/zapi
/v2
/session
/hello
' % self._HOST_URL, None, 
  65             'Opening session
', data=urlencode_postdata({ 
  66                 'client_app_token
': app_token, 
  67                 'uuid
': compat_str(uuid4()), 
  69                 'app_version
': app_version, 
  75     def _extract_cid(self, video_id, channel_name): 
  76         channel_groups = self._download_json( 
  77             '%s/zapi
/v2
/cached
/channels
/%s' % (self._HOST_URL, 
  78                                                self._power_guide_hash), 
  79             video_id, 'Downloading channel 
list', 
  80             query={'details
': False})['channel_groups
'] 
  82         for chgrp in channel_groups: 
  83             channel_list.extend(chgrp['channels
']) 
  86                 chan['cid
'] for chan in channel_list 
  87                 if chan.get('cid
') and ( 
  88                     chan.get('display_alias
') == channel_name or 
  89                     chan.get('cid
') == channel_name)) 
  91             raise ExtractorError('Could 
not extract channel 
id') 
  93     def _extract_cid_and_video_info(self, video_id): 
  94         data = self._download_json( 
  95             '%s/zapi
/program
/details
' % self._HOST_URL, 
  97             'Downloading video information
', 
  99                 'program_id
': video_id, 
 108             'title
': p.get('title
') or p['episode_title
'], 
 109             'description
': p.get('description
'), 
 110             'thumbnail
': p.get('image_url
'), 
 111             'creator
': p.get('channel_name
'), 
 112             'episode
': p.get('episode_title
'), 
 113             'episode_number
': int_or_none(p.get('episode_number
')), 
 114             'season_number
': int_or_none(p.get('season_number
')), 
 115             'release_year
': int_or_none(p.get('year
')), 
 116             'categories
': try_get(p, lambda x: x['categories
'], list), 
 119         return cid, info_dict 
 121     def _extract_formats(self, cid, video_id, record_id=None, is_live=False): 
 123             'https_watch_urls
': True, 
 127             postdata_common.update({'timeshift
': 10800}) 
 128             url = '%s/zapi
/watch
/live
/%s' % (self._HOST_URL, cid) 
 130             url = '%s/zapi
/watch
/recording
/%s' % (self._HOST_URL, record_id) 
 132             url = '%s/zapi
/watch
/recall
/%s/%s' % (self._HOST_URL, cid, video_id) 
 135         for stream_type in ('dash
', 'hls
', 'hls5
', 'hds
'): 
 136             postdata = postdata_common.copy() 
 137             postdata['stream_type
'] = stream_type 
 139             data = self._download_json( 
 140                 url, video_id, 'Downloading 
%s formats
' % stream_type.upper(), 
 141                 data=urlencode_postdata(postdata), fatal=False) 
 145             watch_urls = try_get( 
 146                 data, lambda x: x['stream
']['watch_urls
'], list) 
 150             for watch in watch_urls: 
 151                 if not isinstance(watch, dict): 
 153                 watch_url = watch.get('url
') 
 154                 if not watch_url or not isinstance(watch_url, compat_str): 
 156                 format_id_list = [stream_type] 
 157                 maxrate = watch.get('maxrate
') 
 159                     format_id_list.append(compat_str(maxrate)) 
 160                 audio_channel = watch.get('audio_channel
') 
 162                     format_id_list.append(compat_str(audio_channel)) 
 163                 preference = 1 if audio_channel == 'A
' else None 
 164                 format_id = '-'.join(format_id_list) 
 165                 if stream_type in ('dash
', 'dash_widevine
', 'dash_playready
'): 
 166                     this_formats = self._extract_mpd_formats( 
 167                         watch_url, video_id, mpd_id=format_id, fatal=False) 
 168                 elif stream_type in ('hls
', 'hls5
', 'hls5_fairplay
'): 
 169                     this_formats = self._extract_m3u8_formats( 
 170                         watch_url, video_id, 'mp4
', 
 171                         entry_protocol='m3u8_native
', m3u8_id=format_id, 
 173                 elif stream_type == 'hds
': 
 174                     this_formats = self._extract_f4m_formats( 
 175                         watch_url, video_id, f4m_id=format_id, fatal=False) 
 176                 elif stream_type == 'smooth_playready
': 
 177                     this_formats = self._extract_ism_formats( 
 178                         watch_url, video_id, ism_id=format_id, fatal=False) 
 181                 for this_format in this_formats: 
 182                     this_format['preference
'] = preference 
 183                 formats.extend(this_formats) 
 184         self._sort_formats(formats) 
 187     def _extract_video(self, channel_name, video_id, record_id=None, is_live=False): 
 189             cid = self._extract_cid(video_id, channel_name) 
 192                 'title
': self._live_title(channel_name), 
 196             cid, info_dict = self._extract_cid_and_video_info(video_id) 
 197         formats = self._extract_formats( 
 198             cid, video_id, record_id=record_id, is_live=is_live) 
 199         info_dict['formats
'] = formats 
 203 class QuicklineBaseIE(ZattooBaseIE): 
 204     _NETRC_MACHINE = 'quickline
' 
 205     _HOST_URL = 'https
://mobiltv
.quickline
.com
' 
 208 class QuicklineIE(QuicklineBaseIE): 
 209     _VALID_URL = r'https?
://(?
:www\
.)?mobiltv\
.quickline\
.com
/watch
/(?P
<channel
>[^
/]+)/(?P
<id>[0-9]+)' 
 212         'url
': 'https
://mobiltv
.quickline
.com
/watch
/prosieben
/130671867-maze
-runner
-die
-auserwaehlten
-in-der
-brandwueste
', 
 213         'only_matching
': True, 
 216     def _real_extract(self, url): 
 217         channel_name, video_id = re.match(self._VALID_URL, url).groups() 
 218         return self._extract_video(channel_name, video_id) 
 221 class QuicklineLiveIE(QuicklineBaseIE): 
 222     _VALID_URL = r'https?
://(?
:www\
.)?mobiltv\
.quickline\
.com
/watch
/(?P
<id>[^
/]+)' 
 225         'url
': 'https
://mobiltv
.quickline
.com
/watch
/srf1
', 
 226         'only_matching
': True, 
 230     def suitable(cls, url): 
 231         return False if QuicklineIE.suitable(url) else super(QuicklineLiveIE, cls).suitable(url) 
 233     def _real_extract(self, url): 
 234         channel_name = video_id = self._match_id(url) 
 235         return self._extract_video(channel_name, video_id, is_live=True) 
 238 class ZattooIE(ZattooBaseIE): 
 239     _VALID_URL = r'https?
://(?
:www\
.)?zattoo\
.com
/watch
/(?P
<channel
>[^
/]+?
)/(?P
<id>[0-9]+)[^
/]+(?
:/(?P
<recid
>[0-9]+))?
' 
 241     # Since regular videos are only available for 7 days and recorded videos 
 242     # are only available for a specific user, we cannot have detailed tests. 
 244         'url
': 'https
://zattoo
.com
/watch
/prosieben
/130671867-maze
-runner
-die
-auserwaehlten
-in-der
-brandwueste
', 
 245         'only_matching
': True, 
 247         'url
': 'https
://zattoo
.com
/watch
/srf_zwei
/132905652-eishockey
-spengler
-cup
/102791477/1512211800000/1514433500000/92000', 
 248         'only_matching
': True, 
 251     def _real_extract(self, url): 
 252         channel_name, video_id, record_id = re.match(self._VALID_URL, url).groups() 
 253         return self._extract_video(channel_name, video_id, record_id) 
 256 class ZattooLiveIE(ZattooBaseIE): 
 257     _VALID_URL = r'https?
://(?
:www\
.)?zattoo\
.com
/watch
/(?P
<id>[^
/]+)' 
 260         'url
': 'https
://zattoo
.com
/watch
/srf1
', 
 261         'only_matching
': True, 
 265     def suitable(cls, url): 
 266         return False if ZattooIE.suitable(url) else super(ZattooLiveIE, cls).suitable(url) 
 268     def _real_extract(self, url): 
 269         channel_name = video_id = self._match_id(url) 
 270         return self._extract_video(channel_name, video_id, is_live=True)