[ie/twitter:spaces] Support video spaces (#10789)
[yt-dlp3.git] / yt_dlp / extractor / videa.py
blob8dc7ebd15e2488024b4a1169606b940e457c6f7c
1 import base64
2 import random
3 import string
4 import struct
6 from .common import InfoExtractor
7 from ..compat import compat_ord
8 from ..utils import (
9 ExtractorError,
10 int_or_none,
11 mimetype2ext,
12 parse_codecs,
13 parse_qs,
14 update_url_query,
15 urljoin,
16 xpath_element,
17 xpath_text,
21 class VideaIE(InfoExtractor):
22 _VALID_URL = r'''(?x)
23 https?://
24 videa(?:kid)?\.hu/
25 (?:
26 videok/(?:[^/]+/)*[^?#&]+-|
27 (?:videojs_)?player\?.*?\bv=|
28 player/v/
30 (?P<id>[^?#&]+)
31 '''
32 _EMBED_REGEX = [r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//videa\.hu/player\?.*?\bv=.+?)\1']
33 _TESTS = [{
34 'url': 'http://videa.hu/videok/allatok/az-orult-kigyasz-285-kigyot-kigyo-8YfIAjxwWGwT8HVQ',
35 'md5': '97a7af41faeaffd9f1fc864a7c7e7603',
36 'info_dict': {
37 'id': '8YfIAjxwWGwT8HVQ',
38 'ext': 'mp4',
39 'title': 'Az őrült kígyász 285 kígyót enged szabadon',
40 'thumbnail': r're:^https?://.*',
41 'duration': 21,
42 'age_limit': 0,
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 'age_limit': 0,
55 }, {
56 'url': 'http://videa.hu/player?v=8YfIAjxwWGwT8HVQ',
57 'md5': '97a7af41faeaffd9f1fc864a7c7e7603',
58 'info_dict': {
59 'id': '8YfIAjxwWGwT8HVQ',
60 'ext': 'mp4',
61 'title': 'Az őrült kígyász 285 kígyót enged szabadon',
62 'thumbnail': r're:^https?://.*',
63 'duration': 21,
64 'age_limit': 0,
66 }, {
67 'url': 'http://videa.hu/player/v/8YfIAjxwWGwT8HVQ?autoplay=1',
68 'only_matching': True,
69 }, {
70 'url': 'https://videakid.hu/videok/origo/jarmuvek/supercars-elozes-jAHDWfWSJH5XuFhH',
71 'only_matching': True,
72 }, {
73 'url': 'https://videakid.hu/player?v=8YfIAjxwWGwT8HVQ',
74 'only_matching': True,
75 }, {
76 'url': 'https://videakid.hu/player/v/8YfIAjxwWGwT8HVQ?autoplay=1',
77 'only_matching': True,
79 _STATIC_SECRET = 'xHb0ZvME5q8CBcoQi6AngerDu3FGO9fkUlwPmLVY_RTzj2hJIS4NasXWKy1td7p'
81 @staticmethod
82 def rc4(cipher_text, key):
83 res = b''
85 key_len = len(key)
86 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]
93 i = 0
94 j = 0
95 for m in range(len(cipher_text)):
96 i = (i + 1) % 256
97 j = (j + S[i]) % 256
98 S[i], S[j] = S[j], S[i]
99 k = S[(S[i] + S[j]) % 256]
100 res += struct.pack('B', k ^ compat_ord(cipher_text[m]))
102 return res.decode()
104 def _real_extract(self, url):
105 video_id = self._match_id(url)
106 video_page = self._download_webpage(url, video_id)
108 if 'videa.hu/player' in url:
109 player_url = url
110 player_page = video_page
111 else:
112 player_url = self._search_regex(
113 r'<iframe.*?src="(/player\?[^"]+)"', video_page, 'player url')
114 player_url = urljoin(url, player_url)
115 player_page = self._download_webpage(player_url, video_id)
117 nonce = self._search_regex(
118 r'_xt\s*=\s*"([^"]+)"', player_page, 'nonce')
119 l = nonce[:32]
120 s = nonce[32:]
121 result = ''
122 for i in range(32):
123 result += s[i - (self._STATIC_SECRET.index(l[i]) - 31)]
125 query = parse_qs(player_url)
126 random_seed = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
127 query['_s'] = random_seed
128 query['_t'] = result[:16]
130 b64_info, handle = self._download_webpage_handle(
131 'http://videa.hu/player/xml', video_id, query=query)
132 if b64_info.startswith('<?xml'):
133 info = self._parse_xml(b64_info, video_id)
134 else:
135 key = result[16:] + random_seed + handle.headers['x-videa-xs']
136 info = self._parse_xml(self.rc4(
137 base64.b64decode(b64_info), key), video_id)
139 video = xpath_element(info, './video', 'video')
140 if video is None:
141 raise ExtractorError(xpath_element(
142 info, './error', fatal=True), expected=True)
143 sources = xpath_element(
144 info, './video_sources', 'sources', fatal=True)
145 hash_values = xpath_element(
146 info, './hash_values', 'hash values', fatal=False)
148 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,
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')),
173 formats.append(f)
175 thumbnail = self._proto_relative_url(xpath_text(video, './poster_src'))
177 age_limit = None
178 is_adult = xpath_text(video, './is_adult_content', default=None)
179 if is_adult:
180 age_limit = 18 if is_adult == '1' else 0
182 return {
183 'id': video_id,
184 'title': title,
185 'thumbnail': thumbnail,
186 'duration': int_or_none(xpath_text(video, './duration')),
187 'age_limit': age_limit,
188 'formats': formats,