videa.py 6.4 KB

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