]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/yandexdisk.py
[ie/orf:on] Improve extraction (#9677)
[yt-dlp.git] / yt_dlp / extractor / yandexdisk.py
CommitLineData
29f7c58a 1import json
29f7c58a 2
cbbe6663 3from .common import InfoExtractor
cbbe6663
S
4from ..utils import (
5 determine_ext,
6 float_or_none,
7 int_or_none,
29f7c58a 8 mimetype2ext,
cbbe6663 9 try_get,
29f7c58a 10 urljoin,
cbbe6663
S
11)
12
13
14class YandexDiskIE(InfoExtractor):
29f7c58a 15 _VALID_URL = r'''(?x)https?://
16 (?P<domain>
17 yadi\.sk|
18 disk\.yandex\.
19 (?:
20 az|
21 by|
22 co(?:m(?:\.(?:am|ge|tr))?|\.il)|
23 ee|
24 fr|
25 k[gz]|
26 l[tv]|
27 md|
28 t[jm]|
29 u[az]|
30 ru
31 )
32 )/(?:[di]/|public.*?\bhash=)(?P<id>[^/?#&]+)'''
cbbe6663 33
8519b88f 34 _TESTS = [{
cbbe6663 35 'url': 'https://yadi.sk/i/VdOeDou8eZs6Y',
29f7c58a 36 'md5': 'a4a8d52958c8fddcf9845935070402ae',
cbbe6663
S
37 'info_dict': {
38 'id': 'VdOeDou8eZs6Y',
39 'ext': 'mp4',
40 'title': '4.mp4',
41 'duration': 168.6,
42 'uploader': 'y.botova',
43 'uploader_id': '300043621',
44 'view_count': int,
45 },
29f7c58a 46 'expected_warnings': ['Unable to download JSON metadata'],
8519b88f
S
47 }, {
48 'url': 'https://yadi.sk/d/h3WAXvDS3Li3Ce',
49 'only_matching': True,
29f7c58a 50 }, {
51 'url': 'https://yadi.sk/public?hash=5DZ296JK9GWCLp02f6jrObjnctjRxMs8L6%2B%2FuhNqk38%3D',
52 'only_matching': True,
8519b88f 53 }]
cbbe6663
S
54
55 def _real_extract(self, url):
5ad28e7f 56 domain, video_id = self._match_valid_url(url).groups()
cbbe6663
S
57
58 webpage = self._download_webpage(url, video_id)
29f7c58a 59 store = self._parse_json(self._search_regex(
60 r'<script[^>]+id="store-prefetch"[^>]*>\s*({.+?})\s*</script>',
61 webpage, 'store'), video_id)
62 resource = store['resources'][store['rootResourceId']]
63
64 title = resource['name']
65 meta = resource.get('meta') or {}
66
67 public_url = meta.get('short_url')
68 if public_url:
69 video_id = self._match_id(public_url)
70
71 source_url = (self._download_json(
72 'https://cloud-api.yandex.net/v1/disk/public/resources/download',
73 video_id, query={'public_key': url}, fatal=False) or {}).get('href')
74 video_streams = resource.get('videoStreams') or {}
75 video_hash = resource.get('hash') or url
76 environment = store.get('environment') or {}
77 sk = environment.get('sk')
78 yandexuid = environment.get('yandexuid')
79 if sk and yandexuid and not (source_url and video_streams):
80 self._set_cookie(domain, 'yandexuid', yandexuid)
81
82 def call_api(action):
83 return (self._download_json(
84 urljoin(url, '/public/api/') + action, video_id, data=json.dumps({
85 'hash': video_hash,
86 'sk': sk,
87 }).encode(), headers={
88 'Content-Type': 'text/plain',
89 }, fatal=False) or {}).get('data') or {}
90 if not source_url:
91 # TODO: figure out how to detect if download limit has
92 # been reached and then avoid unnecessary source format
93 # extraction requests
94 source_url = call_api('download-url').get('url')
95 if not video_streams:
96 video_streams = call_api('get-video-streams')
cbbe6663
S
97
98 formats = []
99 if source_url:
100 formats.append({
101 'url': source_url,
102 'format_id': 'source',
29f7c58a 103 'ext': determine_ext(title, meta.get('ext') or mimetype2ext(meta.get('mime_type')) or 'mp4'),
cbbe6663 104 'quality': 1,
29f7c58a 105 'filesize': int_or_none(meta.get('size'))
cbbe6663 106 })
29f7c58a 107
108 for video in (video_streams.get('videos') or []):
cbbe6663
S
109 format_url = video.get('url')
110 if not format_url:
111 continue
29f7c58a 112 if video.get('dimension') == 'adaptive':
cbbe6663 113 formats.extend(self._extract_m3u8_formats(
29f7c58a 114 format_url, video_id, 'mp4', 'm3u8_native',
cbbe6663
S
115 m3u8_id='hls', fatal=False))
116 else:
29f7c58a 117 size = video.get('size') or {}
118 height = int_or_none(size.get('height'))
119 format_id = 'hls'
120 if height:
121 format_id += '-%dp' % height
cbbe6663 122 formats.append({
29f7c58a 123 'ext': 'mp4',
124 'format_id': format_id,
125 'height': height,
126 'protocol': 'm3u8_native',
cbbe6663 127 'url': format_url,
29f7c58a 128 'width': int_or_none(size.get('width')),
cbbe6663 129 })
cbbe6663 130
29f7c58a 131 uid = resource.get('uid')
132 display_name = try_get(store, lambda x: x['users'][uid]['displayName'])
cbbe6663
S
133
134 return {
135 'id': video_id,
136 'title': title,
29f7c58a 137 'duration': float_or_none(video_streams.get('duration'), 1000),
138 'uploader': display_name,
139 'uploader_id': uid,
140 'view_count': int_or_none(meta.get('views_counter')),
cbbe6663
S
141 'formats': formats,
142 }