1
0
Fork 0
mirror of https://github.com/ytdl-org/youtube-dl.git synced 2024-12-22 16:57:40 +00:00
youtube-dl/youtube_dl/extractor/videa.py

215 lines
7.1 KiB
Python
Raw Normal View History

2016-11-03 11:53:45 +00:00
# coding: utf-8
from __future__ import unicode_literals
import random
2020-11-26 11:56:49 +00:00
import re
import string
2016-12-31 15:04:29 +00:00
2016-11-03 11:53:45 +00:00
from .common import InfoExtractor
2024-02-29 14:12:37 +00:00
from ..compat import (
compat_b64decode,
compat_ord,
compat_struct_pack,
)
2016-11-03 11:53:45 +00:00
from ..utils import (
ExtractorError,
2016-11-03 11:53:45 +00:00
int_or_none,
mimetype2ext,
parse_codecs,
2024-02-29 14:12:37 +00:00
parse_qs,
2020-11-26 11:56:49 +00:00
update_url_query,
urljoin,
2016-11-03 11:53:45 +00:00
xpath_element,
xpath_text,
)
2024-02-29 14:12:37 +00:00
def compat_random_choices(population, *args, **kwargs):
# weights=None, *, cum_weights=None, k=1
# limited implementation needed here
weights = args[0] if args else kwargs.get('weights')
assert all(w is None for w in (weights, kwargs.get('cum_weights')))
k = kwargs.get('k', 1)
return ''.join(random.choice(population) for _ in range(k))
2016-11-03 11:53:45 +00:00
class VideaIE(InfoExtractor):
_VALID_URL = r'''(?x)
https?://
2018-03-27 15:02:04 +00:00
videa(?:kid)?\.hu/
(?:
videok/(?:[^/]+/)*[^?#&]+-|
2020-11-26 11:56:49 +00:00
(?:videojs_)?player\?.*?\bv=|
player/v/
)
(?P<id>[^?#&]+)
'''
2024-02-29 14:12:37 +00:00
_EMBED_REGEX = [r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//videa\.hu/player\?.*?\bv=.+?)\1']
2016-11-03 11:53:45 +00:00
_TESTS = [{
'url': 'http://videa.hu/videok/allatok/az-orult-kigyasz-285-kigyot-kigyo-8YfIAjxwWGwT8HVQ',
'md5': '97a7af41faeaffd9f1fc864a7c7e7603',
'info_dict': {
'id': '8YfIAjxwWGwT8HVQ',
'ext': 'mp4',
'title': 'Az őrült kígyász 285 kígyót enged szabadon',
2018-03-27 15:02:04 +00:00
'thumbnail': r're:^https?://.*',
2016-11-03 11:53:45 +00:00
'duration': 21,
'age_limit': 0,
2016-11-03 11:53:45 +00:00
},
}, {
'url': 'http://videa.hu/videok/origo/jarmuvek/supercars-elozes-jAHDWfWSJH5XuFhH',
'md5': 'd57ccd8812c7fd491d33b1eab8c99975',
'info_dict': {
'id': 'jAHDWfWSJH5XuFhH',
'ext': 'mp4',
'title': 'Supercars előzés',
'thumbnail': r're:^https?://.*',
'duration': 64,
'age_limit': 0,
},
}, {
'url': 'http://videa.hu/player?v=8YfIAjxwWGwT8HVQ',
'md5': '97a7af41faeaffd9f1fc864a7c7e7603',
'info_dict': {
'id': '8YfIAjxwWGwT8HVQ',
'ext': 'mp4',
'title': 'Az őrült kígyász 285 kígyót enged szabadon',
'thumbnail': r're:^https?://.*',
'duration': 21,
'age_limit': 0,
},
}, {
'url': 'http://videa.hu/player/v/8YfIAjxwWGwT8HVQ?autoplay=1',
'only_matching': True,
2018-03-27 15:02:04 +00:00
}, {
'url': 'https://videakid.hu/videok/origo/jarmuvek/supercars-elozes-jAHDWfWSJH5XuFhH',
'only_matching': True,
}, {
'url': 'https://videakid.hu/player?v=8YfIAjxwWGwT8HVQ',
'only_matching': True,
}, {
'url': 'https://videakid.hu/player/v/8YfIAjxwWGwT8HVQ?autoplay=1',
'only_matching': True,
2016-11-03 11:53:45 +00:00
}]
2020-11-26 11:56:49 +00:00
_STATIC_SECRET = 'xHb0ZvME5q8CBcoQi6AngerDu3FGO9fkUlwPmLVY_RTzj2hJIS4NasXWKy1td7p'
2016-11-03 11:53:45 +00:00
2024-02-29 14:12:37 +00:00
@classmethod
def _extract_urls(cls, webpage):
def yield_urls():
for pattern in cls._EMBED_REGEX:
for m in re.finditer(pattern, webpage):
yield m.group('url')
return list(yield_urls())
2016-12-31 15:04:29 +00:00
2020-11-26 11:56:49 +00:00
@staticmethod
def rc4(cipher_text, key):
res = b''
2020-11-26 11:56:49 +00:00
key_len = len(key)
S = list(range(256))
j = 0
for i in range(256):
2020-11-26 11:56:49 +00:00
j = (j + S[i] + ord(key[i % key_len])) % 256
S[i], S[j] = S[j], S[i]
i = 0
j = 0
2020-11-26 11:56:49 +00:00
for m in range(len(cipher_text)):
i = (i + 1) % 256
j = (j + S[i]) % 256
S[i], S[j] = S[j], S[i]
k = S[(S[i] + S[j]) % 256]
2020-11-26 11:56:49 +00:00
res += compat_struct_pack('B', k ^ compat_ord(cipher_text[m]))
return res.decode('utf-8')
2016-11-03 11:53:45 +00:00
def _real_extract(self, url):
video_id = self._match_id(url)
video_page = self._download_webpage(url, video_id)
if 'videa.hu/player' in url:
player_url = url
player_page = video_page
else:
player_url = self._search_regex(
r'<iframe.*?src="(/player\?[^"]+)"', video_page, 'player url')
player_url = urljoin(url, player_url)
player_page = self._download_webpage(player_url, video_id)
2020-11-26 11:56:49 +00:00
nonce = self._search_regex(
r'_xt\s*=\s*"([^"]+)"', player_page, 'nonce')
l = nonce[:32]
s = nonce[32:]
result = ''
for i in range(0, 32):
2020-11-26 11:56:49 +00:00
result += s[i - (self._STATIC_SECRET.index(l[i]) - 31)]
2024-02-29 14:12:37 +00:00
query = parse_qs(player_url)
random_seed = ''.join(compat_random_choices(string.ascii_letters + string.digits, k=8))
2020-11-26 11:56:49 +00:00
query['_s'] = random_seed
query['_t'] = result[:16]
b64_info, handle = self._download_webpage_handle(
'http://videa.hu/player/xml', video_id, query=query)
2020-11-26 11:56:49 +00:00
if b64_info.startswith('<?xml'):
info = self._parse_xml(b64_info, video_id)
else:
key = result[16:] + random_seed + handle.headers['x-videa-xs']
info = self._parse_xml(self.rc4(
compat_b64decode(b64_info), key), video_id)
video = xpath_element(info, './video', 'video')
if video is None:
2020-11-26 11:56:49 +00:00
raise ExtractorError(xpath_element(
info, './error', fatal=True), expected=True)
sources = xpath_element(
info, './video_sources', 'sources', fatal=True)
hash_values = xpath_element(
info, './hash_values', 'hash values', fatal=False)
2016-11-03 11:53:45 +00:00
title = xpath_text(video, './title', fatal=True)
2016-11-03 11:53:45 +00:00
formats = []
for source in sources.findall('./video_source'):
source_url = source.text
2020-11-26 11:56:49 +00:00
source_name = source.get('name')
source_exp = source.get('exp')
if not (source_url and source_name):
continue
hash_value = (
xpath_text(hash_values, 'hash_value_' + source_name)
if hash_values is not None else None)
if hash_value and source_exp:
source_url = update_url_query(source_url, {
'md5': hash_value,
'expires': source_exp,
})
f = parse_codecs(source.get('codecs'))
f.update({
2020-11-26 11:56:49 +00:00
'url': self._proto_relative_url(source_url),
'ext': mimetype2ext(source.get('mimetype')) or 'mp4',
'format_id': source.get('name'),
'width': int_or_none(source.get('width')),
'height': int_or_none(source.get('height')),
})
formats.append(f)
self._sort_formats(formats)
2016-11-03 11:53:45 +00:00
2020-11-26 11:56:49 +00:00
thumbnail = self._proto_relative_url(xpath_text(video, './poster_src'))
2016-11-03 11:53:45 +00:00
age_limit = None
is_adult = xpath_text(video, './is_adult_content', default=None)
if is_adult:
age_limit = 18 if is_adult == '1' else 0
2016-11-03 11:53:45 +00:00
return {
'id': video_id,
'title': title,
'thumbnail': thumbnail,
2020-11-26 11:56:49 +00:00
'duration': int_or_none(xpath_text(video, './duration')),
'age_limit': age_limit,
'formats': formats,
}