]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/dropbox.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / dropbox.py
1 import os.path
2 import re
3
4 from .common import InfoExtractor
5 from ..compat import compat_urllib_parse_unquote
6 from ..utils import (
7 ExtractorError,
8 traverse_obj,
9 try_get,
10 url_basename,
11 )
12
13
14 class DropboxIE(InfoExtractor):
15 _VALID_URL = r'https?://(?:www\.)?dropbox[.]com/sh?/(?P<id>[a-zA-Z0-9]{15})/.*'
16 _TESTS = [
17 {
18 'url': 'https://www.dropbox.com/s/nelirfsxnmcfbfh/youtube-dl%20test%20video%20%27%C3%A4%22BaW_jenozKc.mp4?dl=0',
19 'info_dict': {
20 'id': 'nelirfsxnmcfbfh',
21 'ext': 'mp4',
22 'title': 'youtube-dl test video \'รค"BaW_jenozKc'
23 }
24 }, {
25 'url': 'https://www.dropbox.com/sh/662glsejgzoj9sr/AAByil3FGH9KFNZ13e08eSa1a/Pregame%20Ceremony%20Program%20PA%2020140518.m4v',
26 'only_matching': True,
27 },
28 ]
29
30 def _real_extract(self, url):
31 mobj = self._match_valid_url(url)
32 video_id = mobj.group('id')
33 webpage = self._download_webpage(url, video_id)
34 fn = compat_urllib_parse_unquote(url_basename(url))
35 title = os.path.splitext(fn)[0]
36
37 password = self.get_param('videopassword')
38 if (self._og_search_title(webpage) == 'Dropbox - Password Required'
39 or 'Enter the password for this link' in webpage):
40
41 if password:
42 content_id = self._search_regex(r'content_id=(.*?)["\']', webpage, 'content_id')
43 payload = f'is_xhr=true&t={self._get_cookies("https://www.dropbox.com").get("t").value}&content_id={content_id}&password={password}&url={url}'
44 response = self._download_json(
45 'https://www.dropbox.com/sm/auth', video_id, 'POSTing video password', data=payload.encode('UTF-8'),
46 headers={'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'})
47
48 if response.get('status') != 'authed':
49 raise ExtractorError('Authentication failed!', expected=True)
50 webpage = self._download_webpage(url, video_id)
51 elif self._get_cookies('https://dropbox.com').get('sm_auth'):
52 webpage = self._download_webpage(url, video_id)
53 else:
54 raise ExtractorError('Password protected video, use --video-password <password>', expected=True)
55
56 info_json = self._search_json(r'InitReact\.mountComponent\(.*?,', webpage, 'mountComponent', video_id,
57 contains_pattern=r'{.+?"preview".+?}', end_pattern=r'\)')['props']
58 transcode_url = traverse_obj(info_json, ((None, 'preview'), 'file', 'preview', 'content', 'transcode_url'), get_all=False)
59 formats, subtitles = self._extract_m3u8_formats_and_subtitles(transcode_url, video_id)
60
61 # downloads enabled we can get the original file
62 if 'anonymous' in (try_get(info_json, lambda x: x['sharePermission']['canDownloadRoles']) or []):
63 video_url = re.sub(r'[?&]dl=0', '', url)
64 video_url += ('?' if '?' not in video_url else '&') + 'dl=1'
65 formats.append({'url': video_url, 'format_id': 'original', 'format_note': 'Original', 'quality': 1})
66
67 return {
68 'id': video_id,
69 'title': title,
70 'formats': formats,
71 'subtitles': subtitles
72 }