srgssr.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. ExtractorError,
  4. float_or_none,
  5. int_or_none,
  6. join_nonempty,
  7. parse_iso8601,
  8. qualities,
  9. try_get,
  10. )
  11. class SRGSSRIE(InfoExtractor):
  12. _VALID_URL = r'''(?x)
  13. (?:
  14. https?://tp\.srgssr\.ch/p(?:/[^/]+)+\?urn=urn|
  15. srgssr
  16. ):
  17. (?P<bu>
  18. srf|rts|rsi|rtr|swi
  19. ):(?:[^:]+:)?
  20. (?P<type>
  21. video|audio
  22. ):
  23. (?P<id>
  24. [0-9a-f\-]{36}|\d+
  25. )
  26. '''
  27. _GEO_BYPASS = False
  28. _GEO_COUNTRIES = ['CH']
  29. _ERRORS = {
  30. 'AGERATING12': 'To protect children under the age of 12, this video is only available between 8 p.m. and 6 a.m.',
  31. 'AGERATING18': 'To protect children under the age of 18, this video is only available between 11 p.m. and 5 a.m.',
  32. # 'ENDDATE': 'For legal reasons, this video was only available for a specified period of time.',
  33. 'GEOBLOCK': 'For legal reasons, this video is only available in Switzerland.',
  34. 'LEGAL': 'The video cannot be transmitted for legal reasons.',
  35. 'STARTDATE': 'This video is not yet available. Please try again later.',
  36. }
  37. _DEFAULT_LANGUAGE_CODES = {
  38. 'srf': 'de',
  39. 'rts': 'fr',
  40. 'rsi': 'it',
  41. 'rtr': 'rm',
  42. 'swi': 'en',
  43. }
  44. def _get_tokenized_src(self, url, video_id, format_id):
  45. token = self._download_json(
  46. 'http://tp.srgssr.ch/akahd/token?acl=*',
  47. video_id, f'Downloading {format_id} token', fatal=False) or {}
  48. auth_params = try_get(token, lambda x: x['token']['authparams'])
  49. if auth_params:
  50. url += ('?' if '?' not in url else '&') + auth_params
  51. return url
  52. def _get_media_data(self, bu, media_type, media_id):
  53. query = {'onlyChapters': True} if media_type == 'video' else {}
  54. full_media_data = self._download_json(
  55. f'https://il.srgssr.ch/integrationlayer/2.0/{bu}/mediaComposition/{media_type}/{media_id}.json',
  56. media_id, query=query)['chapterList']
  57. try:
  58. media_data = next(
  59. x for x in full_media_data if x.get('id') == media_id)
  60. except StopIteration:
  61. raise ExtractorError('No media information found')
  62. block_reason = media_data.get('blockReason')
  63. if block_reason and block_reason in self._ERRORS:
  64. message = self._ERRORS[block_reason]
  65. if block_reason == 'GEOBLOCK':
  66. self.raise_geo_restricted(
  67. msg=message, countries=self._GEO_COUNTRIES)
  68. raise ExtractorError(
  69. f'{self.IE_NAME} said: {message}', expected=True)
  70. return media_data
  71. def _real_extract(self, url):
  72. bu, media_type, media_id = self._match_valid_url(url).groups()
  73. media_data = self._get_media_data(bu, media_type, media_id)
  74. title = media_data['title']
  75. formats = []
  76. subtitles = {}
  77. q = qualities(['SD', 'HD'])
  78. for source in (media_data.get('resourceList') or []):
  79. format_url = source.get('url')
  80. if not format_url:
  81. continue
  82. protocol = source.get('protocol')
  83. quality = source.get('quality')
  84. format_id = join_nonempty(protocol, source.get('encoding'), quality)
  85. if protocol in ('HDS', 'HLS'):
  86. if source.get('tokenType') == 'AKAMAI':
  87. format_url = self._get_tokenized_src(
  88. format_url, media_id, format_id)
  89. fmts, subs = self._extract_akamai_formats_and_subtitles(
  90. format_url, media_id)
  91. formats.extend(fmts)
  92. subtitles = self._merge_subtitles(subtitles, subs)
  93. elif protocol == 'HLS':
  94. m3u8_fmts, m3u8_subs = self._extract_m3u8_formats_and_subtitles(
  95. format_url, media_id, 'mp4', 'm3u8_native',
  96. m3u8_id=format_id, fatal=False)
  97. formats.extend(m3u8_fmts)
  98. subtitles = self._merge_subtitles(subtitles, m3u8_subs)
  99. elif protocol in ('HTTP', 'HTTPS'):
  100. formats.append({
  101. 'format_id': format_id,
  102. 'url': format_url,
  103. 'quality': q(quality),
  104. })
  105. # This is needed because for audio medias the podcast url is usually
  106. # always included, even if is only an audio segment and not the
  107. # whole episode.
  108. if int_or_none(media_data.get('position')) == 0:
  109. for p in ('S', 'H'):
  110. podcast_url = media_data.get(f'podcast{p}dUrl')
  111. if not podcast_url:
  112. continue
  113. quality = p + 'D'
  114. formats.append({
  115. 'format_id': 'PODCAST-' + quality,
  116. 'url': podcast_url,
  117. 'quality': q(quality),
  118. })
  119. if media_type == 'video':
  120. for sub in (media_data.get('subtitleList') or []):
  121. sub_url = sub.get('url')
  122. if not sub_url:
  123. continue
  124. lang = sub.get('locale') or self._DEFAULT_LANGUAGE_CODES[bu]
  125. subtitles.setdefault(lang, []).append({
  126. 'url': sub_url,
  127. })
  128. return {
  129. 'id': media_id,
  130. 'title': title,
  131. 'description': media_data.get('description'),
  132. 'timestamp': parse_iso8601(media_data.get('date')),
  133. 'thumbnail': media_data.get('imageUrl'),
  134. 'duration': float_or_none(media_data.get('duration'), 1000),
  135. 'subtitles': subtitles,
  136. 'formats': formats,
  137. }
  138. class SRGSSRPlayIE(InfoExtractor):
  139. IE_DESC = 'srf.ch, rts.ch, rsi.ch, rtr.ch and swissinfo.ch play sites'
  140. _VALID_URL = r'''(?x)
  141. https?://
  142. (?:(?:www|play)\.)?
  143. (?P<bu>srf|rts|rsi|rtr|swissinfo)\.ch/play/(?:tv|radio)/
  144. (?:
  145. [^/]+/(?P<type>video|audio)/[^?]+|
  146. popup(?P<type_2>video|audio)player
  147. )
  148. \?.*?\b(?:id=|urn=urn:[^:]+:video:)(?P<id>[0-9a-f\-]{36}|\d+)
  149. '''
  150. _TESTS = [{
  151. 'url': 'http://www.srf.ch/play/tv/10vor10/video/snowden-beantragt-asyl-in-russland?id=28e1a57d-5b76-4399-8ab3-9097f071e6c5',
  152. 'md5': '6db2226ba97f62ad42ce09783680046c',
  153. 'info_dict': {
  154. 'id': '28e1a57d-5b76-4399-8ab3-9097f071e6c5',
  155. 'ext': 'mp4',
  156. 'upload_date': '20130701',
  157. 'title': 'Snowden beantragt Asyl in Russland',
  158. 'timestamp': 1372708215,
  159. 'duration': 113.827,
  160. 'thumbnail': r're:^https?://.*1383719781\.png$',
  161. },
  162. 'expected_warnings': ['Unable to download f4m manifest'],
  163. }, {
  164. 'url': 'http://www.rtr.ch/play/radio/actualitad/audio/saira-tujetsch-tuttina-cuntinuar-cun-sedrun-muster-turissem?id=63cb0778-27f8-49af-9284-8c7a8c6d15fc',
  165. 'info_dict': {
  166. 'id': '63cb0778-27f8-49af-9284-8c7a8c6d15fc',
  167. 'ext': 'mp3',
  168. 'upload_date': '20151013',
  169. 'title': 'Saira: Tujetsch - tuttina cuntinuar cun Sedrun Mustér Turissem',
  170. 'timestamp': 1444709160,
  171. 'duration': 336.816,
  172. },
  173. 'params': {
  174. # rtmp download
  175. 'skip_download': True,
  176. },
  177. }, {
  178. 'url': 'http://www.rts.ch/play/tv/-/video/le-19h30?id=6348260',
  179. 'md5': '67a2a9ae4e8e62a68d0e9820cc9782df',
  180. 'info_dict': {
  181. 'id': '6348260',
  182. 'display_id': '6348260',
  183. 'ext': 'mp4',
  184. 'duration': 1796.76,
  185. 'title': 'Le 19h30',
  186. 'upload_date': '20141201',
  187. 'timestamp': 1417458600,
  188. 'thumbnail': r're:^https?://.*\.image',
  189. },
  190. 'params': {
  191. # m3u8 download
  192. 'skip_download': True,
  193. },
  194. }, {
  195. 'url': 'http://play.swissinfo.ch/play/tv/business/video/why-people-were-against-tax-reforms?id=42960270',
  196. 'info_dict': {
  197. 'id': '42960270',
  198. 'ext': 'mp4',
  199. 'title': 'Why people were against tax reforms',
  200. 'description': 'md5:7ac442c558e9630e947427469c4b824d',
  201. 'duration': 94.0,
  202. 'upload_date': '20170215',
  203. 'timestamp': 1487173560,
  204. 'thumbnail': r're:https?://www\.swissinfo\.ch/srgscalableimage/42961964',
  205. 'subtitles': 'count:9',
  206. },
  207. 'params': {
  208. 'skip_download': True,
  209. },
  210. }, {
  211. 'url': 'https://www.srf.ch/play/tv/popupvideoplayer?id=c4dba0ca-e75b-43b2-a34f-f708a4932e01',
  212. 'only_matching': True,
  213. }, {
  214. 'url': 'https://www.srf.ch/play/tv/10vor10/video/snowden-beantragt-asyl-in-russland?urn=urn:srf:video:28e1a57d-5b76-4399-8ab3-9097f071e6c5',
  215. 'only_matching': True,
  216. }, {
  217. 'url': 'https://www.rts.ch/play/tv/19h30/video/le-19h30?urn=urn:rts:video:6348260',
  218. 'only_matching': True,
  219. }, {
  220. # audio segment, has podcastSdUrl of the full episode
  221. 'url': 'https://www.srf.ch/play/radio/popupaudioplayer?id=50b20dc8-f05b-4972-bf03-e438ff2833eb',
  222. 'only_matching': True,
  223. }]
  224. def _real_extract(self, url):
  225. mobj = self._match_valid_url(url)
  226. bu = mobj.group('bu')
  227. media_type = mobj.group('type') or mobj.group('type_2')
  228. media_id = mobj.group('id')
  229. return self.url_result(f'srgssr:{bu[:3]}:{media_type}:{media_id}', 'SRGSSR')