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.

176 lines
7.1 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 not smuggled_data:
  49. device_types.append('flash')
  50. device_types.append('android')
  51. formats = []
  52. # TODO: extract f4m formats
  53. # f4m formats can be extracted using flashhd device_type but they produce unplayable file
  54. for device_type in device_types:
  55. validation_url = 'http://api.radio-canada.ca/validationMedia/v1/Validation.ashx'
  56. query = {
  57. 'appCode': app_code,
  58. 'idMedia': video_id,
  59. 'connectionType': 'broadband',
  60. 'multibitrate': 'true',
  61. 'deviceType': device_type,
  62. }
  63. if smuggled_data:
  64. validation_url = 'https://services.radio-canada.ca/media/validation/v2/'
  65. query.update(smuggled_data)
  66. else:
  67. query.update({
  68. # paysJ391wsHjbOJwvCs26toz and bypasslock are used to bypass geo-restriction
  69. 'paysJ391wsHjbOJwvCs26toz': 'CA',
  70. 'bypasslock': 'NZt5K62gRqfc',
  71. })
  72. v_data = self._download_xml(validation_url, video_id, note='Downloading %s XML' % device_type, query=query, fatal=False)
  73. v_url = xpath_text(v_data, 'url')
  74. if not v_url:
  75. continue
  76. if v_url == 'null':
  77. raise ExtractorError('%s said: %s' % (
  78. self.IE_NAME, xpath_text(v_data, 'message')), expected=True)
  79. ext = determine_ext(v_url)
  80. if ext == 'm3u8':
  81. formats.extend(self._extract_m3u8_formats(
  82. v_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
  83. elif ext == 'f4m':
  84. formats.extend(self._extract_f4m_formats(
  85. v_url, video_id, f4m_id='hds', fatal=False))
  86. else:
  87. ext = determine_ext(v_url)
  88. bitrates = xpath_element(v_data, 'bitrates')
  89. for url_e in bitrates.findall('url'):
  90. tbr = int_or_none(url_e.get('bitrate'))
  91. if not tbr:
  92. continue
  93. f_url = re.sub(r'\d+\.%s' % ext, '%d.%s' % (tbr, ext), v_url)
  94. protocol = determine_protocol({'url': f_url})
  95. f = {
  96. 'format_id': '%s-%d' % (protocol, tbr),
  97. 'url': f_url,
  98. 'ext': 'flv' if protocol == 'rtmp' else ext,
  99. 'protocol': protocol,
  100. 'width': int_or_none(url_e.get('width')),
  101. 'height': int_or_none(url_e.get('height')),
  102. 'tbr': tbr,
  103. }
  104. mobj = re.match(r'(?P<url>rtmp://[^/]+/[^/]+)/(?P<playpath>[^?]+)(?P<auth>\?.+)', f_url)
  105. if mobj:
  106. f.update({
  107. 'url': mobj.group('url') + mobj.group('auth'),
  108. 'play_path': mobj.group('playpath'),
  109. })
  110. formats.append(f)
  111. if protocol == 'rtsp':
  112. base_url = self._search_regex(
  113. r'rtsp://([^?]+)', f_url, 'base url', default=None)
  114. if base_url:
  115. base_url = 'http://' + base_url
  116. formats.extend(self._extract_m3u8_formats(
  117. base_url + '/playlist.m3u8', video_id, 'mp4',
  118. 'm3u8_native', m3u8_id='hls', fatal=False))
  119. formats.extend(self._extract_f4m_formats(
  120. base_url + '/manifest.f4m', video_id,
  121. f4m_id='hds', fatal=False))
  122. self._sort_formats(formats)
  123. subtitles = {}
  124. closed_caption_url = get_meta('closedCaption') or get_meta('closedCaptionHTML5')
  125. if closed_caption_url:
  126. subtitles['fr'] = [{
  127. 'url': closed_caption_url,
  128. 'ext': determine_ext(closed_caption_url, 'vtt'),
  129. }]
  130. return {
  131. 'id': video_id,
  132. 'title': get_meta('Title'),
  133. 'description': get_meta('Description') or get_meta('ShortDescription'),
  134. 'thumbnail': get_meta('imageHR') or get_meta('imageMR') or get_meta('imageBR'),
  135. 'duration': int_or_none(get_meta('length')),
  136. 'series': get_meta('Emission'),
  137. 'season_number': int_or_none('SrcSaison'),
  138. 'episode_number': int_or_none('SrcEpisode'),
  139. 'upload_date': unified_strdate(get_meta('Date')),
  140. 'subtitles': subtitles,
  141. 'formats': formats,
  142. }
  143. class RadioCanadaAudioVideoIE(InfoExtractor):
  144. 'radiocanada:audiovideo'
  145. _VALID_URL = r'https?://ici\.radio-canada\.ca/audio-video/media-(?P<id>[0-9]+)'
  146. _TEST = {
  147. 'url': 'http://ici.radio-canada.ca/audio-video/media-7527184/barack-obama-au-vietnam',
  148. 'info_dict': {
  149. 'id': '7527184',
  150. 'ext': 'mp4',
  151. 'title': 'Barack Obama au Vietnam',
  152. 'description': 'Les États-Unis lèvent l\'embargo sur la vente d\'armes qui datait de la guerre du Vietnam',
  153. 'upload_date': '20160523',
  154. },
  155. 'params': {
  156. # m3u8 download
  157. 'skip_download': True,
  158. },
  159. }
  160. def _real_extract(self, url):
  161. return self.url_result('radiocanada:medianet:%s' % self._match_id(url))