2021-01-01 12:26:37 +00:00
|
|
|
import json
|
2024-08-13 20:12:31 +00:00
|
|
|
import urllib.parse
|
2021-01-01 12:26:37 +00:00
|
|
|
|
2017-07-29 08:10:19 +00:00
|
|
|
from .common import InfoExtractor
|
|
|
|
from ..utils import (
|
2024-08-13 20:12:31 +00:00
|
|
|
ExtractorError,
|
2017-07-29 08:10:19 +00:00
|
|
|
determine_ext,
|
|
|
|
float_or_none,
|
|
|
|
int_or_none,
|
2024-07-01 22:51:27 +00:00
|
|
|
join_nonempty,
|
2021-01-01 12:26:37 +00:00
|
|
|
mimetype2ext,
|
2024-08-13 20:12:31 +00:00
|
|
|
traverse_obj,
|
2017-07-29 08:10:19 +00:00
|
|
|
try_get,
|
2021-01-01 12:26:37 +00:00
|
|
|
urljoin,
|
2017-07-29 08:10:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class YandexDiskIE(InfoExtractor):
|
2021-01-01 12:26:37 +00:00
|
|
|
_VALID_URL = r'''(?x)https?://
|
|
|
|
(?P<domain>
|
|
|
|
yadi\.sk|
|
|
|
|
disk\.yandex\.
|
|
|
|
(?:
|
|
|
|
az|
|
|
|
|
by|
|
|
|
|
co(?:m(?:\.(?:am|ge|tr))?|\.il)|
|
|
|
|
ee|
|
|
|
|
fr|
|
|
|
|
k[gz]|
|
|
|
|
l[tv]|
|
|
|
|
md|
|
|
|
|
t[jm]|
|
|
|
|
u[az]|
|
|
|
|
ru
|
|
|
|
)
|
|
|
|
)/(?:[di]/|public.*?\bhash=)(?P<id>[^/?#&]+)'''
|
2017-07-29 08:10:19 +00:00
|
|
|
|
2017-08-04 17:59:07 +00:00
|
|
|
_TESTS = [{
|
2017-07-29 08:10:19 +00:00
|
|
|
'url': 'https://yadi.sk/i/VdOeDou8eZs6Y',
|
2021-01-01 12:26:37 +00:00
|
|
|
'md5': 'a4a8d52958c8fddcf9845935070402ae',
|
2017-07-29 08:10:19 +00:00
|
|
|
'info_dict': {
|
|
|
|
'id': 'VdOeDou8eZs6Y',
|
|
|
|
'ext': 'mp4',
|
|
|
|
'title': '4.mp4',
|
|
|
|
'duration': 168.6,
|
|
|
|
'uploader': 'y.botova',
|
|
|
|
'uploader_id': '300043621',
|
|
|
|
'view_count': int,
|
|
|
|
},
|
2021-01-01 12:26:37 +00:00
|
|
|
'expected_warnings': ['Unable to download JSON metadata'],
|
2017-08-04 17:59:07 +00:00
|
|
|
}, {
|
|
|
|
'url': 'https://yadi.sk/d/h3WAXvDS3Li3Ce',
|
|
|
|
'only_matching': True,
|
2021-01-01 12:26:37 +00:00
|
|
|
}, {
|
|
|
|
'url': 'https://yadi.sk/public?hash=5DZ296JK9GWCLp02f6jrObjnctjRxMs8L6%2B%2FuhNqk38%3D',
|
|
|
|
'only_matching': True,
|
2017-08-04 17:59:07 +00:00
|
|
|
}]
|
2017-07-29 08:10:19 +00:00
|
|
|
|
|
|
|
def _real_extract(self, url):
|
2021-08-19 01:41:24 +00:00
|
|
|
domain, video_id = self._match_valid_url(url).groups()
|
2017-07-29 08:10:19 +00:00
|
|
|
|
|
|
|
webpage = self._download_webpage(url, video_id)
|
2021-01-01 12:26:37 +00:00
|
|
|
store = self._parse_json(self._search_regex(
|
|
|
|
r'<script[^>]+id="store-prefetch"[^>]*>\s*({.+?})\s*</script>',
|
|
|
|
webpage, 'store'), video_id)
|
|
|
|
resource = store['resources'][store['rootResourceId']]
|
|
|
|
|
2024-08-13 20:12:31 +00:00
|
|
|
if store['rootResourceId'] == 'password-protected':
|
|
|
|
data = {
|
|
|
|
'hash': resource['hash'],
|
|
|
|
'password': self.get_param('videopassword', default=''),
|
|
|
|
'sk': traverse_obj(store, ('environment', 'sk'))}
|
|
|
|
json_string = json.dumps(data, separators=(',', ':'))
|
|
|
|
url_encoded_string = urllib.parse.quote(json_string, safe='')
|
|
|
|
data_bytes = url_encoded_string.encode('utf-8')
|
|
|
|
token = (self._download_json(
|
|
|
|
'https://disk.yandex.ru/public/api/check-password',
|
|
|
|
video_id, data=data_bytes, fatal=False,
|
|
|
|
headers={
|
|
|
|
'Accept': '*/*',
|
|
|
|
'Cache-Control': 'no-cache',
|
|
|
|
'Connection': 'keep-alive',
|
|
|
|
'Content-Type': 'text/plain',
|
|
|
|
'Origin': 'https://disk.yandex.ru',
|
|
|
|
'Pragma': 'no-cache',
|
|
|
|
'Referer': url,
|
|
|
|
'Sec-Fetch-Dest': 'empty',
|
|
|
|
'Sec-Fetch-Mode': 'cors',
|
|
|
|
'Sec-Fetch-Site': 'same-origin',
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
|
|
'X-Retpath-Y': url}) or {}).get('token') or {}
|
|
|
|
if not token:
|
|
|
|
raise ExtractorError('Password incorrect!', expected=True)
|
|
|
|
self._set_cookie('disk.yandex.ru', name='passToken', value=token)
|
|
|
|
webpage = self._download_webpage(url, video_id)
|
|
|
|
store = self._parse_json(self._search_regex(
|
|
|
|
r'<script[^>]+id="store-prefetch"[^>]*>\s*({.+?})\s*</script>',
|
|
|
|
webpage, 'store'), video_id)
|
|
|
|
resource = store['resources'][store['rootResourceId']]
|
|
|
|
|
|
|
|
thumbnail = self._og_search_property('image', webpage)
|
2021-01-01 12:26:37 +00:00
|
|
|
title = resource['name']
|
|
|
|
meta = resource.get('meta') or {}
|
|
|
|
|
|
|
|
public_url = meta.get('short_url')
|
|
|
|
if public_url:
|
|
|
|
video_id = self._match_id(public_url)
|
|
|
|
|
|
|
|
source_url = (self._download_json(
|
|
|
|
'https://cloud-api.yandex.net/v1/disk/public/resources/download',
|
|
|
|
video_id, query={'public_key': url}, fatal=False) or {}).get('href')
|
|
|
|
video_streams = resource.get('videoStreams') or {}
|
|
|
|
video_hash = resource.get('hash') or url
|
|
|
|
environment = store.get('environment') or {}
|
|
|
|
sk = environment.get('sk')
|
|
|
|
yandexuid = environment.get('yandexuid')
|
|
|
|
if sk and yandexuid and not (source_url and video_streams):
|
|
|
|
self._set_cookie(domain, 'yandexuid', yandexuid)
|
|
|
|
|
|
|
|
def call_api(action):
|
|
|
|
return (self._download_json(
|
|
|
|
urljoin(url, '/public/api/') + action, video_id, data=json.dumps({
|
|
|
|
'hash': video_hash,
|
|
|
|
'sk': sk,
|
|
|
|
}).encode(), headers={
|
|
|
|
'Content-Type': 'text/plain',
|
|
|
|
}, fatal=False) or {}).get('data') or {}
|
|
|
|
if not source_url:
|
|
|
|
# TODO: figure out how to detect if download limit has
|
|
|
|
# been reached and then avoid unnecessary source format
|
|
|
|
# extraction requests
|
|
|
|
source_url = call_api('download-url').get('url')
|
|
|
|
if not video_streams:
|
|
|
|
video_streams = call_api('get-video-streams')
|
2017-07-29 08:10:19 +00:00
|
|
|
|
|
|
|
formats = []
|
|
|
|
if source_url:
|
|
|
|
formats.append({
|
|
|
|
'url': source_url,
|
|
|
|
'format_id': 'source',
|
2021-01-01 12:26:37 +00:00
|
|
|
'ext': determine_ext(title, meta.get('ext') or mimetype2ext(meta.get('mime_type')) or 'mp4'),
|
2017-07-29 08:10:19 +00:00
|
|
|
'quality': 1,
|
2024-06-11 23:09:58 +00:00
|
|
|
'filesize': int_or_none(meta.get('size')),
|
2017-07-29 08:10:19 +00:00
|
|
|
})
|
2021-01-01 12:26:37 +00:00
|
|
|
|
|
|
|
for video in (video_streams.get('videos') or []):
|
2017-07-29 08:10:19 +00:00
|
|
|
format_url = video.get('url')
|
|
|
|
if not format_url:
|
|
|
|
continue
|
2021-01-01 12:26:37 +00:00
|
|
|
if video.get('dimension') == 'adaptive':
|
2017-07-29 08:10:19 +00:00
|
|
|
formats.extend(self._extract_m3u8_formats(
|
2021-01-01 12:26:37 +00:00
|
|
|
format_url, video_id, 'mp4', 'm3u8_native',
|
2017-07-29 08:10:19 +00:00
|
|
|
m3u8_id='hls', fatal=False))
|
|
|
|
else:
|
2021-01-01 12:26:37 +00:00
|
|
|
size = video.get('size') or {}
|
|
|
|
height = int_or_none(size.get('height'))
|
2017-07-29 08:10:19 +00:00
|
|
|
formats.append({
|
2021-01-01 12:26:37 +00:00
|
|
|
'ext': 'mp4',
|
2024-07-01 22:51:27 +00:00
|
|
|
'format_id': join_nonempty('hls', height and f'{height}p'),
|
2021-01-01 12:26:37 +00:00
|
|
|
'height': height,
|
|
|
|
'protocol': 'm3u8_native',
|
2017-07-29 08:10:19 +00:00
|
|
|
'url': format_url,
|
2021-01-01 12:26:37 +00:00
|
|
|
'width': int_or_none(size.get('width')),
|
2017-07-29 08:10:19 +00:00
|
|
|
})
|
|
|
|
|
2021-01-01 12:26:37 +00:00
|
|
|
uid = resource.get('uid')
|
|
|
|
display_name = try_get(store, lambda x: x['users'][uid]['displayName'])
|
2017-07-29 08:10:19 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
'id': video_id,
|
|
|
|
'title': title,
|
2024-08-13 20:12:31 +00:00
|
|
|
'thumbnail': thumbnail,
|
2021-01-01 12:26:37 +00:00
|
|
|
'duration': float_or_none(video_streams.get('duration'), 1000),
|
|
|
|
'uploader': display_name,
|
|
|
|
'uploader_id': uid,
|
|
|
|
'view_count': int_or_none(meta.get('views_counter')),
|
2017-07-29 08:10:19 +00:00
|
|
|
'formats': formats,
|
|
|
|
}
|