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.

221 lines
7.5 KiB

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