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.

230 lines
8.9 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .srgssr import SRGSSRIE
  5. from ..compat import compat_str
  6. from ..utils import (
  7. int_or_none,
  8. parse_duration,
  9. parse_iso8601,
  10. unescapeHTML,
  11. determine_ext,
  12. )
  13. class RTSIE(SRGSSRIE):
  14. IE_DESC = 'RTS.ch'
  15. _VALID_URL = r'rts:(?P<rts_id>\d+)|https?://(?:.+?\.)?rts\.ch/(?:[^/]+/){2,}(?P<id>[0-9]+)-(?P<display_id>.+?)\.html'
  16. _TESTS = [
  17. {
  18. 'url': 'http://www.rts.ch/archives/tv/divers/3449373-les-enfants-terribles.html',
  19. 'md5': 'ff7f8450a90cf58dacb64e29707b4a8e',
  20. 'info_dict': {
  21. 'id': '3449373',
  22. 'display_id': 'les-enfants-terribles',
  23. 'ext': 'mp4',
  24. 'duration': 1488,
  25. 'title': 'Les Enfants Terribles',
  26. 'description': 'France Pommier et sa soeur Luce Feral, les deux filles de ce groupe de 5.',
  27. 'uploader': 'Divers',
  28. 'upload_date': '19680921',
  29. 'timestamp': -40280400,
  30. 'thumbnail': r're:^https?://.*\.image',
  31. 'view_count': int,
  32. },
  33. },
  34. {
  35. 'url': 'http://www.rts.ch/emissions/passe-moi-les-jumelles/5624067-entre-ciel-et-mer.html',
  36. 'info_dict': {
  37. 'id': '5624065',
  38. 'title': 'Passe-moi les jumelles',
  39. },
  40. 'playlist_mincount': 4,
  41. },
  42. {
  43. 'url': 'http://www.rts.ch/video/sport/hockey/5745975-1-2-kloten-fribourg-5-2-second-but-pour-gotteron-par-kwiatowski.html',
  44. 'info_dict': {
  45. 'id': '5745975',
  46. 'display_id': '1-2-kloten-fribourg-5-2-second-but-pour-gotteron-par-kwiatowski',
  47. 'ext': 'mp4',
  48. 'duration': 48,
  49. 'title': '1/2, Kloten - Fribourg (5-2): second but pour Gottéron par Kwiatowski',
  50. 'description': 'Hockey - Playoff',
  51. 'uploader': 'Hockey',
  52. 'upload_date': '20140403',
  53. 'timestamp': 1396556882,
  54. 'thumbnail': r're:^https?://.*\.image',
  55. 'view_count': int,
  56. },
  57. 'params': {
  58. # m3u8 download
  59. 'skip_download': True,
  60. },
  61. 'skip': 'Blocked outside Switzerland',
  62. },
  63. {
  64. 'url': 'http://www.rts.ch/video/info/journal-continu/5745356-londres-cachee-par-un-epais-smog.html',
  65. 'md5': '1bae984fe7b1f78e94abc74e802ed99f',
  66. 'info_dict': {
  67. 'id': '5745356',
  68. 'display_id': 'londres-cachee-par-un-epais-smog',
  69. 'ext': 'mp4',
  70. 'duration': 33,
  71. 'title': 'Londres cachée par un épais smog',
  72. 'description': 'Un important voile de smog recouvre Londres depuis mercredi, provoqué par la pollution et du sable du Sahara.',
  73. 'uploader': 'L\'actu en vidéo',
  74. 'upload_date': '20140403',
  75. 'timestamp': 1396537322,
  76. 'thumbnail': r're:^https?://.*\.image',
  77. 'view_count': int,
  78. },
  79. },
  80. {
  81. 'url': 'http://www.rts.ch/audio/couleur3/programmes/la-belle-video-de-stephane-laurenceau/5706148-urban-hippie-de-damien-krisl-03-04-2014.html',
  82. 'md5': 'dd8ef6a22dff163d063e2a52bc8adcae',
  83. 'info_dict': {
  84. 'id': '5706148',
  85. 'display_id': 'urban-hippie-de-damien-krisl-03-04-2014',
  86. 'ext': 'mp3',
  87. 'duration': 123,
  88. 'title': '"Urban Hippie", de Damien Krisl',
  89. 'description': 'Des Hippies super glam.',
  90. 'upload_date': '20140403',
  91. 'timestamp': 1396551600,
  92. },
  93. },
  94. {
  95. # article with videos on rhs
  96. 'url': 'http://www.rts.ch/sport/hockey/6693917-hockey-davos-decroche-son-31e-titre-de-champion-de-suisse.html',
  97. 'info_dict': {
  98. 'id': '6693917',
  99. 'title': 'Hockey: Davos décroche son 31e titre de champion de Suisse',
  100. },
  101. 'playlist_mincount': 5,
  102. },
  103. {
  104. 'url': 'http://pages.rts.ch/emissions/passe-moi-les-jumelles/5624065-entre-ciel-et-mer.html',
  105. 'only_matching': True,
  106. }
  107. ]
  108. def _real_extract(self, url):
  109. m = re.match(self._VALID_URL, url)
  110. media_id = m.group('rts_id') or m.group('id')
  111. display_id = m.group('display_id') or media_id
  112. def download_json(internal_id):
  113. return self._download_json(
  114. 'http://www.rts.ch/a/%s.html?f=json/article' % internal_id,
  115. display_id)
  116. all_info = download_json(media_id)
  117. # media_id extracted out of URL is not always a real id
  118. if 'video' not in all_info and 'audio' not in all_info:
  119. entries = []
  120. for item in all_info.get('items', []):
  121. item_url = item.get('url')
  122. if not item_url:
  123. continue
  124. entries.append(self.url_result(item_url, 'RTS'))
  125. if not entries:
  126. page, urlh = self._download_webpage_handle(url, display_id)
  127. if re.match(self._VALID_URL, urlh.geturl()).group('id') != media_id:
  128. return self.url_result(urlh.geturl(), 'RTS')
  129. # article with videos on rhs
  130. videos = re.findall(
  131. r'<article[^>]+class="content-item"[^>]*>\s*<a[^>]+data-video-urn="urn:([^"]+)"',
  132. page)
  133. if not videos:
  134. videos = re.findall(
  135. r'(?s)<iframe[^>]+class="srg-player"[^>]+src="[^"]+urn:([^"]+)"',
  136. page)
  137. if videos:
  138. entries = [self.url_result('srgssr:%s' % video_urn, 'SRGSSR') for video_urn in videos]
  139. if entries:
  140. return self.playlist_result(entries, media_id, all_info.get('title'))
  141. internal_id = self._html_search_regex(
  142. r'<(?:video|audio) data-id="([0-9]+)"', page,
  143. 'internal video id')
  144. all_info = download_json(internal_id)
  145. media_type = 'video' if 'video' in all_info else 'audio'
  146. # check for errors
  147. self.get_media_data('rts', media_type, media_id)
  148. info = all_info['video']['JSONinfo'] if 'video' in all_info else all_info['audio']
  149. title = info['title']
  150. def extract_bitrate(url):
  151. return int_or_none(self._search_regex(
  152. r'-([0-9]+)k\.', url, 'bitrate', default=None))
  153. formats = []
  154. streams = info.get('streams', {})
  155. for format_id, format_url in streams.items():
  156. if format_id == 'hds_sd' and 'hds' in streams:
  157. continue
  158. if format_id == 'hls_sd' and 'hls' in streams:
  159. continue
  160. ext = determine_ext(format_url)
  161. if ext in ('m3u8', 'f4m'):
  162. format_url = self._get_tokenized_src(format_url, media_id, format_id)
  163. if ext == 'f4m':
  164. formats.extend(self._extract_f4m_formats(
  165. format_url + ('?' if '?' not in format_url else '&') + 'hdcore=3.4.0',
  166. media_id, f4m_id=format_id, fatal=False))
  167. else:
  168. formats.extend(self._extract_m3u8_formats(
  169. format_url, media_id, 'mp4', 'm3u8_native', m3u8_id=format_id, fatal=False))
  170. else:
  171. formats.append({
  172. 'format_id': format_id,
  173. 'url': format_url,
  174. 'tbr': extract_bitrate(format_url),
  175. })
  176. for media in info.get('media', []):
  177. media_url = media.get('url')
  178. if not media_url or re.match(r'https?://', media_url):
  179. continue
  180. rate = media.get('rate')
  181. ext = media.get('ext') or determine_ext(media_url, 'mp4')
  182. format_id = ext
  183. if rate:
  184. format_id += '-%dk' % rate
  185. formats.append({
  186. 'format_id': format_id,
  187. 'url': 'http://download-video.rts.ch/' + media_url,
  188. 'tbr': rate or extract_bitrate(media_url),
  189. })
  190. self._check_formats(formats, media_id)
  191. self._sort_formats(formats)
  192. duration = info.get('duration') or info.get('cutout') or info.get('cutduration')
  193. if isinstance(duration, compat_str):
  194. duration = parse_duration(duration)
  195. return {
  196. 'id': media_id,
  197. 'display_id': display_id,
  198. 'formats': formats,
  199. 'title': title,
  200. 'description': info.get('intro'),
  201. 'duration': duration,
  202. 'view_count': int_or_none(info.get('plays')),
  203. 'uploader': info.get('programName'),
  204. 'timestamp': parse_iso8601(info.get('broadcast_date')),
  205. 'thumbnail': unescapeHTML(info.get('preview_image_url')),
  206. }