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.

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