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.

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