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.

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