zattoo.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from uuid import uuid4
  5. from .common import InfoExtractor
  6. from ..compat import (
  7. compat_HTTPError,
  8. compat_str,
  9. )
  10. from ..utils import (
  11. ExtractorError,
  12. int_or_none,
  13. try_get,
  14. urlencode_postdata,
  15. )
  16. class ZattooBaseIE(InfoExtractor):
  17. _NETRC_MACHINE = 'zattoo'
  18. _HOST_URL = 'https://zattoo.com'
  19. _power_guide_hash = None
  20. def _login(self):
  21. username, password = self._get_login_info()
  22. if not username or not password:
  23. self.raise_login_required(
  24. 'A valid %s account is needed to access this media.'
  25. % self._NETRC_MACHINE)
  26. try:
  27. data = self._download_json(
  28. '%s/zapi/v2/account/login' % self._HOST_URL, None, 'Logging in',
  29. data=urlencode_postdata({
  30. 'login': username,
  31. 'password': password,
  32. 'remember': 'true',
  33. }), headers={
  34. 'Referer': '%s/login' % self._HOST_URL,
  35. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  36. })
  37. except ExtractorError as e:
  38. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 400:
  39. raise ExtractorError(
  40. 'Unable to login: incorrect username and/or password',
  41. expected=True)
  42. raise
  43. self._power_guide_hash = data['session']['power_guide_hash']
  44. def _real_initialize(self):
  45. webpage = self._download_webpage(
  46. self._HOST_URL, None, 'Downloading app token')
  47. app_token = self._html_search_regex(
  48. r'appToken\s*=\s*(["\'])(?P<token>(?:(?!\1).)+?)\1',
  49. webpage, 'app token', group='token')
  50. app_version = self._html_search_regex(
  51. r'<!--\w+-(.+?)-', webpage, 'app version', default='2.8.2')
  52. # Will setup appropriate cookies
  53. self._request_webpage(
  54. '%s/zapi/v2/session/hello' % self._HOST_URL, None,
  55. 'Opening session', data=urlencode_postdata({
  56. 'client_app_token': app_token,
  57. 'uuid': compat_str(uuid4()),
  58. 'lang': 'en',
  59. 'app_version': app_version,
  60. 'format': 'json',
  61. }))
  62. self._login()
  63. def _extract_cid(self, video_id, channel_name):
  64. channel_groups = self._download_json(
  65. '%s/zapi/v2/cached/channels/%s' % (self._HOST_URL,
  66. self._power_guide_hash),
  67. video_id, 'Downloading channel list',
  68. query={'details': False})['channel_groups']
  69. channel_list = []
  70. for chgrp in channel_groups:
  71. channel_list.extend(chgrp['channels'])
  72. try:
  73. return next(
  74. chan['cid'] for chan in channel_list
  75. if chan.get('cid') and (
  76. chan.get('display_alias') == channel_name or
  77. chan.get('cid') == channel_name))
  78. except StopIteration:
  79. raise ExtractorError('Could not extract channel id')
  80. def _extract_cid_and_video_info(self, video_id):
  81. data = self._download_json(
  82. '%s/zapi/program/details' % self._HOST_URL,
  83. video_id,
  84. 'Downloading video information',
  85. query={
  86. 'program_id': video_id,
  87. 'complete': True
  88. })
  89. p = data['program']
  90. cid = p['cid']
  91. info_dict = {
  92. 'id': video_id,
  93. 'title': p.get('title') or p['episode_title'],
  94. 'description': p.get('description'),
  95. 'thumbnail': p.get('image_url'),
  96. 'creator': p.get('channel_name'),
  97. 'episode': p.get('episode_title'),
  98. 'episode_number': int_or_none(p.get('episode_number')),
  99. 'season_number': int_or_none(p.get('season_number')),
  100. 'release_year': int_or_none(p.get('year')),
  101. 'categories': try_get(p, lambda x: x['categories'], list),
  102. }
  103. return cid, info_dict
  104. def _extract_formats(self, cid, video_id, record_id=None, is_live=False):
  105. postdata_common = {
  106. 'https_watch_urls': True,
  107. }
  108. if is_live:
  109. postdata_common.update({'timeshift': 10800})
  110. url = '%s/zapi/watch/live/%s' % (self._HOST_URL, cid)
  111. elif record_id:
  112. url = '%s/zapi/watch/recording/%s' % (self._HOST_URL, record_id)
  113. else:
  114. url = '%s/zapi/watch/recall/%s/%s' % (self._HOST_URL, cid, video_id)
  115. formats = []
  116. for stream_type in ('dash', 'hls', 'hls5', 'hds'):
  117. postdata = postdata_common.copy()
  118. postdata['stream_type'] = stream_type
  119. data = self._download_json(
  120. url, video_id, 'Downloading %s formats' % stream_type.upper(),
  121. data=urlencode_postdata(postdata), fatal=False)
  122. if not data:
  123. continue
  124. watch_urls = try_get(
  125. data, lambda x: x['stream']['watch_urls'], list)
  126. if not watch_urls:
  127. continue
  128. for watch in watch_urls:
  129. if not isinstance(watch, dict):
  130. continue
  131. watch_url = watch.get('url')
  132. if not watch_url or not isinstance(watch_url, compat_str):
  133. continue
  134. format_id_list = [stream_type]
  135. maxrate = watch.get('maxrate')
  136. if maxrate:
  137. format_id_list.append(compat_str(maxrate))
  138. audio_channel = watch.get('audio_channel')
  139. if audio_channel:
  140. format_id_list.append(compat_str(audio_channel))
  141. preference = 1 if audio_channel == 'A' else None
  142. format_id = '-'.join(format_id_list)
  143. if stream_type in ('dash', 'dash_widevine', 'dash_playready'):
  144. this_formats = self._extract_mpd_formats(
  145. watch_url, video_id, mpd_id=format_id, fatal=False)
  146. elif stream_type in ('hls', 'hls5', 'hls5_fairplay'):
  147. this_formats = self._extract_m3u8_formats(
  148. watch_url, video_id, 'mp4',
  149. entry_protocol='m3u8_native', m3u8_id=format_id,
  150. fatal=False)
  151. elif stream_type == 'hds':
  152. this_formats = self._extract_f4m_formats(
  153. watch_url, video_id, f4m_id=format_id, fatal=False)
  154. elif stream_type == 'smooth_playready':
  155. this_formats = self._extract_ism_formats(
  156. watch_url, video_id, ism_id=format_id, fatal=False)
  157. else:
  158. assert False
  159. for this_format in this_formats:
  160. this_format['preference'] = preference
  161. formats.extend(this_formats)
  162. self._sort_formats(formats)
  163. return formats
  164. def _extract_video(self, channel_name, video_id, record_id=None, is_live=False):
  165. if is_live:
  166. cid = self._extract_cid(video_id, channel_name)
  167. info_dict = {
  168. 'id': channel_name,
  169. 'title': self._live_title(channel_name),
  170. 'is_live': True,
  171. }
  172. else:
  173. cid, info_dict = self._extract_cid_and_video_info(video_id)
  174. formats = self._extract_formats(
  175. cid, video_id, record_id=record_id, is_live=is_live)
  176. info_dict['formats'] = formats
  177. return info_dict
  178. class QuicklineBaseIE(ZattooBaseIE):
  179. _NETRC_MACHINE = 'quickline'
  180. _HOST_URL = 'https://mobiltv.quickline.com'
  181. class QuicklineIE(QuicklineBaseIE):
  182. _VALID_URL = r'https?://(?:www\.)?mobiltv\.quickline\.com/watch/(?P<channel>[^/]+)/(?P<id>[0-9]+)'
  183. _TEST = {
  184. 'url': 'https://mobiltv.quickline.com/watch/prosieben/130671867-maze-runner-die-auserwaehlten-in-der-brandwueste',
  185. 'only_matching': True,
  186. }
  187. def _real_extract(self, url):
  188. channel_name, video_id = re.match(self._VALID_URL, url).groups()
  189. return self._extract_video(channel_name, video_id)
  190. class QuicklineLiveIE(QuicklineBaseIE):
  191. _VALID_URL = r'https?://(?:www\.)?mobiltv\.quickline\.com/watch/(?P<id>[^/]+)'
  192. _TEST = {
  193. 'url': 'https://mobiltv.quickline.com/watch/srf1',
  194. 'only_matching': True,
  195. }
  196. @classmethod
  197. def suitable(cls, url):
  198. return False if QuicklineIE.suitable(url) else super(QuicklineLiveIE, cls).suitable(url)
  199. def _real_extract(self, url):
  200. channel_name = video_id = self._match_id(url)
  201. return self._extract_video(channel_name, video_id, is_live=True)
  202. class ZattooIE(ZattooBaseIE):
  203. _VALID_URL = r'https?://(?:www\.)?zattoo\.com/watch/(?P<channel>[^/]+?)/(?P<id>[0-9]+)[^/]+(?:/(?P<recid>[0-9]+))?'
  204. # Since regular videos are only available for 7 days and recorded videos
  205. # are only available for a specific user, we cannot have detailed tests.
  206. _TESTS = [{
  207. 'url': 'https://zattoo.com/watch/prosieben/130671867-maze-runner-die-auserwaehlten-in-der-brandwueste',
  208. 'only_matching': True,
  209. }, {
  210. 'url': 'https://zattoo.com/watch/srf_zwei/132905652-eishockey-spengler-cup/102791477/1512211800000/1514433500000/92000',
  211. 'only_matching': True,
  212. }]
  213. def _real_extract(self, url):
  214. channel_name, video_id, record_id = re.match(self._VALID_URL, url).groups()
  215. return self._extract_video(channel_name, video_id, record_id)
  216. class ZattooLiveIE(ZattooBaseIE):
  217. _VALID_URL = r'https?://(?:www\.)?zattoo\.com/watch/(?P<id>[^/]+)'
  218. _TEST = {
  219. 'url': 'https://zattoo.com/watch/srf1',
  220. 'only_matching': True,
  221. }
  222. @classmethod
  223. def suitable(cls, url):
  224. return False if ZattooIE.suitable(url) else super(ZattooLiveIE, cls).suitable(url)
  225. def _real_extract(self, url):
  226. channel_name = video_id = self._match_id(url)
  227. return self._extract_video(channel_name, video_id, is_live=True)