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.

130 lines
5.5 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. parse_iso8601,
  8. qualities,
  9. )
  10. class SRGSSRIE(InfoExtractor):
  11. _VALID_URL = r'(?:https?://tp\.srgssr\.ch/p(?:/[^/]+)+\?urn=)?urn:(?P<bu>srf|rts|rsi|rtr|swi):(?:[^:]+:)?(?P<type>video|audio):(?P<id>[0-9a-f\-]{36}|\d+)'
  12. _ERRORS = {
  13. 'AGERATING12': 'To protect children under the age of 12, this video is only available between 8 p.m. and 6 a.m.',
  14. 'AGERATING18': 'To protect children under the age of 18, this video is only available between 11 p.m. and 5 a.m.',
  15. # 'ENDDATE': 'For legal reasons, this video was only available for a specified period of time.',
  16. 'GEOBLOCK': 'For legal reasons, this video is only available in Switzerland.',
  17. 'LEGAL': 'The video cannot be transmitted for legal reasons.',
  18. 'STARTDATE': 'This video is not yet available. Please try again later.',
  19. }
  20. def _real_extract(self, url):
  21. bu, media_type, media_id = re.match(self._VALID_URL, url).groups()
  22. media_data = self._download_json(
  23. 'http://il.srgssr.ch/integrationlayer/1.0/ue/%s/%s/play/%s.json' % (bu, media_type, media_id),
  24. media_id)[media_type.capitalize()]
  25. if media_data.get('block') and media_data['block'] in self._ERRORS:
  26. raise ExtractorError(
  27. '%s said: %s' % (
  28. self.IE_NAME,
  29. self._ERRORS[media_data['block']]),
  30. expected=True)
  31. metadata = media_data['AssetMetadatas']['AssetMetadata'][0]
  32. title = metadata['title']
  33. description = metadata.get('description')
  34. created_date = media_data.get('createdDate') or metadata.get('createdDate')
  35. timestamp = parse_iso8601(created_date)
  36. thumbnails = []
  37. for image in media_data['Image']['ImageRepresentations']['ImageRepresentation']:
  38. thumbnails.append({
  39. 'id': image.get('id'),
  40. 'url': image['url'],
  41. })
  42. preference = qualities(['LQ', 'MQ', 'SD', 'HQ', 'HD'])
  43. formats = []
  44. for source in media_data['Playlists']['Playlist']:
  45. assets = {}
  46. protocol = source.get('@protocol')
  47. if protocol in ('HTTP-HDS', 'HTTP-HLS'):
  48. for quality in source['url']:
  49. assets[quality['@quality']] = quality['text']
  50. asset_url = assets.get('HD') or assets.get('HQ') or assets.get('SD') or assets.get('MQ') or assets.get('LQ')
  51. if '.f4m' in asset_url:
  52. formats.extend(self._extract_f4m_formats(asset_url + '?hdcore=3.4.0', media_id, f4m_id='hds'))
  53. elif '.m3u8' in asset_url:
  54. formats.extend(self._extract_m3u8_formats(asset_url, media_id, m3u8_id='hls'))
  55. else:
  56. for asset in source['url']:
  57. asset_url = asset['text']
  58. ext = None
  59. if asset_url.startswith('rtmp'):
  60. ext = self._search_regex(r'([a-z0-9]+):[^/]+', asset_url, 'ext')
  61. formats.append({
  62. 'url': asset_url,
  63. 'preference': preference(asset['@quality']),
  64. 'ext': ext,
  65. })
  66. downloads = media_data.get('Downloads')
  67. if downloads:
  68. for source in downloads['Download']:
  69. for asset in source['url']:
  70. formats.append({
  71. 'url': asset['text'],
  72. 'preference': preference(asset['@quality'])
  73. })
  74. self._sort_formats(formats)
  75. return {
  76. 'id': media_id,
  77. 'title': title,
  78. 'description': description,
  79. 'timestamp': timestamp,
  80. 'thumbnails': thumbnails,
  81. 'formats': formats,
  82. }
  83. class SRGSSRPlayIE(InfoExtractor):
  84. _VALID_URL = r'https?://(?:(?:www|play)\.)?(?P<bu>srf|rts|rsi|rtr|swi)\.ch/play/(?:tv|radio)/[^/]+/(?P<type>video|audio)/[^?]+\?id=(?P<id>[0-9a-f\-]{36}|\d+)'
  85. _TESTS = [{
  86. 'url': 'http://www.srf.ch/play/tv/10vor10/video/snowden-beantragt-asyl-in-russland?id=28e1a57d-5b76-4399-8ab3-9097f071e6c5',
  87. 'md5': '4cd93523723beff51bb4bee974ee238d',
  88. 'info_dict': {
  89. 'id': '28e1a57d-5b76-4399-8ab3-9097f071e6c5',
  90. 'ext': 'm4v',
  91. 'upload_date': '20130701',
  92. 'title': 'Snowden beantragt Asyl in Russland',
  93. 'timestamp': 1372713995,
  94. }
  95. }, {
  96. # No Speichern (Save) button
  97. 'url': 'http://www.srf.ch/play/tv/top-gear/video/jaguar-xk120-shadow-und-tornado-dampflokomotive?id=677f5829-e473-4823-ac83-a1087fe97faa',
  98. 'md5': '0a274ce38fda48c53c01890651985bc6',
  99. 'info_dict': {
  100. 'id': '677f5829-e473-4823-ac83-a1087fe97faa',
  101. 'ext': 'flv',
  102. 'upload_date': '20130710',
  103. 'title': 'Jaguar XK120, Shadow und Tornado-Dampflokomotive',
  104. 'description': 'md5:88604432b60d5a38787f152dec89cd56',
  105. 'timestamp': 1373493600,
  106. },
  107. }, {
  108. 'url': 'http://www.srf.ch/player/tv/10vor10/video/snowden-beantragt-asyl-in-russland?id=28e1a57d-5b76-4399-8ab3-9097f071e6c5',
  109. 'only_matching': True,
  110. }]
  111. def _real_extract(self, url):
  112. bu, media_type, media_id = re.match(self._VALID_URL, url).groups()
  113. return self.url_result('urn:%s:%s:%s' % (bu, media_type, media_id), 'SRGSSR')