videa.py 6.3 KB

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