logo

youtube-dl

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

rtve.py (9548B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import base64
  4. import io
  5. import re
  6. import sys
  7. from .common import InfoExtractor
  8. from ..compat import (
  9. compat_b64decode,
  10. compat_struct_unpack,
  11. )
  12. from ..utils import (
  13. determine_ext,
  14. ExtractorError,
  15. float_or_none,
  16. qualities,
  17. remove_end,
  18. remove_start,
  19. std_headers,
  20. )
  21. _bytes_to_chr = (lambda x: x) if sys.version_info[0] == 2 else (lambda x: map(chr, x))
  22. class RTVEALaCartaIE(InfoExtractor):
  23. IE_NAME = 'rtve.es:alacarta'
  24. IE_DESC = 'RTVE a la carta'
  25. _VALID_URL = r'https?://(?:www\.)?rtve\.es/(m/)?(alacarta/videos|filmoteca)/[^/]+/[^/]+/(?P<id>\d+)'
  26. _TESTS = [{
  27. 'url': 'http://www.rtve.es/alacarta/videos/balonmano/o-swiss-cup-masculina-final-espana-suecia/2491869/',
  28. 'md5': '1d49b7e1ca7a7502c56a4bf1b60f1b43',
  29. 'info_dict': {
  30. 'id': '2491869',
  31. 'ext': 'mp4',
  32. 'title': 'Balonmano - Swiss Cup masculina. Final: España-Suecia',
  33. 'duration': 5024.566,
  34. 'series': 'Balonmano',
  35. },
  36. 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'],
  37. }, {
  38. 'note': 'Live stream',
  39. 'url': 'http://www.rtve.es/alacarta/videos/television/24h-live/1694255/',
  40. 'info_dict': {
  41. 'id': '1694255',
  42. 'ext': 'mp4',
  43. 'title': 're:^24H LIVE [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  44. 'is_live': True,
  45. },
  46. 'params': {
  47. 'skip_download': 'live stream',
  48. },
  49. }, {
  50. 'url': 'http://www.rtve.es/alacarta/videos/servir-y-proteger/servir-proteger-capitulo-104/4236788/',
  51. 'md5': 'd850f3c8731ea53952ebab489cf81cbf',
  52. 'info_dict': {
  53. 'id': '4236788',
  54. 'ext': 'mp4',
  55. 'title': 'Servir y proteger - Capítulo 104',
  56. 'duration': 3222.0,
  57. },
  58. 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'],
  59. }, {
  60. 'url': 'http://www.rtve.es/m/alacarta/videos/cuentame-como-paso/cuentame-como-paso-t16-ultimo-minuto-nuestra-vida-capitulo-276/2969138/?media=tve',
  61. 'only_matching': True,
  62. }, {
  63. 'url': 'http://www.rtve.es/filmoteca/no-do/not-1-introduccion-primer-noticiario-espanol/1465256/',
  64. 'only_matching': True,
  65. }]
  66. def _real_initialize(self):
  67. user_agent_b64 = base64.b64encode(std_headers['User-Agent'].encode('utf-8')).decode('utf-8')
  68. self._manager = self._download_json(
  69. 'http://www.rtve.es/odin/loki/' + user_agent_b64,
  70. None, 'Fetching manager info')['manager']
  71. @staticmethod
  72. def _decrypt_url(png):
  73. encrypted_data = io.BytesIO(compat_b64decode(png)[8:])
  74. while True:
  75. length = compat_struct_unpack('!I', encrypted_data.read(4))[0]
  76. chunk_type = encrypted_data.read(4)
  77. if chunk_type == b'IEND':
  78. break
  79. data = encrypted_data.read(length)
  80. if chunk_type == b'tEXt':
  81. alphabet_data, text = data.split(b'\0')
  82. quality, url_data = text.split(b'%%')
  83. alphabet = []
  84. e = 0
  85. d = 0
  86. for l in _bytes_to_chr(alphabet_data):
  87. if d == 0:
  88. alphabet.append(l)
  89. d = e = (e + 1) % 4
  90. else:
  91. d -= 1
  92. url = ''
  93. f = 0
  94. e = 3
  95. b = 1
  96. for letter in _bytes_to_chr(url_data):
  97. if f == 0:
  98. l = int(letter) * 10
  99. f = 1
  100. else:
  101. if e == 0:
  102. l += int(letter)
  103. url += alphabet[l]
  104. e = (b + 3) % 4
  105. f = 0
  106. b += 1
  107. else:
  108. e -= 1
  109. yield quality.decode(), url
  110. encrypted_data.read(4) # CRC
  111. def _extract_png_formats(self, video_id):
  112. png = self._download_webpage(
  113. 'http://www.rtve.es/ztnr/movil/thumbnail/%s/videos/%s.png' % (self._manager, video_id),
  114. video_id, 'Downloading url information', query={'q': 'v2'})
  115. q = qualities(['Media', 'Alta', 'HQ', 'HD_READY', 'HD_FULL'])
  116. formats = []
  117. for quality, video_url in self._decrypt_url(png):
  118. ext = determine_ext(video_url)
  119. if ext == 'm3u8':
  120. formats.extend(self._extract_m3u8_formats(
  121. video_url, video_id, 'mp4', 'm3u8_native',
  122. m3u8_id='hls', fatal=False))
  123. elif ext == 'mpd':
  124. formats.extend(self._extract_mpd_formats(
  125. video_url, video_id, 'dash', fatal=False))
  126. else:
  127. formats.append({
  128. 'format_id': quality,
  129. 'quality': q(quality),
  130. 'url': video_url,
  131. })
  132. self._sort_formats(formats)
  133. return formats
  134. def _real_extract(self, url):
  135. video_id = self._match_id(url)
  136. info = self._download_json(
  137. 'http://www.rtve.es/api/videos/%s/config/alacarta_videos.json' % video_id,
  138. video_id)['page']['items'][0]
  139. if info['state'] == 'DESPU':
  140. raise ExtractorError('The video is no longer available', expected=True)
  141. title = info['title'].strip()
  142. formats = self._extract_png_formats(video_id)
  143. subtitles = None
  144. sbt_file = info.get('sbtFile')
  145. if sbt_file:
  146. subtitles = self.extract_subtitles(video_id, sbt_file)
  147. is_live = info.get('live') is True
  148. return {
  149. 'id': video_id,
  150. 'title': self._live_title(title) if is_live else title,
  151. 'formats': formats,
  152. 'thumbnail': info.get('image'),
  153. 'subtitles': subtitles,
  154. 'duration': float_or_none(info.get('duration'), 1000),
  155. 'is_live': is_live,
  156. 'series': info.get('programTitle'),
  157. }
  158. def _get_subtitles(self, video_id, sub_file):
  159. subs = self._download_json(
  160. sub_file + '.json', video_id,
  161. 'Downloading subtitles info')['page']['items']
  162. return dict(
  163. (s['lang'], [{'ext': 'vtt', 'url': s['src']}])
  164. for s in subs)
  165. class RTVEInfantilIE(RTVEALaCartaIE):
  166. IE_NAME = 'rtve.es:infantil'
  167. IE_DESC = 'RTVE infantil'
  168. _VALID_URL = r'https?://(?:www\.)?rtve\.es/infantil/serie/[^/]+/video/[^/]+/(?P<id>[0-9]+)/'
  169. _TESTS = [{
  170. 'url': 'http://www.rtve.es/infantil/serie/cleo/video/maneras-vivir/3040283/',
  171. 'md5': '5747454717aedf9f9fdf212d1bcfc48d',
  172. 'info_dict': {
  173. 'id': '3040283',
  174. 'ext': 'mp4',
  175. 'title': 'Maneras de vivir',
  176. 'thumbnail': r're:https?://.+/1426182947956\.JPG',
  177. 'duration': 357.958,
  178. },
  179. 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'],
  180. }]
  181. class RTVELiveIE(RTVEALaCartaIE):
  182. IE_NAME = 'rtve.es:live'
  183. IE_DESC = 'RTVE.es live streams'
  184. _VALID_URL = r'https?://(?:www\.)?rtve\.es/directo/(?P<id>[a-zA-Z0-9-]+)'
  185. _TESTS = [{
  186. 'url': 'http://www.rtve.es/directo/la-1/',
  187. 'info_dict': {
  188. 'id': 'la-1',
  189. 'ext': 'mp4',
  190. 'title': 're:^La 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  191. },
  192. 'params': {
  193. 'skip_download': 'live stream',
  194. }
  195. }]
  196. def _real_extract(self, url):
  197. mobj = re.match(self._VALID_URL, url)
  198. video_id = mobj.group('id')
  199. webpage = self._download_webpage(url, video_id)
  200. title = remove_end(self._og_search_title(webpage), ' en directo en RTVE.es')
  201. title = remove_start(title, 'Estoy viendo ')
  202. vidplayer_id = self._search_regex(
  203. (r'playerId=player([0-9]+)',
  204. r'class=["\'].*?\blive_mod\b.*?["\'][^>]+data-assetid=["\'](\d+)',
  205. r'data-id=["\'](\d+)'),
  206. webpage, 'internal video ID')
  207. return {
  208. 'id': video_id,
  209. 'title': self._live_title(title),
  210. 'formats': self._extract_png_formats(vidplayer_id),
  211. 'is_live': True,
  212. }
  213. class RTVETelevisionIE(InfoExtractor):
  214. IE_NAME = 'rtve.es:television'
  215. _VALID_URL = r'https?://(?:www\.)?rtve\.es/television/[^/]+/[^/]+/(?P<id>\d+).shtml'
  216. _TEST = {
  217. 'url': 'http://www.rtve.es/television/20160628/revolucion-del-movil/1364141.shtml',
  218. 'info_dict': {
  219. 'id': '3069778',
  220. 'ext': 'mp4',
  221. 'title': 'Documentos TV - La revolución del móvil',
  222. 'duration': 3496.948,
  223. },
  224. 'params': {
  225. 'skip_download': True,
  226. },
  227. }
  228. def _real_extract(self, url):
  229. page_id = self._match_id(url)
  230. webpage = self._download_webpage(url, page_id)
  231. alacarta_url = self._search_regex(
  232. r'data-location="alacarta_videos"[^<]+url&quot;:&quot;(http://www\.rtve\.es/alacarta.+?)&',
  233. webpage, 'alacarta url', default=None)
  234. if alacarta_url is None:
  235. raise ExtractorError(
  236. 'The webpage doesn\'t contain any video', expected=True)
  237. return self.url_result(alacarta_url, ie=RTVEALaCartaIE.ie_key())