videa.py 7.1 KB

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