logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

srgssr.py (9882B)


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