]>
Commit | Line | Data |
---|---|---|
1 | import json | |
2 | ||
3 | from .common import InfoExtractor | |
4 | from ..utils import ( | |
5 | determine_ext, | |
6 | float_or_none, | |
7 | int_or_none, | |
8 | mimetype2ext, | |
9 | try_get, | |
10 | urljoin, | |
11 | ) | |
12 | ||
13 | ||
14 | class YandexDiskIE(InfoExtractor): | |
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>[^/?#&]+)''' | |
33 | ||
34 | _TESTS = [{ | |
35 | 'url': 'https://yadi.sk/i/VdOeDou8eZs6Y', | |
36 | 'md5': 'a4a8d52958c8fddcf9845935070402ae', | |
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 | }, | |
46 | 'expected_warnings': ['Unable to download JSON metadata'], | |
47 | }, { | |
48 | 'url': 'https://yadi.sk/d/h3WAXvDS3Li3Ce', | |
49 | 'only_matching': True, | |
50 | }, { | |
51 | 'url': 'https://yadi.sk/public?hash=5DZ296JK9GWCLp02f6jrObjnctjRxMs8L6%2B%2FuhNqk38%3D', | |
52 | 'only_matching': True, | |
53 | }] | |
54 | ||
55 | def _real_extract(self, url): | |
56 | domain, video_id = self._match_valid_url(url).groups() | |
57 | ||
58 | webpage = self._download_webpage(url, video_id) | |
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') | |
97 | ||
98 | formats = [] | |
99 | if source_url: | |
100 | formats.append({ | |
101 | 'url': source_url, | |
102 | 'format_id': 'source', | |
103 | 'ext': determine_ext(title, meta.get('ext') or mimetype2ext(meta.get('mime_type')) or 'mp4'), | |
104 | 'quality': 1, | |
105 | 'filesize': int_or_none(meta.get('size')), | |
106 | }) | |
107 | ||
108 | for video in (video_streams.get('videos') or []): | |
109 | format_url = video.get('url') | |
110 | if not format_url: | |
111 | continue | |
112 | if video.get('dimension') == 'adaptive': | |
113 | formats.extend(self._extract_m3u8_formats( | |
114 | format_url, video_id, 'mp4', 'm3u8_native', | |
115 | m3u8_id='hls', fatal=False)) | |
116 | else: | |
117 | size = video.get('size') or {} | |
118 | height = int_or_none(size.get('height')) | |
119 | format_id = 'hls' | |
120 | if height: | |
121 | format_id += f'-{height}p' | |
122 | formats.append({ | |
123 | 'ext': 'mp4', | |
124 | 'format_id': format_id, | |
125 | 'height': height, | |
126 | 'protocol': 'm3u8_native', | |
127 | 'url': format_url, | |
128 | 'width': int_or_none(size.get('width')), | |
129 | }) | |
130 | ||
131 | uid = resource.get('uid') | |
132 | display_name = try_get(store, lambda x: x['users'][uid]['displayName']) | |
133 | ||
134 | return { | |
135 | 'id': video_id, | |
136 | 'title': title, | |
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')), | |
141 | 'formats': formats, | |
142 | } |