rts.py 8.9 KB

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