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.

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