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.

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