yt-dlp/yt_dlp/extractor/dropbox.py

102 lines
4.6 KiB
Python
Raw Normal View History

import base64
2014-01-19 05:16:40 +00:00
import os.path
2014-01-18 15:15:53 +00:00
import re
import urllib.parse
2014-01-18 15:15:53 +00:00
from .common import InfoExtractor
from ..utils import (
ExtractorError,
update_url_query,
url_basename,
)
2014-01-18 15:15:53 +00:00
2014-01-19 05:14:24 +00:00
2014-01-19 04:50:26 +00:00
class DropboxIE(InfoExtractor):
_VALID_URL = r'https?://(?:www\.)?dropbox\.com/(?:(?:e/)?scl/fi|sh?)/(?P<id>\w+)'
2014-11-23 20:39:15 +00:00
_TESTS = [
{
'url': 'https://www.dropbox.com/s/nelirfsxnmcfbfh/youtube-dl%20test%20video%20%27%C3%A4%22BaW_jenozKc.mp4?dl=0',
2014-11-23 20:39:15 +00:00
'info_dict': {
'id': 'nelirfsxnmcfbfh',
'ext': 'mp4',
'title': 'youtube-dl test video \'ä"BaW_jenozKc',
},
2014-11-23 20:39:15 +00:00
}, {
'url': 'https://www.dropbox.com/s/nelirfsxnmcfbfh',
'only_matching': True,
}, {
'url': 'https://www.dropbox.com/sh/2mgpiuq7kv8nqdf/AABy-fW4dkydT4GmWi2mdOUDa?dl=0&preview=Drone+Shot.mp4',
'only_matching': True,
}, {
'url': 'https://www.dropbox.com/scl/fi/r2kd2skcy5ylbbta5y1pz/DJI_0003.MP4?dl=0&rlkey=wcdgqangn7t3lnmmv6li9mu9h',
'only_matching': True,
}, {
'url': 'https://www.dropbox.com/e/scl/fi/r2kd2skcy5ylbbta5y1pz/DJI_0003.MP4?dl=0&rlkey=wcdgqangn7t3lnmmv6li9mu9h',
2014-11-23 20:39:15 +00:00
'only_matching': True,
},
]
2014-01-19 05:14:24 +00:00
def _real_extract(self, url):
mobj = self._match_valid_url(url)
2014-01-19 05:14:24 +00:00
video_id = mobj.group('id')
webpage = self._download_webpage(url, video_id)
fn = urllib.parse.unquote(url_basename(url))
title = os.path.splitext(fn)[0]
password = self.get_param('videopassword')
2024-08-13 12:50:58 +00:00
thumbnail = None
for encoded in reversed(re.findall(r'registerStreamedPrefetch\s*\(\s*"[\w/+=]+"\s*,\s*"([\w/+=]+)"', webpage)):
part = base64.b64decode(encoded).decode('utf-8', 'ignore')
if 'sm/password' in part:
webpage = self._download_webpage(
'https://www.dropbox.com/sm/password?' + part.split('?')[1], video_id)
if (self._og_search_title(webpage) == 'Dropbox - Password Required'
or 'Enter the password for this link' in webpage):
if password:
content_id = self._search_regex(r'content_id=(.*?)["\']', webpage, 'content_id')
2024-08-13 12:50:58 +00:00
payload = f'is_xhr=true&t={self._get_cookies("https://www.dropbox.com").get("t").value}&content_id={content_id}&password={password}&url={url.replace("https://www.dropbox.com", "")}'
response = self._download_json(
'https://www.dropbox.com/sm/auth', video_id, 'POSTing video password', data=payload.encode(),
headers={'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'})
if response.get('status') != 'authed':
raise ExtractorError('Authentication failed!', expected=True)
webpage = self._download_webpage(url, video_id)
elif self._get_cookies('https://dropbox.com').get('sm_auth'):
webpage = self._download_webpage(url, video_id)
else:
raise ExtractorError('Password protected video, use --video-password <password>', expected=True)
formats, subtitles, has_anonymous_download = [], {}, False
for encoded in reversed(re.findall(r'registerStreamedPrefetch\s*\(\s*"[\w/+=]+"\s*,\s*"([\w/+=]+)"', webpage)):
decoded = base64.b64decode(encoded).decode('utf-8', 'ignore')
if not has_anonymous_download:
has_anonymous_download = self._search_regex(
r'(anonymous:\tanonymous)', decoded, 'anonymous', default=False)
transcode_url = self._search_regex(
r'\n.(https://[^\x03\x08\x12\n]+\.m3u8)', decoded, 'transcode url', default=None)
if not transcode_url:
continue
formats, subtitles = self._extract_m3u8_formats_and_subtitles(transcode_url, video_id, 'mp4')
2024-08-13 12:50:58 +00:00
thumbnail = self._search_regex(
r'(https://www\.dropbox\.com/temp_thumb_from_token/c/(.*?)\?preserve_transparency=False&rlkey=(.*?)&secure_hash=&size=(.*?)&size_mode=4)', decoded, 'thumbnail url', default=None)
break
# downloads enabled we can get the original file
if has_anonymous_download:
formats.append({
'url': update_url_query(url, {'dl': '1'}),
'format_id': 'original',
'format_note': 'Original',
'quality': 1,
})
2014-01-19 05:14:24 +00:00
return {
'id': video_id,
2024-08-13 12:50:58 +00:00
'thumbnail': thumbnail,
2014-01-19 05:14:24 +00:00
'title': title,
'formats': formats,
'subtitles': subtitles,
2014-01-19 05:14:24 +00:00
}