]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/videa.py
9b05c86a5e10eee342a1dfc39741d68248c7862b
[yt-dlp.git] / yt_dlp / extractor / videa.py
1 import random
2 import re
3 import string
4 import struct
5
6 from .common import InfoExtractor
7 from ..compat import compat_b64decode, 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,
18 )
19
20
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/
29 )
30 (?P<id>[^?#&]+)
31 '''
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 },
42 }, {
43 'url': 'http://videa.hu/videok/origo/jarmuvek/supercars-elozes-jAHDWfWSJH5XuFhH',
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 },
52 }, {
53 'url': 'http://videa.hu/player?v=8YfIAjxwWGwT8HVQ',
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 },
62 }, {
63 'url': 'http://videa.hu/player/v/8YfIAjxwWGwT8HVQ?autoplay=1',
64 'only_matching': True,
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,
74 }]
75 _STATIC_SECRET = 'xHb0ZvME5q8CBcoQi6AngerDu3FGO9fkUlwPmLVY_RTzj2hJIS4NasXWKy1td7p'
76
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
83 @staticmethod
84 def rc4(cipher_text, key):
85 res = b''
86
87 key_len = len(key)
88 S = list(range(256))
89
90 j = 0
91 for i in range(256):
92 j = (j + S[i] + ord(key[i % key_len])) % 256
93 S[i], S[j] = S[j], S[i]
94
95 i = 0
96 j = 0
97 for m in range(len(cipher_text)):
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]
102 res += struct.pack('B', k ^ compat_ord(cipher_text[m]))
103
104 return res.decode()
105
106 def _real_extract(self, url):
107 video_id = self._match_id(url)
108 video_page = self._download_webpage(url, video_id)
109
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)
118
119 nonce = self._search_regex(
120 r'_xt\s*=\s*"([^"]+)"', player_page, 'nonce')
121 l = nonce[:32]
122 s = nonce[32:]
123 result = ''
124 for i in range(0, 32):
125 result += s[i - (self._STATIC_SECRET.index(l[i]) - 31)]
126
127 query = parse_qs(player_url)
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')
142 if video is None:
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(
148 info, './hash_values', 'hash values', fatal=False)
149
150 title = xpath_text(video, './title', fatal=True)
151
152 formats = []
153 for source in sources.findall('./video_source'):
154 source_url = source.text
155 source_name = source.get('name')
156 source_exp = source.get('exp')
157 if not (source_url and source_name):
158 continue
159 hash_value = (
160 xpath_text(hash_values, 'hash_value_' + source_name)
161 if hash_values is not None else None)
162 if hash_value and source_exp:
163 source_url = update_url_query(source_url, {
164 'md5': hash_value,
165 'expires': source_exp,
166 })
167 f = parse_codecs(source.get('codecs'))
168 f.update({
169 'url': self._proto_relative_url(source_url),
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)
177
178 thumbnail = self._proto_relative_url(xpath_text(video, './poster_src'))
179
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
184
185 return {
186 'id': video_id,
187 'title': title,
188 'thumbnail': thumbnail,
189 'duration': int_or_none(xpath_text(video, './duration')),
190 'age_limit': age_limit,
191 'formats': formats,
192 }