]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/videa.py
[extractors] Use new framework for existing embeds (#4307)
[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 },
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 rc4(cipher_text, key):
79 res = b''
80
81 key_len = len(key)
82 S = list(range(256))
83
84 j = 0
85 for i in range(256):
86 j = (j + S[i] + ord(key[i % key_len])) % 256
87 S[i], S[j] = S[j], S[i]
88
89 i = 0
90 j = 0
91 for m in range(len(cipher_text)):
92 i = (i + 1) % 256
93 j = (j + S[i]) % 256
94 S[i], S[j] = S[j], S[i]
95 k = S[(S[i] + S[j]) % 256]
96 res += struct.pack('B', k ^ compat_ord(cipher_text[m]))
97
98 return res.decode()
99
100 def _real_extract(self, url):
101 video_id = self._match_id(url)
102 video_page = self._download_webpage(url, video_id)
103
104 if 'videa.hu/player' in url:
105 player_url = url
106 player_page = video_page
107 else:
108 player_url = self._search_regex(
109 r'<iframe.*?src="(/player\?[^"]+)"', video_page, 'player url')
110 player_url = urljoin(url, player_url)
111 player_page = self._download_webpage(player_url, video_id)
112
113 nonce = self._search_regex(
114 r'_xt\s*=\s*"([^"]+)"', player_page, 'nonce')
115 l = nonce[:32]
116 s = nonce[32:]
117 result = ''
118 for i in range(0, 32):
119 result += s[i - (self._STATIC_SECRET.index(l[i]) - 31)]
120
121 query = parse_qs(player_url)
122 random_seed = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(8))
123 query['_s'] = random_seed
124 query['_t'] = result[:16]
125
126 b64_info, handle = self._download_webpage_handle(
127 'http://videa.hu/videaplayer_get_xml.php', video_id, query=query)
128 if b64_info.startswith('<?xml'):
129 info = self._parse_xml(b64_info, video_id)
130 else:
131 key = result[16:] + random_seed + handle.headers['x-videa-xs']
132 info = self._parse_xml(self.rc4(
133 compat_b64decode(b64_info), key), video_id)
134
135 video = xpath_element(info, './video', 'video')
136 if video is None:
137 raise ExtractorError(xpath_element(
138 info, './error', fatal=True), expected=True)
139 sources = xpath_element(
140 info, './video_sources', 'sources', fatal=True)
141 hash_values = xpath_element(
142 info, './hash_values', 'hash values', fatal=False)
143
144 title = xpath_text(video, './title', fatal=True)
145
146 formats = []
147 for source in sources.findall('./video_source'):
148 source_url = source.text
149 source_name = source.get('name')
150 source_exp = source.get('exp')
151 if not (source_url and source_name):
152 continue
153 hash_value = (
154 xpath_text(hash_values, 'hash_value_' + source_name)
155 if hash_values is not None else None)
156 if hash_value and source_exp:
157 source_url = update_url_query(source_url, {
158 'md5': hash_value,
159 'expires': source_exp,
160 })
161 f = parse_codecs(source.get('codecs'))
162 f.update({
163 'url': self._proto_relative_url(source_url),
164 'ext': mimetype2ext(source.get('mimetype')) or 'mp4',
165 'format_id': source.get('name'),
166 'width': int_or_none(source.get('width')),
167 'height': int_or_none(source.get('height')),
168 })
169 formats.append(f)
170 self._sort_formats(formats)
171
172 thumbnail = self._proto_relative_url(xpath_text(video, './poster_src'))
173
174 age_limit = None
175 is_adult = xpath_text(video, './is_adult_content', default=None)
176 if is_adult:
177 age_limit = 18 if is_adult == '1' else 0
178
179 return {
180 'id': video_id,
181 'title': title,
182 'thumbnail': thumbnail,
183 'duration': int_or_none(xpath_text(video, './duration')),
184 'age_limit': age_limit,
185 'formats': formats,
186 }