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.

84 lines
2.5 KiB

  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import base64
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. struct_unpack,
  8. )
  9. class RTVEALaCartaIE(InfoExtractor):
  10. IE_NAME = 'rtve.es:alacarta'
  11. IE_DESC = 'RTVE a la carta'
  12. _VALID_URL = r'http://www\.rtve\.es/alacarta/videos/[^/]+/[^/]+/(?P<id>\d+)'
  13. _TEST = {
  14. 'url': 'http://www.rtve.es/alacarta/videos/balonmano/o-swiss-cup-masculina-final-espana-suecia/2491869/',
  15. 'md5': '1d49b7e1ca7a7502c56a4bf1b60f1b43',
  16. 'info_dict': {
  17. 'id': '2491869',
  18. 'ext': 'mp4',
  19. 'title': 'Balonmano - Swiss Cup masculina. Final: España-Suecia',
  20. },
  21. }
  22. def _decrypt_url(self, png):
  23. encrypted_data = base64.b64decode(png)
  24. text_index = encrypted_data.find(b'tEXt')
  25. text_chunk = encrypted_data[text_index-4:]
  26. length = struct_unpack('!I', text_chunk[:4])[0]
  27. # Use bytearray to get integers when iterating in both python 2.x and 3.x
  28. data = bytearray(text_chunk[8:8+length])
  29. data = [chr(b) for b in data if b != 0]
  30. hash_index = data.index('#')
  31. alphabet_data = data[:hash_index]
  32. url_data = data[hash_index+1:]
  33. alphabet = []
  34. e = 0
  35. d = 0
  36. for l in alphabet_data:
  37. if d == 0:
  38. alphabet.append(l)
  39. d = e = (e + 1) % 4
  40. else:
  41. d -= 1
  42. url = ''
  43. f = 0
  44. e = 3
  45. b = 1
  46. for letter in url_data:
  47. if f == 0:
  48. l = int(letter)*10
  49. f = 1
  50. else:
  51. if e == 0:
  52. l += int(letter)
  53. url += alphabet[l]
  54. e = (b + 3) % 4
  55. f = 0
  56. b += 1
  57. else:
  58. e -= 1
  59. return url
  60. def _real_extract(self, url):
  61. mobj = re.match(self._VALID_URL, url)
  62. video_id = mobj.group('id')
  63. info = self._download_json(
  64. 'http://www.rtve.es/api/videos/%s/config/alacarta_videos.json' % video_id,
  65. video_id)['page']['items'][0]
  66. png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/default/videos/%s.png' % video_id
  67. png = self._download_webpage(png_url, video_id, 'Downloading url information')
  68. video_url = self._decrypt_url(png)
  69. return {
  70. 'id': video_id,
  71. 'title': info['title'],
  72. 'url': video_url,
  73. 'thumbnail': info['image'],
  74. }