srgssr.py 7.2 KB

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