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.

229 lines
9.1 KiB

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