rtve.py 7.2 KB

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