logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

rts.py (9574B)


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