dropbox.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import base64
  2. import os.path
  3. import re
  4. import urllib.parse
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. ExtractorError,
  8. update_url_query,
  9. url_basename,
  10. )
  11. class DropboxIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?dropbox\.com/(?:(?:e/)?scl/fi|sh?)/(?P<id>\w+)'
  13. _TESTS = [
  14. {
  15. 'url': 'https://www.dropbox.com/s/nelirfsxnmcfbfh/youtube-dl%20test%20video%20%27%C3%A4%22BaW_jenozKc.mp4?dl=0',
  16. 'info_dict': {
  17. 'id': 'nelirfsxnmcfbfh',
  18. 'ext': 'mp4',
  19. 'title': 'youtube-dl test video \'ä"BaW_jenozKc',
  20. },
  21. }, {
  22. 'url': 'https://www.dropbox.com/s/nelirfsxnmcfbfh',
  23. 'only_matching': True,
  24. }, {
  25. 'url': 'https://www.dropbox.com/sh/2mgpiuq7kv8nqdf/AABy-fW4dkydT4GmWi2mdOUDa?dl=0&preview=Drone+Shot.mp4',
  26. 'only_matching': True,
  27. }, {
  28. 'url': 'https://www.dropbox.com/scl/fi/r2kd2skcy5ylbbta5y1pz/DJI_0003.MP4?dl=0&rlkey=wcdgqangn7t3lnmmv6li9mu9h',
  29. 'only_matching': True,
  30. }, {
  31. 'url': 'https://www.dropbox.com/e/scl/fi/r2kd2skcy5ylbbta5y1pz/DJI_0003.MP4?dl=0&rlkey=wcdgqangn7t3lnmmv6li9mu9h',
  32. 'only_matching': True,
  33. },
  34. ]
  35. def _real_extract(self, url):
  36. mobj = self._match_valid_url(url)
  37. video_id = mobj.group('id')
  38. webpage = self._download_webpage(url, video_id)
  39. fn = urllib.parse.unquote(url_basename(url))
  40. title = os.path.splitext(fn)[0]
  41. password = self.get_param('videopassword')
  42. if (self._og_search_title(webpage) == 'Dropbox - Password Required'
  43. or 'Enter the password for this link' in webpage):
  44. if password:
  45. content_id = self._search_regex(r'content_id=(.*?)["\']', webpage, 'content_id')
  46. payload = f'is_xhr=true&t={self._get_cookies("https://www.dropbox.com").get("t").value}&content_id={content_id}&password={password}&url={url}'
  47. response = self._download_json(
  48. 'https://www.dropbox.com/sm/auth', video_id, 'POSTing video password', data=payload.encode(),
  49. headers={'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'})
  50. if response.get('status') != 'authed':
  51. raise ExtractorError('Authentication failed!', expected=True)
  52. webpage = self._download_webpage(url, video_id)
  53. elif self._get_cookies('https://dropbox.com').get('sm_auth'):
  54. webpage = self._download_webpage(url, video_id)
  55. else:
  56. raise ExtractorError('Password protected video, use --video-password <password>', expected=True)
  57. formats, subtitles, has_anonymous_download = [], {}, False
  58. for encoded in reversed(re.findall(r'registerStreamedPrefetch\s*\(\s*"[\w/+=]+"\s*,\s*"([\w/+=]+)"', webpage)):
  59. decoded = base64.b64decode(encoded).decode('utf-8', 'ignore')
  60. if not has_anonymous_download:
  61. has_anonymous_download = self._search_regex(
  62. r'(anonymous:\tanonymous)', decoded, 'anonymous', default=False)
  63. transcode_url = self._search_regex(
  64. r'\n.(https://[^\x03\x08\x12\n]+\.m3u8)', decoded, 'transcode url', default=None)
  65. if not transcode_url:
  66. continue
  67. formats, subtitles = self._extract_m3u8_formats_and_subtitles(transcode_url, video_id, 'mp4')
  68. break
  69. # downloads enabled we can get the original file
  70. if has_anonymous_download:
  71. formats.append({
  72. 'url': update_url_query(url, {'dl': '1'}),
  73. 'format_id': 'original',
  74. 'format_note': 'Original',
  75. 'quality': 1,
  76. })
  77. return {
  78. 'id': video_id,
  79. 'title': title,
  80. 'formats': formats,
  81. 'subtitles': subtitles,
  82. }