2
0

rtve.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import base64
  4. import re
  5. import time
  6. from .common import InfoExtractor
  7. from ..compat import (
  8. compat_struct_unpack,
  9. )
  10. from ..utils import (
  11. ExtractorError,
  12. float_or_none,
  13. remove_end,
  14. remove_start,
  15. sanitized_Request,
  16. std_headers,
  17. )
  18. def _decrypt_url(png):
  19. encrypted_data = base64.b64decode(png.encode('utf-8'))
  20. text_index = encrypted_data.find(b'tEXt')
  21. text_chunk = encrypted_data[text_index - 4:]
  22. length = compat_struct_unpack('!I', text_chunk[:4])[0]
  23. # Use bytearray to get integers when iterating in both python 2.x and 3.x
  24. data = bytearray(text_chunk[8:8 + length])
  25. data = [chr(b) for b in data if b != 0]
  26. hash_index = data.index('#')
  27. alphabet_data = data[:hash_index]
  28. url_data = data[hash_index + 1:]
  29. alphabet = []
  30. e = 0
  31. d = 0
  32. for l in alphabet_data:
  33. if d == 0:
  34. alphabet.append(l)
  35. d = e = (e + 1) % 4
  36. else:
  37. d -= 1
  38. url = ''
  39. f = 0
  40. e = 3
  41. b = 1
  42. for letter in url_data:
  43. if f == 0:
  44. l = int(letter) * 10
  45. f = 1
  46. else:
  47. if e == 0:
  48. l += int(letter)
  49. url += alphabet[l]
  50. e = (b + 3) % 4
  51. f = 0
  52. b += 1
  53. else:
  54. e -= 1
  55. return url
  56. class RTVEALaCartaIE(InfoExtractor):
  57. IE_NAME = 'rtve.es:alacarta'
  58. IE_DESC = 'RTVE a la carta'
  59. _VALID_URL = r'https?://www\.rtve\.es/(m/)?alacarta/videos/[^/]+/[^/]+/(?P<id>\d+)'
  60. _TESTS = [{
  61. 'url': 'http://www.rtve.es/alacarta/videos/balonmano/o-swiss-cup-masculina-final-espana-suecia/2491869/',
  62. 'md5': '1d49b7e1ca7a7502c56a4bf1b60f1b43',
  63. 'info_dict': {
  64. 'id': '2491869',
  65. 'ext': 'mp4',
  66. 'title': 'Balonmano - Swiss Cup masculina. Final: España-Suecia',
  67. 'duration': 5024.566,
  68. },
  69. }, {
  70. 'note': 'Live stream',
  71. 'url': 'http://www.rtve.es/alacarta/videos/television/24h-live/1694255/',
  72. 'info_dict': {
  73. 'id': '1694255',
  74. 'ext': 'flv',
  75. 'title': 'TODO',
  76. },
  77. 'skip': 'The f4m manifest can\'t be used yet',
  78. }, {
  79. 'url': 'http://www.rtve.es/m/alacarta/videos/cuentame-como-paso/cuentame-como-paso-t16-ultimo-minuto-nuestra-vida-capitulo-276/2969138/?media=tve',
  80. 'only_matching': True,
  81. }]
  82. def _real_initialize(self):
  83. user_agent_b64 = base64.b64encode(std_headers['User-Agent'].encode('utf-8')).decode('utf-8')
  84. manager_info = self._download_json(
  85. 'http://www.rtve.es/odin/loki/' + user_agent_b64,
  86. None, 'Fetching manager info')
  87. self._manager = manager_info['manager']
  88. def _real_extract(self, url):
  89. mobj = re.match(self._VALID_URL, url)
  90. video_id = mobj.group('id')
  91. info = self._download_json(
  92. 'http://www.rtve.es/api/videos/%s/config/alacarta_videos.json' % video_id,
  93. video_id)['page']['items'][0]
  94. if info['state'] == 'DESPU':
  95. raise ExtractorError('The video is no longer available', expected=True)
  96. png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/%s/videos/%s.png' % (self._manager, video_id)
  97. png_request = sanitized_Request(png_url)
  98. png_request.add_header('Referer', url)
  99. png = self._download_webpage(png_request, video_id, 'Downloading url information')
  100. video_url = _decrypt_url(png)
  101. if not video_url.endswith('.f4m'):
  102. video_url = video_url.replace(
  103. 'resources/', 'auth/resources/'
  104. ).replace('.net.rtve', '.multimedia.cdn.rtve')
  105. subtitles = None
  106. if info.get('sbtFile') is not None:
  107. subtitles = self.extract_subtitles(video_id, info['sbtFile'])
  108. return {
  109. 'id': video_id,
  110. 'title': info['title'],
  111. 'url': video_url,
  112. 'thumbnail': info.get('image'),
  113. 'page_url': url,
  114. 'subtitles': subtitles,
  115. 'duration': float_or_none(info.get('duration'), scale=1000),
  116. }
  117. def _get_subtitles(self, video_id, sub_file):
  118. subs = self._download_json(
  119. sub_file + '.json', video_id,
  120. 'Downloading subtitles info')['page']['items']
  121. return dict(
  122. (s['lang'], [{'ext': 'vtt', 'url': s['src']}])
  123. for s in subs)
  124. class RTVEInfantilIE(InfoExtractor):
  125. IE_NAME = 'rtve.es:infantil'
  126. IE_DESC = 'RTVE infantil'
  127. _VALID_URL = r'https?://(?:www\.)?rtve\.es/infantil/serie/(?P<show>[^/]*)/video/(?P<short_title>[^/]*)/(?P<id>[0-9]+)/'
  128. _TESTS = [{
  129. 'url': 'http://www.rtve.es/infantil/serie/cleo/video/maneras-vivir/3040283/',
  130. 'md5': '915319587b33720b8e0357caaa6617e6',
  131. 'info_dict': {
  132. 'id': '3040283',
  133. 'ext': 'mp4',
  134. 'title': 'Maneras de vivir',
  135. 'thumbnail': 'http://www.rtve.es/resources/jpg/6/5/1426182947956.JPG',
  136. 'duration': 357.958,
  137. },
  138. }]
  139. def _real_extract(self, url):
  140. video_id = self._match_id(url)
  141. info = self._download_json(
  142. 'http://www.rtve.es/api/videos/%s/config/alacarta_videos.json' % video_id,
  143. video_id)['page']['items'][0]
  144. webpage = self._download_webpage(url, video_id)
  145. vidplayer_id = self._search_regex(
  146. r' id="vidplayer([0-9]+)"', webpage, 'internal video ID')
  147. png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/default/videos/%s.png' % vidplayer_id
  148. png = self._download_webpage(png_url, video_id, 'Downloading url information')
  149. video_url = _decrypt_url(png)
  150. return {
  151. 'id': video_id,
  152. 'ext': 'mp4',
  153. 'title': info['title'],
  154. 'url': video_url,
  155. 'thumbnail': info.get('image'),
  156. 'duration': float_or_none(info.get('duration'), scale=1000),
  157. }
  158. class RTVELiveIE(InfoExtractor):
  159. IE_NAME = 'rtve.es:live'
  160. IE_DESC = 'RTVE.es live streams'
  161. _VALID_URL = r'https?://www\.rtve\.es/directo/(?P<id>[a-zA-Z0-9-]+)'
  162. _TESTS = [{
  163. 'url': 'http://www.rtve.es/directo/la-1/',
  164. 'info_dict': {
  165. 'id': 'la-1',
  166. 'ext': 'mp4',
  167. 'title': 're:^La 1 [0-9]{4}-[0-9]{2}-[0-9]{2}Z[0-9]{6}$',
  168. },
  169. 'params': {
  170. 'skip_download': 'live stream',
  171. }
  172. }]
  173. def _real_extract(self, url):
  174. mobj = re.match(self._VALID_URL, url)
  175. start_time = time.gmtime()
  176. video_id = mobj.group('id')
  177. webpage = self._download_webpage(url, video_id)
  178. title = remove_end(self._og_search_title(webpage), ' en directo en RTVE.es')
  179. title = remove_start(title, 'Estoy viendo ')
  180. title += ' ' + time.strftime('%Y-%m-%dZ%H%M%S', start_time)
  181. vidplayer_id = self._search_regex(
  182. r'playerId=player([0-9]+)', webpage, 'internal video ID')
  183. png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/amonet/videos/%s.png' % vidplayer_id
  184. png = self._download_webpage(png_url, video_id, 'Downloading url information')
  185. m3u8_url = _decrypt_url(png)
  186. formats = self._extract_m3u8_formats(m3u8_url, video_id, ext='mp4')
  187. self._sort_formats(formats)
  188. return {
  189. 'id': video_id,
  190. 'title': title,
  191. 'formats': formats,
  192. 'is_live': True,
  193. }