MaxMad
MaxMad

Reputation: 5

Script that downloads the most recently added image in a Dropbox directory

I am creating a project for my thesis and I am stuck trying to make a python script that downloads the most resent image from a Dropbox directory. The script I show below can download specific images that I ask but, how can I download the most recently added image in the /photos directory?

import dropbox
dbx = dropbox.Dropbox("ACCESS_TOKEN")

with open("photo1.jpg", "wb") as f:
    metadata, res = dbx.files_download(path="/photos/photo1.jpg")
    f.write(res.content)

Upvotes: 0

Views: 401

Answers (1)

Greg
Greg

Reputation: 16930

The Dropbox API doesn't offer a direct way to list the most recent file(s) in a particular path, so you'll need to use the files_list_folder and files_list_folder_continue methods to list all of the contents of the folder, and then sort through them to find the desired file, i.e., in this case, the one with the latest FileMetadata.server_modified.

Once you find the most recent file, you can then use its id or path_lower property as the path value when calling files_download to download the file content.

Upvotes: 1

Related Questions