rtve.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/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. def _real_extract(self, url):
  72. mobj = re.match(self._VALID_URL, url)
  73. video_id = mobj.group('id')
  74. info = self._download_json(
  75. 'http://www.rtve.es/api/videos/%s/config/alacarta_videos.json' % video_id,
  76. video_id)['page']['items'][0]
  77. png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/default/videos/%s.png' % video_id
  78. png = self._download_webpage(png_url, video_id, 'Downloading url information')
  79. video_url = _decrypt_url(png)
  80. if not video_url.endswith('.f4m'):
  81. auth_url = video_url.replace(
  82. 'resources/', 'auth/resources/'
  83. ).replace('.net.rtve', '.multimedia.cdn.rtve')
  84. video_path = self._download_webpage(
  85. auth_url, video_id, 'Getting video url')
  86. # Use mvod.akcdn instead of flash.akamaihd.multimedia.cdn to get
  87. # the right Content-Length header and the mp4 format
  88. video_url = (
  89. 'http://mvod.akcdn.rtve.es/{0}&v=2.6.8'
  90. '&fp=MAC%2016,0,0,296&r=MRUGG&g=OEOJWFXNFGCP'.format(video_path)
  91. )
  92. return {
  93. 'id': video_id,
  94. 'title': info['title'],
  95. 'url': video_url,
  96. 'thumbnail': info.get('image'),
  97. 'page_url': url,
  98. }
  99. class RTVELiveIE(InfoExtractor):
  100. IE_NAME = 'rtve.es:live'
  101. IE_DESC = 'RTVE.es live streams'
  102. _VALID_URL = r'http://www\.rtve\.es/(?:deportes/directo|noticias|television)/(?P<id>[a-zA-Z0-9-]+)'
  103. _TESTS = [{
  104. 'url': 'http://www.rtve.es/noticias/directo-la-1/',
  105. 'info_dict': {
  106. 'id': 'directo-la-1',
  107. 'ext': 'flv',
  108. 'title': 're:^La 1 de TVE [0-9]{4}-[0-9]{2}-[0-9]{2}Z[0-9]{6}$',
  109. },
  110. 'params': {
  111. 'skip_download': 'live stream',
  112. }
  113. }]
  114. def _real_extract(self, url):
  115. mobj = re.match(self._VALID_URL, url)
  116. start_time = time.gmtime()
  117. video_id = mobj.group('id')
  118. webpage = self._download_webpage(url, video_id)
  119. player_url = self._search_regex(
  120. r'<param name="movie" value="([^"]+)"/>', webpage, 'player URL')
  121. title = remove_end(self._og_search_title(webpage), ' en directo')
  122. title += ' ' + time.strftime('%Y-%m-%dZ%H%M%S', start_time)
  123. vidplayer_id = self._search_regex(
  124. r' id="vidplayer([0-9]+)"', webpage, 'internal video ID')
  125. png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/default/videos/%s.png' % vidplayer_id
  126. png = self._download_webpage(png_url, video_id, 'Downloading url information')
  127. video_url = _decrypt_url(png)
  128. return {
  129. 'id': video_id,
  130. 'ext': 'flv',
  131. 'title': title,
  132. 'url': video_url,
  133. 'app': 'rtve-live-live?ovpfv=2.1.2',
  134. 'player_url': player_url,
  135. 'rtmp_live': True,
  136. }