videa.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import random
  5. import string
  6. import struct
  7. from .common import InfoExtractor
  8. from ..utils import (
  9. ExtractorError,
  10. int_or_none,
  11. mimetype2ext,
  12. parse_codecs,
  13. xpath_element,
  14. xpath_text,
  15. )
  16. from ..compat import (
  17. compat_b64decode,
  18. compat_ord,
  19. compat_parse_qs,
  20. )
  21. class VideaIE(InfoExtractor):
  22. _VALID_URL = r'''(?x)
  23. https?://
  24. videa(?:kid)?\.hu/
  25. (?:
  26. videok/(?:[^/]+/)*[^?#&]+-|
  27. player\?.*?\bv=|
  28. player/v/
  29. )
  30. (?P<id>[^?#&]+)
  31. '''
  32. _TESTS = [{
  33. 'url': 'http://videa.hu/videok/allatok/az-orult-kigyasz-285-kigyot-kigyo-8YfIAjxwWGwT8HVQ',
  34. 'md5': '97a7af41faeaffd9f1fc864a7c7e7603',
  35. 'info_dict': {
  36. 'id': '8YfIAjxwWGwT8HVQ',
  37. 'ext': 'mp4',
  38. 'title': 'Az őrült kígyász 285 kígyót enged szabadon',
  39. 'thumbnail': r're:^https?://.*',
  40. 'duration': 21,
  41. },
  42. }, {
  43. 'url': 'http://videa.hu/videok/origo/jarmuvek/supercars-elozes-jAHDWfWSJH5XuFhH',
  44. 'only_matching': True,
  45. }, {
  46. 'url': 'http://videa.hu/player?v=8YfIAjxwWGwT8HVQ',
  47. 'only_matching': True,
  48. }, {
  49. 'url': 'http://videa.hu/player/v/8YfIAjxwWGwT8HVQ?autoplay=1',
  50. 'only_matching': True,
  51. }, {
  52. 'url': 'https://videakid.hu/videok/origo/jarmuvek/supercars-elozes-jAHDWfWSJH5XuFhH',
  53. 'only_matching': True,
  54. }, {
  55. 'url': 'https://videakid.hu/player?v=8YfIAjxwWGwT8HVQ',
  56. 'only_matching': True,
  57. }, {
  58. 'url': 'https://videakid.hu/player/v/8YfIAjxwWGwT8HVQ?autoplay=1',
  59. 'only_matching': True,
  60. }]
  61. @staticmethod
  62. def _extract_urls(webpage):
  63. return [url for _, url in re.findall(
  64. r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//videa\.hu/player\?.*?\bv=.+?)\1',
  65. webpage)]
  66. def rc4(self, ciphertext, key):
  67. res = b''
  68. keyLen = len(key)
  69. S = list(range(256))
  70. j = 0
  71. for i in range(256):
  72. j = (j + S[i] + ord(key[i % keyLen])) % 256
  73. S[i], S[j] = S[j], S[i]
  74. i = 0
  75. j = 0
  76. for m in range(len(ciphertext)):
  77. i = (i + 1) % 256
  78. j = (j + S[i]) % 256
  79. S[i], S[j] = S[j], S[i]
  80. k = S[(S[i] + S[j]) % 256]
  81. res += struct.pack("B", k ^ compat_ord(ciphertext[m]))
  82. return res
  83. def _real_extract(self, url):
  84. video_id = self._match_id(url)
  85. webpage = self._download_webpage(url, video_id, fatal=True)
  86. error = self._search_regex(r'<p class="error-text">([^<]+)</p>', webpage, 'error', default=None)
  87. if error:
  88. raise ExtractorError(error, expected=True)
  89. video_src_params_raw = self._search_regex(r'<iframe[^>]+id="videa_player_iframe"[^>]+src="/player\?([^"]+)"', webpage, 'video_src_params')
  90. video_src_params = compat_parse_qs(video_src_params_raw)
  91. player_page = self._download_webpage("https://videa.hu/videojs_player?%s" % video_src_params_raw, video_id, fatal=True)
  92. nonce = self._search_regex(r'_xt\s*=\s*"([^"]+)"', player_page, 'nonce')
  93. random_seed = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(8))
  94. static_secret = 'xHb0ZvME5q8CBcoQi6AngerDu3FGO9fkUlwPmLVY_RTzj2hJIS4NasXWKy1td7p'
  95. l = nonce[:32]
  96. s = nonce[32:]
  97. result = ''
  98. for i in range(0, 32):
  99. result += s[i - (static_secret.index(l[i]) - 31)]
  100. video_src_params['_s'] = random_seed
  101. video_src_params['_t'] = result[:16]
  102. encryption_key_stem = result[16:] + random_seed
  103. [b64_info, handle] = self._download_webpage_handle(
  104. 'http://videa.hu/videaplayer_get_xml.php', video_id,
  105. query=video_src_params, fatal=True)
  106. encrypted_info = compat_b64decode(b64_info)
  107. key = encryption_key_stem + handle.info()['x-videa-xs']
  108. info_str = self.rc4(encrypted_info, key).decode('utf8')
  109. info = self._parse_xml(info_str, video_id)
  110. video = xpath_element(info, './/video', 'video', fatal=True)
  111. sources = xpath_element(info, './/video_sources', 'sources', fatal=True)
  112. hash_values = xpath_element(info, './/hash_values', 'hash_values', fatal=True)
  113. title = xpath_text(video, './title', fatal=True)
  114. formats = []
  115. for source in sources.findall('./video_source'):
  116. source_url = source.text
  117. if not source_url:
  118. continue
  119. source_url += '?md5=%s&expires=%s' % (hash_values.find('hash_value_%s' % source.get('name')).text, source.get('exp'))
  120. f = parse_codecs(source.get('codecs'))
  121. f.update({
  122. 'url': source_url,
  123. 'ext': mimetype2ext(source.get('mimetype')) or 'mp4',
  124. 'format_id': source.get('name'),
  125. 'width': int_or_none(source.get('width')),
  126. 'height': int_or_none(source.get('height')),
  127. })
  128. formats.append(f)
  129. self._sort_formats(formats)
  130. thumbnail = xpath_text(video, './poster_src')
  131. duration = int_or_none(xpath_text(video, './duration'))
  132. age_limit = None
  133. is_adult = xpath_text(video, './is_adult_content', default=None)
  134. if is_adult:
  135. age_limit = 18 if is_adult == '1' else 0
  136. return {
  137. 'id': video_id,
  138. 'title': title,
  139. 'thumbnail': thumbnail,
  140. 'duration': duration,
  141. 'age_limit': age_limit,
  142. 'formats': formats,
  143. }