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.

156 lines
5.0 KiB

  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. return {
  95. 'id': video_id,
  96. 'title': info['title'],
  97. 'url': video_url,
  98. 'thumbnail': info.get('image'),
  99. 'page_url': url,
  100. }
  101. class RTVELiveIE(InfoExtractor):
  102. IE_NAME = 'rtve.es:live'
  103. IE_DESC = 'RTVE.es live streams'
  104. _VALID_URL = r'http://www\.rtve\.es/(?:deportes/directo|noticias|television)/(?P<id>[a-zA-Z0-9-]+)'
  105. _TESTS = [{
  106. 'url': 'http://www.rtve.es/noticias/directo-la-1/',
  107. 'info_dict': {
  108. 'id': 'directo-la-1',
  109. 'ext': 'flv',
  110. 'title': 're:^La 1 de TVE [0-9]{4}-[0-9]{2}-[0-9]{2}Z[0-9]{6}$',
  111. },
  112. 'params': {
  113. 'skip_download': 'live stream',
  114. }
  115. }]
  116. def _real_extract(self, url):
  117. mobj = re.match(self._VALID_URL, url)
  118. start_time = time.gmtime()
  119. video_id = mobj.group('id')
  120. webpage = self._download_webpage(url, video_id)
  121. player_url = self._search_regex(
  122. r'<param name="movie" value="([^"]+)"/>', webpage, 'player URL')
  123. title = remove_end(self._og_search_title(webpage), ' en directo')
  124. title += ' ' + time.strftime('%Y-%m-%dZ%H%M%S', start_time)
  125. vidplayer_id = self._search_regex(
  126. r' id="vidplayer([0-9]+)"', webpage, 'internal video ID')
  127. png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/default/videos/%s.png' % vidplayer_id
  128. png = self._download_webpage(png_url, video_id, 'Downloading url information')
  129. video_url = _decrypt_url(png)
  130. return {
  131. 'id': video_id,
  132. 'ext': 'flv',
  133. 'title': title,
  134. 'url': video_url,
  135. 'app': 'rtve-live-live?ovpfv=2.1.2',
  136. 'player_url': player_url,
  137. 'rtmp_live': True,
  138. }