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.

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