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.

172 lines
5.6 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. 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 RTVELiveIE(InfoExtractor):
  116. IE_NAME = 'rtve.es:live'
  117. IE_DESC = 'RTVE.es live streams'
  118. _VALID_URL = r'http://www\.rtve\.es/(?:deportes/directo|noticias|television)/(?P<id>[a-zA-Z0-9-]+)'
  119. _TESTS = [{
  120. 'url': 'http://www.rtve.es/noticias/directo-la-1/',
  121. 'info_dict': {
  122. 'id': 'directo-la-1',
  123. 'ext': 'flv',
  124. 'title': 're:^La 1 de TVE [0-9]{4}-[0-9]{2}-[0-9]{2}Z[0-9]{6}$',
  125. },
  126. 'params': {
  127. 'skip_download': 'live stream',
  128. }
  129. }]
  130. def _real_extract(self, url):
  131. mobj = re.match(self._VALID_URL, url)
  132. start_time = time.gmtime()
  133. video_id = mobj.group('id')
  134. webpage = self._download_webpage(url, video_id)
  135. player_url = self._search_regex(
  136. r'<param name="movie" value="([^"]+)"/>', webpage, 'player URL')
  137. title = remove_end(self._og_search_title(webpage), ' en directo')
  138. title += ' ' + time.strftime('%Y-%m-%dZ%H%M%S', start_time)
  139. vidplayer_id = self._search_regex(
  140. r' id="vidplayer([0-9]+)"', webpage, 'internal video ID')
  141. png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/default/videos/%s.png' % vidplayer_id
  142. png = self._download_webpage(png_url, video_id, 'Downloading url information')
  143. video_url = _decrypt_url(png)
  144. return {
  145. 'id': video_id,
  146. 'ext': 'flv',
  147. 'title': title,
  148. 'url': video_url,
  149. 'app': 'rtve-live-live?ovpfv=2.1.2',
  150. 'player_url': player_url,
  151. 'rtmp_live': True,
  152. }