srgssr.py 9.7 KB

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