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.

161 lines
6.4 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. xpath_text,
  7. find_xpath_attr,
  8. determine_ext,
  9. int_or_none,
  10. unified_strdate,
  11. xpath_element,
  12. ExtractorError,
  13. determine_protocol,
  14. unsmuggle_url,
  15. )
  16. class RadioCanadaIE(InfoExtractor):
  17. IE_NAME = 'radiocanada'
  18. _VALID_URL = r'(?:radiocanada:|https?://ici\.radio-canada\.ca/widgets/mediaconsole/)(?P<app_code>[^:/]+)[:/](?P<id>[0-9]+)'
  19. _TEST = {
  20. 'url': 'http://ici.radio-canada.ca/widgets/mediaconsole/medianet/7184272',
  21. 'info_dict': {
  22. 'id': '7184272',
  23. 'ext': 'mp4',
  24. 'title': 'Le parcours du tireur capté sur vidéo',
  25. 'description': 'Images des caméras de surveillance fournies par la GRC montrant le parcours du tireur d\'Ottawa',
  26. 'upload_date': '20141023',
  27. },
  28. 'params': {
  29. # m3u8 download
  30. 'skip_download': True,
  31. },
  32. }
  33. def _real_extract(self, url):
  34. url, smuggled_data = unsmuggle_url(url, {})
  35. app_code, video_id = re.match(self._VALID_URL, url).groups()
  36. metadata = self._download_xml(
  37. 'http://api.radio-canada.ca/metaMedia/v1/index.ashx',
  38. video_id, note='Downloading metadata XML', query={
  39. 'appCode': app_code,
  40. 'idMedia': video_id,
  41. })
  42. def get_meta(name):
  43. el = find_xpath_attr(metadata, './/Meta', 'name', name)
  44. return el.text if el is not None else None
  45. if get_meta('protectionType'):
  46. raise ExtractorError('This video is DRM protected.', expected=True)
  47. device_types = ['ipad']
  48. if app_code != 'toutv':
  49. device_types.append('flash')
  50. if not smuggled_data:
  51. device_types.append('android')
  52. formats = []
  53. # TODO: extract f4m formats
  54. # f4m formats can be extracted using flashhd device_type but they produce unplayable file
  55. for device_type in device_types:
  56. validation_url = 'http://api.radio-canada.ca/validationMedia/v1/Validation.ashx'
  57. query = {
  58. 'appCode': app_code,
  59. 'idMedia': video_id,
  60. 'connectionType': 'broadband',
  61. 'multibitrate': 'true',
  62. 'deviceType': device_type,
  63. }
  64. if smuggled_data:
  65. validation_url = 'https://services.radio-canada.ca/media/validation/v2/'
  66. query.update(smuggled_data)
  67. else:
  68. query.update({
  69. # paysJ391wsHjbOJwvCs26toz and bypasslock are used to bypass geo-restriction
  70. 'paysJ391wsHjbOJwvCs26toz': 'CA',
  71. 'bypasslock': 'NZt5K62gRqfc',
  72. })
  73. v_data = self._download_xml(validation_url, video_id, note='Downloading %s XML' % device_type, query=query, fatal=False)
  74. v_url = xpath_text(v_data, 'url')
  75. if not v_url:
  76. continue
  77. if v_url == 'null':
  78. raise ExtractorError('%s said: %s' % (
  79. self.IE_NAME, xpath_text(v_data, 'message')), expected=True)
  80. ext = determine_ext(v_url)
  81. if ext == 'm3u8':
  82. formats.extend(self._extract_m3u8_formats(
  83. v_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
  84. elif ext == 'f4m':
  85. formats.extend(self._extract_f4m_formats(
  86. v_url, video_id, f4m_id='hds', fatal=False))
  87. else:
  88. ext = determine_ext(v_url)
  89. bitrates = xpath_element(v_data, 'bitrates')
  90. for url_e in bitrates.findall('url'):
  91. tbr = int_or_none(url_e.get('bitrate'))
  92. if not tbr:
  93. continue
  94. f_url = re.sub(r'\d+\.%s' % ext, '%d.%s' % (tbr, ext), v_url)
  95. protocol = determine_protocol({'url': f_url})
  96. formats.append({
  97. 'format_id': '%s-%d' % (protocol, tbr),
  98. 'url': f_url,
  99. 'ext': 'flv' if protocol == 'rtmp' else ext,
  100. 'protocol': protocol,
  101. 'width': int_or_none(url_e.get('width')),
  102. 'height': int_or_none(url_e.get('height')),
  103. 'tbr': tbr,
  104. })
  105. if protocol == 'rtsp':
  106. base_url = self._search_regex(
  107. r'rtsp://([^?]+)', f_url, 'base url', default=None)
  108. if base_url:
  109. base_url = 'http://' + base_url
  110. formats.extend(self._extract_m3u8_formats(
  111. base_url + '/playlist.m3u8', video_id, 'mp4',
  112. 'm3u8_native', m3u8_id='hls', fatal=False))
  113. formats.extend(self._extract_f4m_formats(
  114. base_url + '/manifest.f4m', video_id,
  115. f4m_id='hds', fatal=False))
  116. self._sort_formats(formats)
  117. return {
  118. 'id': video_id,
  119. 'title': get_meta('Title'),
  120. 'description': get_meta('Description') or get_meta('ShortDescription'),
  121. 'thumbnail': get_meta('imageHR') or get_meta('imageMR') or get_meta('imageBR'),
  122. 'duration': int_or_none(get_meta('length')),
  123. 'series': get_meta('Emission'),
  124. 'season_number': int_or_none('SrcSaison'),
  125. 'episode_number': int_or_none('SrcEpisode'),
  126. 'upload_date': unified_strdate(get_meta('Date')),
  127. 'formats': formats,
  128. }
  129. class RadioCanadaAudioVideoIE(InfoExtractor):
  130. 'radiocanada:audiovideo'
  131. _VALID_URL = r'https?://ici\.radio-canada\.ca/audio-video/media-(?P<id>[0-9]+)'
  132. _TEST = {
  133. 'url': 'http://ici.radio-canada.ca/audio-video/media-7527184/barack-obama-au-vietnam',
  134. 'info_dict': {
  135. 'id': '7527184',
  136. 'ext': 'mp4',
  137. 'title': 'Barack Obama au Vietnam',
  138. 'description': 'Les États-Unis lèvent l\'embargo sur la vente d\'armes qui datait de la guerre du Vietnam',
  139. 'upload_date': '20160523',
  140. },
  141. 'params': {
  142. # m3u8 download
  143. 'skip_download': True,
  144. },
  145. }
  146. def _real_extract(self, url):
  147. return self.url_result('radiocanada:medianet:%s' % self._match_id(url))