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.

168 lines
7.1 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_urllib_parse_urlparse
  6. from ..utils import (
  7. ExtractorError,
  8. parse_iso8601,
  9. qualities,
  10. )
  11. class SRGSSRIE(InfoExtractor):
  12. _VALID_URL = r'(?:https?://tp\.srgssr\.ch/p(?:/[^/]+)+\?urn=urn|srgssr):(?P<bu>srf|rts|rsi|rtr|swi):(?:[^:]+:)?(?P<type>video|audio):(?P<id>[0-9a-f\-]{36}|\d+)'
  13. _ERRORS = {
  14. 'AGERATING12': 'To protect children under the age of 12, this video is only available between 8 p.m. and 6 a.m.',
  15. 'AGERATING18': 'To protect children under the age of 18, this video is only available between 11 p.m. and 5 a.m.',
  16. # 'ENDDATE': 'For legal reasons, this video was only available for a specified period of time.',
  17. 'GEOBLOCK': 'For legal reasons, this video is only available in Switzerland.',
  18. 'LEGAL': 'The video cannot be transmitted for legal reasons.',
  19. 'STARTDATE': 'This video is not yet available. Please try again later.',
  20. }
  21. def _get_tokenized_src(self, url, video_id, format_id):
  22. sp = compat_urllib_parse_urlparse(url).path.split('/')
  23. token = self._download_json(
  24. 'http://tp.srgssr.ch/akahd/token?acl=/%s/%s/*' % (sp[1], sp[2]),
  25. video_id, 'Downloading %s token' % format_id, fatal=False) or {}
  26. auth_params = token.get('token', {}).get('authparams')
  27. if auth_params:
  28. url += '?' + auth_params
  29. return url
  30. def get_media_data(self, bu, media_type, media_id):
  31. media_data = self._download_json(
  32. 'http://il.srgssr.ch/integrationlayer/1.0/ue/%s/%s/play/%s.json' % (bu, media_type, media_id),
  33. media_id)[media_type.capitalize()]
  34. if media_data.get('block') and media_data['block'] in self._ERRORS:
  35. raise ExtractorError('%s said: %s' % (
  36. self.IE_NAME, self._ERRORS[media_data['block']]), expected=True)
  37. return media_data
  38. def _real_extract(self, url):
  39. bu, media_type, media_id = re.match(self._VALID_URL, url).groups()
  40. if bu == 'rts':
  41. return self.url_result('rts:%s' % media_id, 'RTS')
  42. media_data = self.get_media_data(bu, media_type, media_id)
  43. metadata = media_data['AssetMetadatas']['AssetMetadata'][0]
  44. title = metadata['title']
  45. description = metadata.get('description')
  46. created_date = media_data.get('createdDate') or metadata.get('createdDate')
  47. timestamp = parse_iso8601(created_date)
  48. thumbnails = [{
  49. 'id': image.get('id'),
  50. 'url': image['url'],
  51. } for image in media_data.get('Image', {}).get('ImageRepresentations', {}).get('ImageRepresentation', [])]
  52. preference = qualities(['LQ', 'MQ', 'SD', 'HQ', 'HD'])
  53. formats = []
  54. for source in media_data.get('Playlists', {}).get('Playlist', []) + media_data.get('Downloads', {}).get('Download', []):
  55. protocol = source.get('@protocol')
  56. for asset in source['url']:
  57. asset_url = asset['text']
  58. quality = asset['@quality']
  59. format_id = '%s-%s' % (protocol, quality)
  60. if protocol.startswith('HTTP-HDS') or protocol.startswith('HTTP-HLS'):
  61. asset_url = self._get_tokenized_src(asset_url, media_id, format_id)
  62. if protocol.startswith('HTTP-HDS'):
  63. formats.extend(self._extract_f4m_formats(
  64. asset_url + ('?' if '?' not in asset_url else '&') + 'hdcore=3.4.0',
  65. media_id, f4m_id=format_id, fatal=False))
  66. elif protocol.startswith('HTTP-HLS'):
  67. formats.extend(self._extract_m3u8_formats(
  68. asset_url, media_id, 'mp4', 'm3u8_native',
  69. m3u8_id=format_id, fatal=False))
  70. else:
  71. formats.append({
  72. 'format_id': format_id,
  73. 'url': asset_url,
  74. 'preference': preference(quality),
  75. 'ext': 'flv' if protocol == 'RTMP' else None,
  76. })
  77. self._sort_formats(formats)
  78. return {
  79. 'id': media_id,
  80. 'title': title,
  81. 'description': description,
  82. 'timestamp': timestamp,
  83. 'thumbnails': thumbnails,
  84. 'formats': formats,
  85. }
  86. class SRGSSRPlayIE(InfoExtractor):
  87. IE_DESC = 'srf.ch, rts.ch, rsi.ch, rtr.ch and swissinfo.ch play sites'
  88. _VALID_URL = r'https?://(?:(?:www|play)\.)?(?P<bu>srf|rts|rsi|rtr|swissinfo)\.ch/play/(?:tv|radio)/[^/]+/(?P<type>video|audio)/[^?]+\?id=(?P<id>[0-9a-f\-]{36}|\d+)'
  89. _TESTS = [{
  90. 'url': 'http://www.srf.ch/play/tv/10vor10/video/snowden-beantragt-asyl-in-russland?id=28e1a57d-5b76-4399-8ab3-9097f071e6c5',
  91. 'md5': 'da6b5b3ac9fa4761a942331cef20fcb3',
  92. 'info_dict': {
  93. 'id': '28e1a57d-5b76-4399-8ab3-9097f071e6c5',
  94. 'ext': 'mp4',
  95. 'upload_date': '20130701',
  96. 'title': 'Snowden beantragt Asyl in Russland',
  97. 'timestamp': 1372713995,
  98. }
  99. }, {
  100. # No Speichern (Save) button
  101. 'url': 'http://www.srf.ch/play/tv/top-gear/video/jaguar-xk120-shadow-und-tornado-dampflokomotive?id=677f5829-e473-4823-ac83-a1087fe97faa',
  102. 'md5': '0a274ce38fda48c53c01890651985bc6',
  103. 'info_dict': {
  104. 'id': '677f5829-e473-4823-ac83-a1087fe97faa',
  105. 'ext': 'flv',
  106. 'upload_date': '20130710',
  107. 'title': 'Jaguar XK120, Shadow und Tornado-Dampflokomotive',
  108. 'description': 'md5:88604432b60d5a38787f152dec89cd56',
  109. 'timestamp': 1373493600,
  110. },
  111. }, {
  112. 'url': 'http://www.rtr.ch/play/radio/actualitad/audio/saira-tujetsch-tuttina-cuntinuar-cun-sedrun-muster-turissem?id=63cb0778-27f8-49af-9284-8c7a8c6d15fc',
  113. 'info_dict': {
  114. 'id': '63cb0778-27f8-49af-9284-8c7a8c6d15fc',
  115. 'ext': 'mp3',
  116. 'upload_date': '20151013',
  117. 'title': 'Saira: Tujetsch - tuttina cuntinuar cun Sedrun Mustér Turissem',
  118. 'timestamp': 1444750398,
  119. },
  120. 'params': {
  121. # rtmp download
  122. 'skip_download': True,
  123. },
  124. }, {
  125. 'url': 'http://www.rts.ch/play/tv/-/video/le-19h30?id=6348260',
  126. 'md5': '67a2a9ae4e8e62a68d0e9820cc9782df',
  127. 'info_dict': {
  128. 'id': '6348260',
  129. 'display_id': '6348260',
  130. 'ext': 'mp4',
  131. 'duration': 1796,
  132. 'title': 'Le 19h30',
  133. 'description': '',
  134. 'uploader': '19h30',
  135. 'upload_date': '20141201',
  136. 'timestamp': 1417458600,
  137. 'thumbnail': r're:^https?://.*\.image',
  138. 'view_count': int,
  139. },
  140. 'params': {
  141. # m3u8 download
  142. 'skip_download': True,
  143. }
  144. }]
  145. def _real_extract(self, url):
  146. bu, media_type, media_id = re.match(self._VALID_URL, url).groups()
  147. # other info can be extracted from url + '&layout=json'
  148. return self.url_result('srgssr:%s:%s:%s' % (bu[:3], media_type, media_id), 'SRGSSR')