2013-12-22 57 views
1

我想編寫代碼,使用Dropbox API可以下載指定文件夾中的所有圖像,而不是Dropbox中的子文件夾。到目前爲止,我已經寫了這個使用dropbox API下載指定文件夾中的所有圖像而不是子文件夾API

def download_cont(self, folderName): 

    fname = folderName 
    folder_metadata = self.client.metadata('/' + fname) 
    print folder_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} 

從我想我要遍歷JSON對象和元數據使用'contents'下載的每個文件,但我我不知道如何做到這一點。請幫忙。

回答

2

你說得對。元數據的contents成員會告訴您有關列出的文件夾中的文件(和子文件夾)。每個條目都有一個path成員,告訴你它的完整路徑和一個is_dir成員,它告訴你這個條目是否是一個目錄(而不是文件)。下面是一個使用Python的列表理解來獲得指定文件夾中的所有文件的路徑,然後下載每一個部分代碼:

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:如果你不使用最新版本(2.0 )版本的Dropbox Python SDK,那麼您不應該使用嵌套的with語句。只需做到這一點:

print 'Saving "%s"...' % name 
with open(name, 'wb') as out: 
    out.write(self.client.get_file(path).read()) 
+0

感謝您的幫助代碼。然而,我得到這個錯誤'追蹤(最近呼叫最後): 文件「/home/archit/Documents/Dropbox/dropbox-api-dev/first_app_1.0.py」,行91,在 drop.download_cont ('images') 文件「/home/archit/Documents/Dropbox/dropbox-api-dev/first_app_1.0.py」,第53行,在download_cont 與self.client.get_file(路徑)作爲f: AttributeError :HTTPResponse實例沒有屬性'__exit __''。請告訴我錯誤是什麼? –

+1

我猜你在SDK的1.6版本而不是2.0版本(剛剛幾天前發佈)。看到我更新的答案。 – smarx

相關問題