logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

videa.py (7298B)


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