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.

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