videa.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import random
  4. import re
  5. import string
  6. from .common import InfoExtractor
  7. from ..utils import (
  8. ExtractorError,
  9. int_or_none,
  10. mimetype2ext,
  11. parse_codecs,
  12. update_url_query,
  13. xpath_element,
  14. xpath_text,
  15. )
  16. from ..compat import (
  17. compat_b64decode,
  18. compat_ord,
  19. compat_struct_pack,
  20. )
  21. class VideaIE(InfoExtractor):
  22. _VALID_URL = r'''(?x)
  23. https?://
  24. videa(?:kid)?\.hu/
  25. (?:
  26. videok/(?:[^/]+/)*[^?#&]+-|
  27. (?:videojs_)?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. _STATIC_SECRET = 'xHb0ZvME5q8CBcoQi6AngerDu3FGO9fkUlwPmLVY_RTzj2hJIS4NasXWKy1td7p'
  62. @staticmethod
  63. def _extract_urls(webpage):
  64. return [url for _, url in re.findall(
  65. r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//videa\.hu/player\?.*?\bv=.+?)\1',
  66. webpage)]
  67. @staticmethod
  68. def rc4(cipher_text, key):
  69. res = b''
  70. key_len = len(key)
  71. S = list(range(256))
  72. j = 0
  73. for i in range(256):
  74. j = (j + S[i] + ord(key[i % key_len])) % 256
  75. S[i], S[j] = S[j], S[i]
  76. i = 0
  77. j = 0
  78. for m in range(len(cipher_text)):
  79. i = (i + 1) % 256
  80. j = (j + S[i]) % 256
  81. S[i], S[j] = S[j], S[i]
  82. k = S[(S[i] + S[j]) % 256]
  83. res += compat_struct_pack('B', k ^ compat_ord(cipher_text[m]))
  84. return res.decode()
  85. def _real_extract(self, url):
  86. video_id = self._match_id(url)
  87. query = {'v': video_id}
  88. player_page = self._download_webpage(
  89. 'https://videa.hu/player', video_id, query=query)
  90. nonce = self._search_regex(
  91. r'_xt\s*=\s*"([^"]+)"', player_page, 'nonce')
  92. l = nonce[:32]
  93. s = nonce[32:]
  94. result = ''
  95. for i in range(0, 32):
  96. result += s[i - (self._STATIC_SECRET.index(l[i]) - 31)]
  97. random_seed = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(8))
  98. query['_s'] = random_seed
  99. query['_t'] = result[:16]
  100. b64_info, handle = self._download_webpage_handle(
  101. 'http://videa.hu/videaplayer_get_xml.php', video_id, query=query)
  102. if b64_info.startswith('<?xml'):
  103. info = self._parse_xml(b64_info, video_id)
  104. else:
  105. key = result[16:] + random_seed + handle.headers['x-videa-xs']
  106. info = self._parse_xml(self.rc4(
  107. compat_b64decode(b64_info), key), video_id)
  108. video = xpath_element(info, './video', 'video')
  109. if not video:
  110. raise ExtractorError(xpath_element(
  111. info, './error', fatal=True), expected=True)
  112. sources = xpath_element(
  113. info, './video_sources', 'sources', fatal=True)
  114. hash_values = xpath_element(
  115. info, './hash_values', 'hash values', fatal=True)
  116. title = xpath_text(video, './title', fatal=True)
  117. formats = []
  118. for source in sources.findall('./video_source'):
  119. source_url = source.text
  120. source_name = source.get('name')
  121. source_exp = source.get('exp')
  122. if not (source_url and source_name and source_exp):
  123. continue
  124. hash_value = xpath_text(hash_values, 'hash_value_' + source_name)
  125. if not hash_value:
  126. continue
  127. source_url = update_url_query(source_url, {
  128. 'md5': hash_value,
  129. 'expires': source_exp,
  130. })
  131. f = parse_codecs(source.get('codecs'))
  132. f.update({
  133. 'url': self._proto_relative_url(source_url),
  134. 'ext': mimetype2ext(source.get('mimetype')) or 'mp4',
  135. 'format_id': source.get('name'),
  136. 'width': int_or_none(source.get('width')),
  137. 'height': int_or_none(source.get('height')),
  138. })
  139. formats.append(f)
  140. self._sort_formats(formats)
  141. thumbnail = self._proto_relative_url(xpath_text(video, './poster_src'))
  142. age_limit = None
  143. is_adult = xpath_text(video, './is_adult_content', default=None)
  144. if is_adult:
  145. age_limit = 18 if is_adult == '1' else 0
  146. return {
  147. 'id': video_id,
  148. 'title': title,
  149. 'thumbnail': thumbnail,
  150. 'duration': int_or_none(xpath_text(video, './duration')),
  151. 'age_limit': age_limit,
  152. 'formats': formats,
  153. }