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