rts.py 9.3 KB

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