radiocanada.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. error = None
  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. error = xpath_text(v_data, 'message')
  79. continue
  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. f = {
  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. mobj = re.match(r'(?P<url>rtmp://[^/]+/[^/]+)/(?P<playpath>[^?]+)(?P<auth>\?.+)', f_url)
  106. if mobj:
  107. f.update({
  108. 'url': mobj.group('url') + mobj.group('auth'),
  109. 'play_path': mobj.group('playpath'),
  110. })
  111. formats.append(f)
  112. if protocol == 'rtsp':
  113. base_url = self._search_regex(
  114. r'rtsp://([^?]+)', f_url, 'base url', default=None)
  115. if base_url:
  116. base_url = 'http://' + base_url
  117. formats.extend(self._extract_m3u8_formats(
  118. base_url + '/playlist.m3u8', video_id, 'mp4',
  119. 'm3u8_native', m3u8_id='hls', fatal=False))
  120. formats.extend(self._extract_f4m_formats(
  121. base_url + '/manifest.f4m', video_id,
  122. f4m_id='hds', fatal=False))
  123. if not formats and error:
  124. raise ExtractorError(
  125. '%s said: %s' % (self.IE_NAME, error), expected=True)
  126. self._sort_formats(formats)
  127. subtitles = {}
  128. closed_caption_url = get_meta('closedCaption') or get_meta('closedCaptionHTML5')
  129. if closed_caption_url:
  130. subtitles['fr'] = [{
  131. 'url': closed_caption_url,
  132. 'ext': determine_ext(closed_caption_url, 'vtt'),
  133. }]
  134. return {
  135. 'id': video_id,
  136. 'title': get_meta('Title'),
  137. 'description': get_meta('Description') or get_meta('ShortDescription'),
  138. 'thumbnail': get_meta('imageHR') or get_meta('imageMR') or get_meta('imageBR'),
  139. 'duration': int_or_none(get_meta('length')),
  140. 'series': get_meta('Emission'),
  141. 'season_number': int_or_none('SrcSaison'),
  142. 'episode_number': int_or_none('SrcEpisode'),
  143. 'upload_date': unified_strdate(get_meta('Date')),
  144. 'subtitles': subtitles,
  145. 'formats': formats,
  146. }
  147. class RadioCanadaAudioVideoIE(InfoExtractor):
  148. 'radiocanada:audiovideo'
  149. _VALID_URL = r'https?://ici\.radio-canada\.ca/audio-video/media-(?P<id>[0-9]+)'
  150. _TEST = {
  151. 'url': 'http://ici.radio-canada.ca/audio-video/media-7527184/barack-obama-au-vietnam',
  152. 'info_dict': {
  153. 'id': '7527184',
  154. 'ext': 'mp4',
  155. 'title': 'Barack Obama au Vietnam',
  156. 'description': 'Les États-Unis lèvent l\'embargo sur la vente d\'armes qui datait de la guerre du Vietnam',
  157. 'upload_date': '20160523',
  158. },
  159. 'params': {
  160. # m3u8 download
  161. 'skip_download': True,
  162. },
  163. }
  164. def _real_extract(self, url):
  165. return self.url_result('radiocanada:medianet:%s' % self._match_id(url))