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.

141 lines
4.2 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/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. }]
  70. def _real_extract(self, url):
  71. mobj = re.match(self._VALID_URL, url)
  72. video_id = mobj.group('id')
  73. info = self._download_json(
  74. 'http://www.rtve.es/api/videos/%s/config/alacarta_videos.json' % video_id,
  75. video_id)['page']['items'][0]
  76. png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/default/videos/%s.png' % video_id
  77. png = self._download_webpage(png_url, video_id, 'Downloading url information')
  78. video_url = _decrypt_url(png)
  79. return {
  80. 'id': video_id,
  81. 'title': info['title'],
  82. 'url': video_url,
  83. 'thumbnail': info.get('image'),
  84. 'page_url': url,
  85. }
  86. class RTVELiveIE(InfoExtractor):
  87. IE_NAME = 'rtve.es:live'
  88. IE_DESC = 'RTVE.es live streams'
  89. _VALID_URL = r'http://www\.rtve\.es/(?:deportes/directo|noticias|television)/(?P<id>[a-zA-Z0-9-]+)'
  90. _TESTS = [{
  91. 'url': 'http://www.rtve.es/noticias/directo-la-1/',
  92. 'info_dict': {
  93. 'id': 'directo-la-1',
  94. 'ext': 'flv',
  95. 'title': 're:^La 1 de TVE [0-9]{4}-[0-9]{2}-[0-9]{2}Z[0-9]{6}$',
  96. },
  97. 'params': {
  98. 'skip_download': 'live stream',
  99. }
  100. }]
  101. def _real_extract(self, url):
  102. mobj = re.match(self._VALID_URL, url)
  103. start_time = time.gmtime()
  104. video_id = mobj.group('id')
  105. webpage = self._download_webpage(url, video_id)
  106. player_url = self._search_regex(
  107. r'<param name="movie" value="([^"]+)"/>', webpage, 'player URL')
  108. title = remove_end(self._og_search_title(webpage), ' en directo')
  109. title += ' ' + time.strftime('%Y-%m-%dZ%H%M%S', start_time)
  110. vidplayer_id = self._search_regex(
  111. r' id="vidplayer([0-9]+)"', webpage, 'internal video ID')
  112. png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/default/videos/%s.png' % vidplayer_id
  113. png = self._download_webpage(png_url, video_id, 'Downloading url information')
  114. video_url = _decrypt_url(png)
  115. return {
  116. 'id': video_id,
  117. 'ext': 'flv',
  118. 'title': title,
  119. 'url': video_url,
  120. 'app': 'rtve-live-live?ovpfv=2.1.2',
  121. 'player_url': player_url,
  122. 'rtmp_live': True,
  123. }