Download part of Dropbox folder #39

Closed
opened 2024-04-06 02:29:02 +02:00 by Benjamin_Loison · 5 comments

download_dropbox.py:

#!/usr/bin/python3

import dropbox
import urllib.request
from tqdm import tqdm

# https://www.dropbox.com/developers/apps
# `Choose the type of access you need`:
# - `App folder`
# `Permissions`:
# - `files.metadata.read`
# Then `Settings` > `Generate access token`.
ACCESS_TOKEN = 'CENSORED'
# May have 2 random parts delimited by `/` or just have a second part being `h`.
FOLDER = 'CENSORED'
RLKEY = 'CENSORED'

dbx = dropbox.Dropbox(ACCESS_TOKEN)

BASE_URL = f'https://www.dropbox.com/scl/fo/{FOLDER}'
url = f'{BASE_URL}?rlkey={RLKEY}'
shared_link = dropbox.files.SharedLink(url = url)

result = dbx.files_list_folder(path = '', shared_link=shared_link)

for entry in tqdm(result.entries):
    entryName = entry.name
    if entryName.endswith('.ARW'):
        downloadUrl = f'{BASE_URL}/{entryName}?rlkey={RLKEY}&dl=1'
        # Avoid file injection.
        fileName = ''.join(character for character in entryName if (character.isalnum() or character == '.'))
        urllib.request.urlretrieve(downloadUrl, fileName)

Related to Benjamin_Loison/wget/issues/1.

`download_dropbox.py`: ```py #!/usr/bin/python3 import dropbox import urllib.request from tqdm import tqdm # https://www.dropbox.com/developers/apps # `Choose the type of access you need`: # - `App folder` # `Permissions`: # - `files.metadata.read` # Then `Settings` > `Generate access token`. ACCESS_TOKEN = 'CENSORED' # May have 2 random parts delimited by `/` or just have a second part being `h`. FOLDER = 'CENSORED' RLKEY = 'CENSORED' dbx = dropbox.Dropbox(ACCESS_TOKEN) BASE_URL = f'https://www.dropbox.com/scl/fo/{FOLDER}' url = f'{BASE_URL}?rlkey={RLKEY}' shared_link = dropbox.files.SharedLink(url = url) result = dbx.files_list_folder(path = '', shared_link=shared_link) for entry in tqdm(result.entries): entryName = entry.name if entryName.endswith('.ARW'): downloadUrl = f'{BASE_URL}/{entryName}?rlkey={RLKEY}&dl=1' # Avoid file injection. fileName = ''.join(character for character in entryName if (character.isalnum() or character == '.')) urllib.request.urlretrieve(downloadUrl, fileName) ``` Related to [Benjamin_Loison/wget/issues/1](https://codeberg.org/Benjamin_Loison/wget/issues/1).
Benjamin_Loison added the
enhancement
medium priority
medium
labels 2024-04-06 02:29:46 +02:00
Author
Owner

Above script seems limited to 500 entries. To download all Dropbox files can use:

wget https://CENSORED.dl.dropboxusercontent.com/zip_download_get/CENSORED
Above script seems limited to 500 entries. To download all Dropbox files can use: ```bash wget https://CENSORED.dl.dropboxusercontent.com/zip_download_get/CENSORED ```
Author
Owner

Related to issues/59#issue-1243.

Related to [issues/59#issue-1243](https://gitea.lemnoslife.com/Benjamin_Loison/Robust_image_source_identification_on_modern_smartphones/issues/59#issue-1243).
Author
Owner
help(dbx.files_list_folder)
result = dbx.files_list_folder(path = '', shared_link=shared_link, limit = 2_000)
Traceback (most recent call last):
  File "./download_dropbox.py", line 25, in <module>
    result = dbx.files_list_folder(path = '', shared_link=shared_link, limit = 10 ** 6)
  File "/home/bloison/.local/lib/python3.6/site-packages/dropbox/base.py", line 2148, in files_list_folder
    include_non_downloadable_files)
  File "/home/bloison/.local/lib/python3.6/site-packages/dropbox/files.py", line 3678, in __init__
    self.limit = limit
  File "/home/bloison/.local/lib/python3.6/site-packages/stone/backends/python_rsrc/stone_base.py", line 81, in __set__
    value = self.validator.validate(value)
  File "/home/bloison/.local/lib/python3.6/site-packages/stone/backends/python_rsrc/stone_validators.py", line 652, in validate
    return self.validator.validate(val)
  File "/home/bloison/.local/lib/python3.6/site-packages/stone/backends/python_rsrc/stone_validators.py", line 172, in validate
    % (val, self.minimum, self.maximum))
stone.backends.python_rsrc.stone_validators.ValidationError: 1000000 is not within range [1, 2000]
print(len(result.entries))
1282
```python help(dbx.files_list_folder) ``` ```python result = dbx.files_list_folder(path = '', shared_link=shared_link, limit = 2_000) ``` ``` Traceback (most recent call last): File "./download_dropbox.py", line 25, in <module> result = dbx.files_list_folder(path = '', shared_link=shared_link, limit = 10 ** 6) File "/home/bloison/.local/lib/python3.6/site-packages/dropbox/base.py", line 2148, in files_list_folder include_non_downloadable_files) File "/home/bloison/.local/lib/python3.6/site-packages/dropbox/files.py", line 3678, in __init__ self.limit = limit File "/home/bloison/.local/lib/python3.6/site-packages/stone/backends/python_rsrc/stone_base.py", line 81, in __set__ value = self.validator.validate(value) File "/home/bloison/.local/lib/python3.6/site-packages/stone/backends/python_rsrc/stone_validators.py", line 652, in validate return self.validator.validate(val) File "/home/bloison/.local/lib/python3.6/site-packages/stone/backends/python_rsrc/stone_validators.py", line 172, in validate % (val, self.minimum, self.maximum)) stone.backends.python_rsrc.stone_validators.ValidationError: 1000000 is not within range [1, 2000] ``` ```python print(len(result.entries)) ``` ``` 1282 ```
Author
Owner
import re

INTERESTED_IN_FILES_REGEX = re.compile('DSC0(\d{4})\\.ARW')

# ...

        actualFileIndex = int(INTERESTED_IN_FILES_REGEX.match(entryName).group(1))
        if 3294 <= actualFileIndex and actualFileIndex <= 3552:
```python import re INTERESTED_IN_FILES_REGEX = re.compile('DSC0(\d{4})\\.ARW') # ... actualFileIndex = int(INTERESTED_IN_FILES_REGEX.match(entryName).group(1)) if 3294 <= actualFileIndex and actualFileIndex <= 3552: ```
Author
Owner
    #print(dbx.get_file_and_metadata(f'/{entryName}'))
    #dbx.sharing_get_shared_link_file(url=url, path=f'/{entryName}')
    #help(dbx.files_download_to_file)
    '''
    if isinstance(entry, dropbox.files.FileMetadata):
        print(entry)
        break
    '''
    #dbx.files_download_to_file(entryName, f'/{entryName}')
    #dbx.files_download_to_file(entryName, entry.id)
    #print(entry.path_lower)
    #print(dbx.files_download(entry.path_lower))
    #print(dbx.files_download(f'/{entryName}'))
    print(entry)
    #continue
```python #print(dbx.get_file_and_metadata(f'/{entryName}')) #dbx.sharing_get_shared_link_file(url=url, path=f'/{entryName}') #help(dbx.files_download_to_file) ''' if isinstance(entry, dropbox.files.FileMetadata): print(entry) break ''' #dbx.files_download_to_file(entryName, f'/{entryName}') #dbx.files_download_to_file(entryName, entry.id) #print(entry.path_lower) #print(dbx.files_download(entry.path_lower)) #print(dbx.files_download(f'/{entryName}')) print(entry) #continue ```
Sign in to join this conversation.
No description provided.