videa.py 6.6 KB

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