canvas.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import os
  4. import urlparse
  5. from youtube_dl import utils
  6. from .common import InfoExtractor
  7. class CanvasIE(InfoExtractor):
  8. _VALID_URL = r'(?:https?://)?(?:www\.)?canvas\.be/video/(?P<id>.+)'
  9. _TEST = {
  10. 'url': 'http://www.canvas.be/video/de-afspraak/najaar-2015/de-afspraak-veilt-voor-de-warmste-week',
  11. 'md5': 'ea838375a547ac787d4064d8c7860a6c',
  12. 'info_dict': {
  13. 'id': 'de-afspraak/najaar-2015/de-afspraak-veilt-voor-de-warmste-week',
  14. 'title': 'De afspraak veilt voor de Warmste Week',
  15. 'ext': 'mp4',
  16. 'duration': 49,
  17. }
  18. }
  19. def _real_extract(self, url):
  20. video_id = self._match_id(url)
  21. webpage = self._download_webpage(url, video_id)
  22. title = self._search_regex(
  23. r'<h1 class="video__body__header__title">(.+?)</h1>', webpage,
  24. 'title')
  25. data_video = self._html_search_regex(
  26. r'data-video=(["\'])(?P<id>.+?)\1', webpage, 'data-video', group='id')
  27. json_url = 'https://mediazone.vrt.be/api/v1/canvas/assets/' + data_video
  28. data = self._download_json(json_url, video_id)
  29. formats = []
  30. for target in data['targetUrls']:
  31. if 'type' and 'url' in target:
  32. extension = utils.determine_ext(target['url'])
  33. if target['type'] == 'PROGRESSIVE_DOWNLOAD':
  34. formats.append({
  35. 'format_id': extension,
  36. 'url': target['url'],
  37. 'protocol': 'http',
  38. })
  39. elif target['type'] == 'HLS':
  40. formats.extend(self._extract_m3u8_formats(
  41. target['url'], video_id, entry_protocol='m3u8_native',
  42. ext='mp4',
  43. preference=0,
  44. fatal=False,
  45. m3u8_id='hls'))
  46. elif target['type'] == 'HDS':
  47. formats.append({
  48. 'format_id': extension,
  49. 'url': target['url'],
  50. 'protocol': 'HDS',
  51. })
  52. elif target['type'] == 'RTMP':
  53. formats.append({
  54. 'format_id': extension,
  55. 'url': target['url'],
  56. 'protocol': 'rtmp',
  57. })
  58. elif target['type'] == 'RTSP':
  59. formats.append({
  60. 'format_id': extension,
  61. 'url': target['url'],
  62. 'protocol': 'rtsp',
  63. })
  64. self._sort_formats(formats)
  65. duration = utils.int_or_none(data.get('duration')) / 1000
  66. return {
  67. 'id': video_id,
  68. 'title': title,
  69. 'formats': formats,
  70. 'duration': duration,
  71. }