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.

213 lines
7.1 KiB

9 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. 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:infantil'
  117. IE_DESC = 'RTVE infantil'
  118. _VALID_URL = r'https?://(?:www\.)?rtve\.es/infantil/serie/(?P<show>[^/]*)/video/(?P<short_title>[^/]*)/(?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. video_id = self._match_id(url)
  132. info = self._download_json(
  133. 'http://www.rtve.es/api/videos/%s/config/alacarta_videos.json' % video_id,
  134. video_id)['page']['items'][0]
  135. webpage = self._download_webpage(url, video_id)
  136. vidplayer_id = self._search_regex(
  137. r' id="vidplayer([0-9]+)"', webpage, 'internal video ID')
  138. png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/default/videos/%s.png' % vidplayer_id
  139. png = self._download_webpage(png_url, video_id, 'Downloading url information')
  140. video_url = _decrypt_url(png)
  141. return {
  142. 'id': video_id,
  143. 'ext': 'mp4',
  144. 'title': info['title'],
  145. 'url': video_url,
  146. 'thumbnail': info.get('image'),
  147. 'duration': float_or_none(info.get('duration'), scale=1000),
  148. }
  149. class RTVELiveIE(InfoExtractor):
  150. IE_NAME = 'rtve.es:live'
  151. IE_DESC = 'RTVE.es live streams'
  152. _VALID_URL = r'http://www\.rtve\.es/(?:deportes/directo|noticias|television)/(?P<id>[a-zA-Z0-9-]+)'
  153. _TESTS = [{
  154. 'url': 'http://www.rtve.es/noticias/directo-la-1/',
  155. 'info_dict': {
  156. 'id': 'directo-la-1',
  157. 'ext': 'flv',
  158. 'title': 're:^La 1 de TVE [0-9]{4}-[0-9]{2}-[0-9]{2}Z[0-9]{6}$',
  159. },
  160. 'params': {
  161. 'skip_download': 'live stream',
  162. }
  163. }]
  164. def _real_extract(self, url):
  165. mobj = re.match(self._VALID_URL, url)
  166. start_time = time.gmtime()
  167. video_id = mobj.group('id')
  168. webpage = self._download_webpage(url, video_id)
  169. player_url = self._search_regex(
  170. r'<param name="movie" value="([^"]+)"/>', webpage, 'player URL')
  171. title = remove_end(self._og_search_title(webpage), ' en directo')
  172. title += ' ' + time.strftime('%Y-%m-%dZ%H%M%S', start_time)
  173. vidplayer_id = self._search_regex(
  174. r' id="vidplayer([0-9]+)"', webpage, 'internal video ID')
  175. png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/default/videos/%s.png' % vidplayer_id
  176. png = self._download_webpage(png_url, video_id, 'Downloading url information')
  177. video_url = _decrypt_url(png)
  178. return {
  179. 'id': video_id,
  180. 'ext': 'flv',
  181. 'title': title,
  182. 'url': video_url,
  183. 'app': 'rtve-live-live?ovpfv=2.1.2',
  184. 'player_url': player_url,
  185. 'rtmp_live': True,
  186. }