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