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.

161 lines
5.4 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. float_or_none,
  8. int_or_none,
  9. strip_or_none,
  10. )
  11. class RTBFIE(InfoExtractor):
  12. _VALID_URL = r'''(?x)
  13. https?://(?:www\.)?rtbf\.be/
  14. (?:
  15. video/[^?]+\?.*\bid=|
  16. ouftivi/(?:[^/]+/)*[^?]+\?.*\bvideoId=|
  17. auvio/[^/]+\?.*\b(?P<live>l)?id=
  18. )(?P<id>\d+)'''
  19. _TESTS = [{
  20. 'url': 'https://www.rtbf.be/video/detail_les-diables-au-coeur-episode-2?id=1921274',
  21. 'md5': '8c876a1cceeb6cf31b476461ade72384',
  22. 'info_dict': {
  23. 'id': '1921274',
  24. 'ext': 'mp4',
  25. 'title': 'Les Diables au coeur (épisode 2)',
  26. 'description': '(du 25/04/2014)',
  27. 'duration': 3099.54,
  28. 'upload_date': '20140425',
  29. 'timestamp': 1398456300,
  30. }
  31. }, {
  32. # geo restricted
  33. 'url': 'http://www.rtbf.be/ouftivi/heros/detail_scooby-doo-mysteres-associes?id=1097&videoId=2057442',
  34. 'only_matching': True,
  35. }, {
  36. 'url': 'http://www.rtbf.be/ouftivi/niouzz?videoId=2055858',
  37. 'only_matching': True,
  38. }, {
  39. 'url': 'http://www.rtbf.be/auvio/detail_jeudi-en-prime-siegfried-bracke?id=2102996',
  40. 'only_matching': True,
  41. }, {
  42. # Live
  43. 'url': 'https://www.rtbf.be/auvio/direct_pure-fm?lid=134775',
  44. 'only_matching': True,
  45. }, {
  46. # Audio
  47. 'url': 'https://www.rtbf.be/auvio/detail_cinq-heures-cinema?id=2360811',
  48. 'only_matching': True,
  49. }, {
  50. # With Subtitle
  51. 'url': 'https://www.rtbf.be/auvio/detail_les-carnets-du-bourlingueur?id=2361588',
  52. 'only_matching': True,
  53. }]
  54. _IMAGE_HOST = 'http://ds1.ds.static.rtbf.be'
  55. _PROVIDERS = {
  56. 'YOUTUBE': 'Youtube',
  57. 'DAILYMOTION': 'Dailymotion',
  58. 'VIMEO': 'Vimeo',
  59. }
  60. _QUALITIES = [
  61. ('mobile', 'SD'),
  62. ('web', 'MD'),
  63. ('high', 'HD'),
  64. ]
  65. def _real_extract(self, url):
  66. live, media_id = re.match(self._VALID_URL, url).groups()
  67. embed_page = self._download_webpage(
  68. 'https://www.rtbf.be/auvio/embed/' + ('direct' if live else 'media'),
  69. media_id, query={'id': media_id})
  70. data = self._parse_json(self._html_search_regex(
  71. r'data-media="([^"]+)"', embed_page, 'media data'), media_id)
  72. error = data.get('error')
  73. if error:
  74. raise ExtractorError('%s said: %s' % (self.IE_NAME, error), expected=True)
  75. provider = data.get('provider')
  76. if provider in self._PROVIDERS:
  77. return self.url_result(data['url'], self._PROVIDERS[provider])
  78. title = data['title']
  79. is_live = data.get('isLive')
  80. if is_live:
  81. title = self._live_title(title)
  82. height_re = r'-(\d+)p\.'
  83. formats = []
  84. m3u8_url = data.get('urlHlsAes128') or data.get('urlHls')
  85. if m3u8_url:
  86. formats.extend(self._extract_m3u8_formats(
  87. m3u8_url, media_id, 'mp4', m3u8_id='hls', fatal=False))
  88. fix_url = lambda x: x.replace('//rtbf-vod.', '//rtbf.') if '/geo/drm/' in x else x
  89. http_url = data.get('url')
  90. if formats and http_url and re.search(height_re, http_url):
  91. http_url = fix_url(http_url)
  92. for m3u8_f in formats[:]:
  93. height = m3u8_f.get('height')
  94. if not height:
  95. continue
  96. f = m3u8_f.copy()
  97. del f['protocol']
  98. f.update({
  99. 'format_id': m3u8_f['format_id'].replace('hls-', 'http-'),
  100. 'url': re.sub(height_re, '-%dp.' % height, http_url),
  101. })
  102. formats.append(f)
  103. else:
  104. sources = data.get('sources') or {}
  105. for key, format_id in self._QUALITIES:
  106. format_url = sources.get(key)
  107. if not format_url:
  108. continue
  109. height = int_or_none(self._search_regex(
  110. height_re, format_url, 'height', default=None))
  111. formats.append({
  112. 'format_id': format_id,
  113. 'url': fix_url(format_url),
  114. 'height': height,
  115. })
  116. mpd_url = data.get('urlDash')
  117. if not data.get('drm') and mpd_url:
  118. formats.extend(self._extract_mpd_formats(
  119. mpd_url, media_id, mpd_id='dash', fatal=False))
  120. audio_url = data.get('urlAudio')
  121. if audio_url:
  122. formats.append({
  123. 'format_id': 'audio',
  124. 'url': audio_url,
  125. 'vcodec': 'none',
  126. })
  127. self._sort_formats(formats)
  128. subtitles = {}
  129. for track in (data.get('tracks') or {}).values():
  130. sub_url = track.get('url')
  131. if not sub_url:
  132. continue
  133. subtitles.setdefault(track.get('lang') or 'fr', []).append({
  134. 'url': sub_url,
  135. })
  136. return {
  137. 'id': media_id,
  138. 'formats': formats,
  139. 'title': title,
  140. 'description': strip_or_none(data.get('description')),
  141. 'thumbnail': data.get('thumbnail'),
  142. 'duration': float_or_none(data.get('realDuration')),
  143. 'timestamp': int_or_none(data.get('liveFrom')),
  144. 'series': data.get('programLabel'),
  145. 'subtitles': subtitles,
  146. 'is_live': is_live,
  147. }