]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/dropbox.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / dropbox.py
CommitLineData
4cf393bb 1import os.path
8da53135 2import re
3
4from .common import InfoExtractor
8c25f81b 5from ..compat import compat_urllib_parse_unquote
7141ced5 6from ..utils import (
7 ExtractorError,
8 traverse_obj,
9 try_get,
10 url_basename,
11)
8da53135 12
ce4e242a 13
6b79f40c 14class DropboxIE(InfoExtractor):
7bd4b422 15 _VALID_URL = r'https?://(?:www\.)?dropbox[.]com/sh?/(?P<id>[a-zA-Z0-9]{15})/.*'
9e1a5b84
JW
16 _TESTS = [
17 {
7a5c1cfe 18 'url': 'https://www.dropbox.com/s/nelirfsxnmcfbfh/youtube-dl%20test%20video%20%27%C3%A4%22BaW_jenozKc.mp4?dl=0',
9e1a5b84
JW
19 'info_dict': {
20 'id': 'nelirfsxnmcfbfh',
21 'ext': 'mp4',
7a5c1cfe 22 'title': 'youtube-dl test video \'ä"BaW_jenozKc'
9e1a5b84
JW
23 }
24 }, {
25 'url': 'https://www.dropbox.com/sh/662glsejgzoj9sr/AAByil3FGH9KFNZ13e08eSa1a/Pregame%20Ceremony%20Program%20PA%2020140518.m4v',
26 'only_matching': True,
27 },
7bd4b422 28 ]
ce4e242a
PH
29
30 def _real_extract(self, url):
5ad28e7f 31 mobj = self._match_valid_url(url)
ce4e242a 32 video_id = mobj.group('id')
7141ced5 33 webpage = self._download_webpage(url, video_id)
7bd4b422 34 fn = compat_urllib_parse_unquote(url_basename(url))
264a7044 35 title = os.path.splitext(fn)[0]
7141ced5 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
f254d6cc 56 info_json = self._search_json(r'InitReact\.mountComponent\(.*?,', webpage, 'mountComponent', video_id,
8b7fb8b6 57 contains_pattern=r'{.+?"preview".+?}', end_pattern=r'\)')['props']
7141ced5 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})
ce4e242a
PH
66
67 return {
68 'id': video_id,
69 'title': title,
7141ced5 70 'formats': formats,
71 'subtitles': subtitles
ce4e242a 72 }