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.

234 lines
9.4 KiB

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