]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/videa.py
[extractors] Use new framework for existing embeds (#4307)
[yt-dlp.git] / yt_dlp / extractor / videa.py
CommitLineData
34675f9d
AH
1import random
2import string
ac668111 3import struct
e186a9ec 4
e7460215 5from .common import InfoExtractor
ac668111 6from ..compat import compat_b64decode, compat_ord
e7460215 7from ..utils import (
34675f9d 8 ExtractorError,
e7460215 9 int_or_none,
69677f3e
S
10 mimetype2ext,
11 parse_codecs,
4dfbf869 12 parse_qs,
29f7c58a 13 update_url_query,
e010672a 14 urljoin,
e7460215
B
15 xpath_element,
16 xpath_text,
e7460215
B
17)
18
19
20class VideaIE(InfoExtractor):
69677f3e
S
21 _VALID_URL = r'''(?x)
22 https?://
99c30918 23 videa(?:kid)?\.hu/
69677f3e
S
24 (?:
25 videok/(?:[^/]+/)*[^?#&]+-|
29f7c58a 26 (?:videojs_)?player\?.*?\bv=|
69677f3e
S
27 player/v/
28 )
29 (?P<id>[^?#&]+)
30 '''
bfd973ec 31 _EMBED_REGEX = [r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//videa\.hu/player\?.*?\bv=.+?)\1']
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
29f7c58a 77 @staticmethod
78 def rc4(cipher_text, key):
34675f9d
AH
79 res = b''
80
29f7c58a 81 key_len = len(key)
34675f9d
AH
82 S = list(range(256))
83
84 j = 0
85 for i in range(256):
29f7c58a 86 j = (j + S[i] + ord(key[i % key_len])) % 256
34675f9d
AH
87 S[i], S[j] = S[j], S[i]
88
89 i = 0
90 j = 0
29f7c58a 91 for m in range(len(cipher_text)):
34675f9d
AH
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]
ac668111 96 res += struct.pack('B', k ^ compat_ord(cipher_text[m]))
34675f9d 97
29f7c58a 98 return res.decode()
34675f9d 99
e7460215
B
100 def _real_extract(self, url):
101 video_id = self._match_id(url)
e010672a 102 video_page = self._download_webpage(url, video_id)
103
47626219 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)
29f7c58a 112
113 nonce = self._search_regex(
114 r'_xt\s*=\s*"([^"]+)"', player_page, 'nonce')
34675f9d
AH
115 l = nonce[:32]
116 s = nonce[32:]
117 result = ''
118 for i in range(0, 32):
29f7c58a 119 result += s[i - (self._STATIC_SECRET.index(l[i]) - 31)]
120
4dfbf869 121 query = parse_qs(player_url)
29f7c58a 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')
50e93e03 136 if video is None:
29f7c58a 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(
47626219 142 info, './hash_values', 'hash values', fatal=False)
e7460215 143
69677f3e 144 title = xpath_text(video, './title', fatal=True)
e7460215 145
69677f3e
S
146 formats = []
147 for source in sources.findall('./video_source'):
148 source_url = source.text
29f7c58a 149 source_name = source.get('name')
150 source_exp = source.get('exp')
47626219 151 if not (source_url and source_name):
69677f3e 152 continue
50e93e03 153 hash_value = (
154 xpath_text(hash_values, 'hash_value_' + source_name)
155 if hash_values is not None else None)
47626219 156 if hash_value and source_exp:
157 source_url = update_url_query(source_url, {
158 'md5': hash_value,
159 'expires': source_exp,
160 })
69677f3e
S
161 f = parse_codecs(source.get('codecs'))
162 f.update({
29f7c58a 163 'url': self._proto_relative_url(source_url),
69677f3e
S
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)
e7460215 171
29f7c58a 172 thumbnail = self._proto_relative_url(xpath_text(video, './poster_src'))
e7460215 173
69677f3e
S
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
e7460215 178
69677f3e
S
179 return {
180 'id': video_id,
181 'title': title,
182 'thumbnail': thumbnail,
29f7c58a 183 'duration': int_or_none(xpath_text(video, './duration')),
69677f3e
S
184 'age_limit': age_limit,
185 'formats': formats,
186 }