radiocanada.py 8.2 KB

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