2 from __future__ import unicode_literals
7 from .common import InfoExtractor
16 class ZattooBaseIE(InfoExtractor):
18 _NETRC_MACHINE = 'zattoo'
19 _HOST_URL = 'https://zattoo.com'
21 _power_guide_hash = None
23 def _login(self, uuid, session_id):
24 (username, password) = self._get_login_info()
25 if not username or not password:
27 'A valid %s account is needed to access this media.' % self._NETRC_MACHINE,
34 request = sanitized_Request(
35 '%s/zapi/v2/account/login' % self._HOST_URL,
36 urlencode_postdata(login_form))
38 'Referer', '%s/login' % self._HOST_URL)
40 'Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
42 'Cookie', 'uuid=%s; beaker.session.id=%s' % (uuid, session_id))
43 response = self._request_webpage(
44 request, None, 'Logging in')
45 data = self._parse_json(response.read(), None)
46 return data['session']['power_guide_hash']
48 def _get_app_token_and_version(self):
49 host_webpage = self._download_webpage(
50 self._HOST_URL, None, 'Downloading %s' % self._HOST_URL)
51 app_token = self._html_search_regex(
52 r'<script.+window\.appToken\s*=\s*\'(.+)\'', host_webpage, 'app token')
53 app_version = self._html_search_regex(
54 r'<!--\w+-(.+?)-', host_webpage, 'app version', default='2.8.2')
55 return app_token, app_version
57 def _say_hello(self, uuid, app_token, app_version):
59 'client_app_token': app_token,
62 'app_version': app_version,
65 request = sanitized_Request(
66 '%s/zapi/v2/session/hello' % self._HOST_URL,
67 urlencode_postdata(postdata))
68 response = self._request_webpage(
69 request, None, 'Say hello')
71 cookie = response.headers.get('Set-Cookie')
72 session_id = self._search_regex(
73 r'beaker\.session\.id\s*=\s*(.+?);', cookie, 'session id')
76 def _extract_cid(self, video_id, channel_name):
77 channel_groups = self._download_json(
78 '%s/zapi/v2/cached/channels/%s' % (self._HOST_URL,
79 self._power_guide_hash),
81 'Downloading available channel list',
82 query={'details': False})['channel_groups']
84 for chgrp in channel_groups:
85 channel_list.extend(chgrp['channels'])
88 chan['cid'] for chan in channel_list
89 if chan['display_alias'] == channel_name or chan['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,
105 'title': data['program']['title'],
106 'description': data['program'].get('description'),
107 'thumbnail': data['program'].get('image_url')
109 cid = data['program']['cid']
110 return cid, info_dict
112 def _extract_formats(self, cid, video_id, record_id=None, is_live=False):
114 'stream_type': 'dash',
115 'https_watch_urls': True,
118 url = '%s/zapi/watch/recording/%s' % (self._HOST_URL, record_id)
120 url = '%s/zapi/watch/recall/%s/%s' % (self._HOST_URL, cid, video_id)
123 postdata.update({'timeshift': 10800})
124 url = '%s/zapi/watch/live/%s' % (self._HOST_URL, cid)
126 data = self._download_json(
127 sanitized_Request(url, urlencode_postdata(postdata)),
128 video_id, 'Downloading dash formats')
131 for elem in data['stream']['watch_urls']:
132 audio_channel = elem.get('audio_channel')
133 maxrate = elem.get('maxrate')
135 self._extract_mpd_formats(
136 elem['url'], video_id,
137 mpd_id='dash-maxrate-%s-channel-%s' % (maxrate, audio_channel), fatal=False))
139 postdata.update({'stream_type': 'hls'})
140 request = sanitized_Request(
141 url, urlencode_postdata(postdata))
142 data = self._download_json(
143 request, video_id, 'Downloading hls formats')
144 for elem in data['stream']['watch_urls']:
145 audio_channel = elem.get('audio_channel')
148 # Prefer audio channel A:
149 if audio_channel == 'A':
152 maxrate = elem.get('maxrate')
154 self._extract_m3u8_formats(
155 elem['url'], video_id, 'mp4', entry_protocol='m3u8_native',
156 preference=preference,
157 m3u8_id='hls-maxrate-%s-channel-%s' % (maxrate, audio_channel),
160 self._sort_formats(formats)
163 def _real_initialize(self):
164 uuid = compat_str(uuid4())
165 app_token, app_version = self._get_app_token_and_version()
166 session_id = self._say_hello(uuid, app_token, app_version)
167 self._power_guide_hash = self._login(uuid, session_id)
169 def _extract_video(self, channel_name, video_id, record_id=None, is_live=False):
171 cid = self._extract_cid(video_id, channel_name)
174 'title': self._live_title(channel_name),
178 cid, info_dict = self._extract_cid_and_video_info(video_id)
179 formats = self._extract_formats(
180 cid, video_id, record_id=record_id, is_live=is_live)
181 info_dict['formats'] = formats
185 class QuicklineBaseIE(ZattooBaseIE):
186 _NETRC_MACHINE = 'quickline'
187 _HOST_URL = 'https://mobiltv.quickline.com'
190 class QuicklineIE(QuicklineBaseIE):
191 _VALID_URL = r'https?://(?:www\.)?mobiltv\.quickline\.com/watch/(?P<channel>[^/]+)/(?P<id>[0-9]+)'
193 def _real_extract(self, url):
194 channel_name, video_id = re.match(self._VALID_URL, url).groups()
195 return self._extract_video(channel_name, video_id)
198 class QuicklineLiveIE(QuicklineBaseIE):
199 _VALID_URL = r'https?://(?:www\.)?mobiltv\.quickline\.com/watch/(?P<id>[^/]+)$'
201 def _real_extract(self, url):
202 channel_name = video_id = self._match_id(url)
203 return self._extract_video(channel_name, video_id, is_live=True)
206 class ZattooIE(ZattooBaseIE):
207 _VALID_URL = r'https?://(?:www\.)?zattoo\.com/watch/(?P<channel>[^/]+?)/(?P<id>[0-9]+)[^/]+(?:/(?P<recid>[0-9]+))?'
209 # Since regular videos are only available for 7 days and recorded videos
210 # are only available for a specific user, we cannot have detailed tests.
212 'url': 'https://zattoo.com/watch/prosieben/130671867-maze-runner-die-auserwaehlten-in-der-brandwueste',
213 'only_matching': True,
215 'url': 'https://zattoo.com/watch/srf_zwei/132905652-eishockey-spengler-cup/102791477/1512211800000/1514433500000/92000',
216 'only_matching': True,
219 def _real_extract(self, url):
220 channel_name, video_id, record_id = re.match(self._VALID_URL, url).groups()
221 return self._extract_video(channel_name, video_id, record_id)
224 class ZattooLiveIE(ZattooBaseIE):
225 _VALID_URL = r'https?://(?:www\.)?zattoo\.com/watch/(?P<id>[^/]+)$'
228 'url': 'https://zattoo.com/watch/srf1',
229 'only_matching': True,
232 def _real_extract(self, url):
233 channel_name = video_id = self._match_id(url)
234 return self._extract_video(channel_name, video_id, is_live=True)