Reputation: 1917
I wanted to write the code that could download all the images in a specified folder and not the subfolders in Dropbox using the Dropbox API. So far I have written this
def download_cont(self, folderName):
fname = folderName
folder_metadata = self.client.metadata('/' + fname)
print folder_metadata
This is the metadata:
{u'size': u'0 bytes', u'hash': u'3fad7ce5537e0941f8768413cdb7b84d', u'bytes': 0, u'thumb_exists': False, u'rev': u'111c24338d', u'modified': u'Sun, 22 Dec 2013 02:09:41 +0000', u'path': u'/images', u'is_dir': True, u'icon': u'folder', u'root': u'dropbox', u'contents': [{u'size': u'56.2 KB', u'rev': u'131c24338d', u'thumb_exists': True, u'bytes': 57538, u'modified': u'Sun, 22 Dec 2013 02:16:34 +0000', u'mime_type': u'image/png', u'path': u'/images/296px-Manchester_United_FC_crest.svg.png', u'is_dir': False, u'icon': u'page_white_picture', u'root': u'dropbox', u'client_mtime': u'Sun, 22 Dec 2013 02:16:34 +0000', u'revision': 19}, {u'size': u'9.8 KB', u'rev': u'141c24338d', u'thumb_exists': True, u'bytes': 9999, u'modified': u'Sun, 22 Dec 2013 02:16:36 +0000', u'mime_type': u'image/jpeg', u'path': u'/images/images.jpg', u'is_dir': False, u'icon': u'page_white_picture', u'root': u'dropbox', u'client_mtime': u'Sun, 22 Dec 2013 02:16:36 +0000', u'revision': 20}, {u'size': u'77 KB', u'rev': u'151c24338d', u'thumb_exists': True, u'bytes': 78798, u'modified': u'Sun, 22 Dec 2013 02:16:39 +0000', u'mime_type': u'image/jpeg', u'path': u'/images/manchester-united-fc-3d.jpg', u'is_dir': False, u'icon': u'page_white_picture', u'root': u'dropbox', u'client_mtime': u'Sun, 22 Dec 2013 02:16:39 +0000', u'revision': 21}, {u'size': u'220.9 KB', u'rev': u'121c24338d', u'thumb_exists': True, u'bytes': 226209, u'modified': u'Sun, 22 Dec 2013 02:15:51 +0000', u'mime_type': u'image/jpeg', u'path': u'/images/manchester-united-plc-logo.jpg', u'is_dir': False, u'icon': u'page_white_picture', u'root': u'dropbox', u'client_mtime': u'Sun, 22 Dec 2013 02:15:51 +0000', u'revision': 18}, {u'size': u'0 bytes', u'rev': u'161c24338d', u'thumb_exists': False, u'bytes': 0, u'modified': u'Sun, 22 Dec 2013 02:16:53 +0000', u'path': u'/images/sub', u'is_dir': True, u'icon': u'folder', u'root': u'dropbox', u'revision': 22}], u'revision': 17}
From what I think I'll have to traverse the JSON object and download each file using the 'contents'
in the metadata but I am not sure how to do this. Please help.
Upvotes: 1
Views: 759
Reputation: 60143
You're right. The contents
member of the metadata will tell you about the files (and subfolders) in the folder you listed. Each entry will have a path
member that tells you its full path and a is_dir
member that tells you if the entry is a directory (versus a file). Here's some code that uses Python's list comprehension to get all the paths of the files within the specified folder and then download each one:
def download_cont(self, folder_name):
for path in [entry['path'] for entry in self.client.metadata(folder_name)['contents'] if not entry['is_dir']]:
name = os.path.basename(path)
print 'Saving "%s"...' % name
with open(name, 'wb') as out:
with self.client.get_file(path) as f:
out.write(f.read())
UPDATE: If you're not using the latest (2.0) version of the Dropbox Python SDK, then you shouldn't use the nested with
statement. Just do this instead:
print 'Saving "%s"...' % name
with open(name, 'wb') as out:
out.write(self.client.get_file(path).read())
Upvotes: 2