You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
12 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. from .common import InfoExtractor
  5. from .gigya import GigyaBaseIE
  6. from ..compat import compat_HTTPError
  7. from ..utils import (
  8. ExtractorError,
  9. strip_or_none,
  10. float_or_none,
  11. int_or_none,
  12. parse_iso8601,
  13. )
  14. class CanvasIE(InfoExtractor):
  15. _VALID_URL = r'https?://mediazone\.vrt\.be/api/v1/(?P<site_id>canvas|een|ketnet|vrtvideo)/assets/(?P<id>[^/?#&]+)'
  16. _TESTS = [{
  17. 'url': 'https://mediazone.vrt.be/api/v1/ketnet/assets/md-ast-4ac54990-ce66-4d00-a8ca-9eac86f4c475',
  18. 'md5': '90139b746a0a9bd7bb631283f6e2a64e',
  19. 'info_dict': {
  20. 'id': 'md-ast-4ac54990-ce66-4d00-a8ca-9eac86f4c475',
  21. 'display_id': 'md-ast-4ac54990-ce66-4d00-a8ca-9eac86f4c475',
  22. 'ext': 'flv',
  23. 'title': 'Nachtwacht: De Greystook',
  24. 'description': 'md5:1db3f5dc4c7109c821261e7512975be7',
  25. 'thumbnail': r're:^https?://.*\.jpg$',
  26. 'duration': 1468.03,
  27. },
  28. 'expected_warnings': ['is not a supported codec', 'Unknown MIME type'],
  29. }, {
  30. 'url': 'https://mediazone.vrt.be/api/v1/canvas/assets/mz-ast-5e5f90b6-2d72-4c40-82c2-e134f884e93e',
  31. 'only_matching': True,
  32. }]
  33. def _real_extract(self, url):
  34. mobj = re.match(self._VALID_URL, url)
  35. site_id, video_id = mobj.group('site_id'), mobj.group('id')
  36. data = self._download_json(
  37. 'https://mediazone.vrt.be/api/v1/%s/assets/%s'
  38. % (site_id, video_id), video_id)
  39. title = data['title']
  40. description = data.get('description')
  41. formats = []
  42. for target in data['targetUrls']:
  43. format_url, format_type = target.get('url'), target.get('type')
  44. if not format_url or not format_type:
  45. continue
  46. if format_type == 'HLS':
  47. formats.extend(self._extract_m3u8_formats(
  48. format_url, video_id, 'mp4', entry_protocol='m3u8_native',
  49. m3u8_id=format_type, fatal=False))
  50. elif format_type == 'HDS':
  51. formats.extend(self._extract_f4m_formats(
  52. format_url, video_id, f4m_id=format_type, fatal=False))
  53. elif format_type == 'MPEG_DASH':
  54. formats.extend(self._extract_mpd_formats(
  55. format_url, video_id, mpd_id=format_type, fatal=False))
  56. elif format_type == 'HSS':
  57. formats.extend(self._extract_ism_formats(
  58. format_url, video_id, ism_id='mss', fatal=False))
  59. else:
  60. formats.append({
  61. 'format_id': format_type,
  62. 'url': format_url,
  63. })
  64. self._sort_formats(formats)
  65. subtitles = {}
  66. subtitle_urls = data.get('subtitleUrls')
  67. if isinstance(subtitle_urls, list):
  68. for subtitle in subtitle_urls:
  69. subtitle_url = subtitle.get('url')
  70. if subtitle_url and subtitle.get('type') == 'CLOSED':
  71. subtitles.setdefault('nl', []).append({'url': subtitle_url})
  72. return {
  73. 'id': video_id,
  74. 'display_id': video_id,
  75. 'title': title,
  76. 'description': description,
  77. 'formats': formats,
  78. 'duration': float_or_none(data.get('duration'), 1000),
  79. 'thumbnail': data.get('posterImageUrl'),
  80. 'subtitles': subtitles,
  81. }
  82. class CanvasEenIE(InfoExtractor):
  83. IE_DESC = 'canvas.be and een.be'
  84. _VALID_URL = r'https?://(?:www\.)?(?P<site_id>canvas|een)\.be/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  85. _TESTS = [{
  86. 'url': 'http://www.canvas.be/video/de-afspraak/najaar-2015/de-afspraak-veilt-voor-de-warmste-week',
  87. 'md5': 'ed66976748d12350b118455979cca293',
  88. 'info_dict': {
  89. 'id': 'mz-ast-5e5f90b6-2d72-4c40-82c2-e134f884e93e',
  90. 'display_id': 'de-afspraak-veilt-voor-de-warmste-week',
  91. 'ext': 'flv',
  92. 'title': 'De afspraak veilt voor de Warmste Week',
  93. 'description': 'md5:24cb860c320dc2be7358e0e5aa317ba6',
  94. 'thumbnail': r're:^https?://.*\.jpg$',
  95. 'duration': 49.02,
  96. },
  97. 'expected_warnings': ['is not a supported codec'],
  98. }, {
  99. # with subtitles
  100. 'url': 'http://www.canvas.be/video/panorama/2016/pieter-0167',
  101. 'info_dict': {
  102. 'id': 'mz-ast-5240ff21-2d30-4101-bba6-92b5ec67c625',
  103. 'display_id': 'pieter-0167',
  104. 'ext': 'mp4',
  105. 'title': 'Pieter 0167',
  106. 'description': 'md5:943cd30f48a5d29ba02c3a104dc4ec4e',
  107. 'thumbnail': r're:^https?://.*\.jpg$',
  108. 'duration': 2553.08,
  109. 'subtitles': {
  110. 'nl': [{
  111. 'ext': 'vtt',
  112. }],
  113. },
  114. },
  115. 'params': {
  116. 'skip_download': True,
  117. },
  118. 'skip': 'Pagina niet gevonden',
  119. }, {
  120. 'url': 'https://www.een.be/sorry-voor-alles/herbekijk-sorry-voor-alles',
  121. 'info_dict': {
  122. 'id': 'mz-ast-11a587f8-b921-4266-82e2-0bce3e80d07f',
  123. 'display_id': 'herbekijk-sorry-voor-alles',
  124. 'ext': 'mp4',
  125. 'title': 'Herbekijk Sorry voor alles',
  126. 'description': 'md5:8bb2805df8164e5eb95d6a7a29dc0dd3',
  127. 'thumbnail': r're:^https?://.*\.jpg$',
  128. 'duration': 3788.06,
  129. },
  130. 'params': {
  131. 'skip_download': True,
  132. },
  133. 'skip': 'Episode no longer available',
  134. }, {
  135. 'url': 'https://www.canvas.be/check-point/najaar-2016/de-politie-uw-vriend',
  136. 'only_matching': True,
  137. }]
  138. def _real_extract(self, url):
  139. mobj = re.match(self._VALID_URL, url)
  140. site_id, display_id = mobj.group('site_id'), mobj.group('id')
  141. webpage = self._download_webpage(url, display_id)
  142. title = strip_or_none(self._search_regex(
  143. r'<h1[^>]+class="video__body__header__title"[^>]*>(.+?)</h1>',
  144. webpage, 'title', default=None) or self._og_search_title(
  145. webpage, default=None))
  146. video_id = self._html_search_regex(
  147. r'data-video=(["\'])(?P<id>(?:(?!\1).)+)\1', webpage, 'video id',
  148. group='id')
  149. return {
  150. '_type': 'url_transparent',
  151. 'url': 'https://mediazone.vrt.be/api/v1/%s/assets/%s' % (site_id, video_id),
  152. 'ie_key': CanvasIE.ie_key(),
  153. 'id': video_id,
  154. 'display_id': display_id,
  155. 'title': title,
  156. 'description': self._og_search_description(webpage),
  157. }
  158. class VrtNUIE(GigyaBaseIE):
  159. IE_DESC = 'VrtNU.be'
  160. _VALID_URL = r'https?://(?:www\.)?vrt\.be/(?P<site_id>vrtnu)/(?:[^/]+/)*(?P<id>[^/?#&]+)'
  161. _TESTS = [{
  162. 'url': 'https://www.vrt.be/vrtnu/a-z/postbus-x/1/postbus-x-s1a1/',
  163. 'info_dict': {
  164. 'id': 'pbs-pub-2e2d8c27-df26-45c9-9dc6-90c78153044d$vid-90c932b1-e21d-4fb8-99b1-db7b49cf74de',
  165. 'ext': 'flv',
  166. 'title': 'De zwarte weduwe',
  167. 'description': 'md5:d90c21dced7db869a85db89a623998d4',
  168. 'duration': 1457.04,
  169. 'thumbnail': r're:^https?://.*\.jpg$',
  170. 'season': '1',
  171. 'season_number': 1,
  172. 'episode_number': 1,
  173. },
  174. 'skip': 'This video is only available for registered users'
  175. }]
  176. _NETRC_MACHINE = 'vrtnu'
  177. _APIKEY = '3_0Z2HujMtiWq_pkAjgnS2Md2E11a1AwZjYiBETtwNE-EoEHDINgtnvcAOpNgmrVGy'
  178. _CONTEXT_ID = 'R3595707040'
  179. def _real_initialize(self):
  180. self._login()
  181. def _login(self):
  182. username, password = self._get_login_info()
  183. if username is None:
  184. return
  185. auth_data = {
  186. 'APIKey': self._APIKEY,
  187. 'targetEnv': 'jssdk',
  188. 'loginID': username,
  189. 'password': password,
  190. 'authMode': 'cookie',
  191. }
  192. auth_info = self._gigya_login(auth_data)
  193. # Sometimes authentication fails for no good reason, retry
  194. login_attempt = 1
  195. while login_attempt <= 3:
  196. try:
  197. # When requesting a token, no actual token is returned, but the
  198. # necessary cookies are set.
  199. self._request_webpage(
  200. 'https://token.vrt.be',
  201. None, note='Requesting a token', errnote='Could not get a token',
  202. headers={
  203. 'Content-Type': 'application/json',
  204. 'Referer': 'https://www.vrt.be/vrtnu/',
  205. },
  206. data=json.dumps({
  207. 'uid': auth_info['UID'],
  208. 'uidsig': auth_info['UIDSignature'],
  209. 'ts': auth_info['signatureTimestamp'],
  210. 'email': auth_info['profile']['email'],
  211. }).encode('utf-8'))
  212. except ExtractorError as e:
  213. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
  214. login_attempt += 1
  215. self.report_warning('Authentication failed')
  216. self._sleep(1, None, msg_template='Waiting for %(timeout)s seconds before trying again')
  217. else:
  218. raise e
  219. else:
  220. break
  221. def _real_extract(self, url):
  222. display_id = self._match_id(url)
  223. webpage = self._download_webpage(url, display_id)
  224. title = self._html_search_regex(
  225. r'(?ms)<h1 class="content__heading">(.+?)</h1>',
  226. webpage, 'title').strip()
  227. description = self._html_search_regex(
  228. r'(?ms)<div class="content__description">(.+?)</div>',
  229. webpage, 'description', default=None)
  230. season = self._html_search_regex(
  231. [r'''(?xms)<div\ class="tabs__tab\ tabs__tab--active">\s*
  232. <span>seizoen\ (.+?)</span>\s*
  233. </div>''',
  234. r'<option value="seizoen (\d{1,3})" data-href="[^"]+?" selected>'],
  235. webpage, 'season', default=None)
  236. season_number = int_or_none(season)
  237. episode_number = int_or_none(self._html_search_regex(
  238. r'''(?xms)<div\ class="content__episode">\s*
  239. <abbr\ title="aflevering">afl</abbr>\s*<span>(\d+)</span>
  240. </div>''',
  241. webpage, 'episode_number', default=None))
  242. release_date = parse_iso8601(self._html_search_regex(
  243. r'(?ms)<div class="content__broadcastdate">\s*<time\ datetime="(.+?)"',
  244. webpage, 'release_date', default=None))
  245. # If there's a ? or a # in the URL, remove them and everything after
  246. clean_url = url.split('?')[0].split('#')[0].strip('/')
  247. securevideo_url = clean_url + '.mssecurevideo.json'
  248. try:
  249. video = self._download_json(securevideo_url, display_id)
  250. except ExtractorError as e:
  251. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
  252. self.raise_login_required()
  253. raise
  254. # We are dealing with a '../<show>.relevant' URL
  255. redirect_url = video.get('url')
  256. if redirect_url:
  257. return self.url_result(self._proto_relative_url(redirect_url, 'https:'))
  258. # There is only one entry, but with an unknown key, so just get
  259. # the first one
  260. video_id = list(video.values())[0].get('videoid')
  261. return {
  262. '_type': 'url_transparent',
  263. 'url': 'https://mediazone.vrt.be/api/v1/vrtvideo/assets/%s' % video_id,
  264. 'ie_key': CanvasIE.ie_key(),
  265. 'id': video_id,
  266. 'display_id': display_id,
  267. 'title': title,
  268. 'description': description,
  269. 'season': season,
  270. 'season_number': season_number,
  271. 'episode_number': episode_number,
  272. 'release_date': release_date,
  273. }