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