[英]Download all .mp4 files present in a Dropbox folder
使用 API 從 Dropbox下載文件,一旦擁有訪問令牌就很簡單了。
然后,使用方法sharing_get_shared_link_file可以簡單地運行
import dropbox
dbx = dropbox.Dropbox("ACCESS_TOKEN")
#dbx.users_get_current_account()
with open("test1.mp4", "wb") as f:
metadata, res = dbx.sharing_get_shared_link_file("https://www.dropbox.com/s/kowz06jo7i3xyv2/you_saved_me.mp4?dl=0")
f.write(res.content)
正如您在 URL 中看到的那樣, /s/表示我們正在處理一個文件。
問題是,有時它不是一個文件,而是文件所在的文件夾,因此鏈接將包含/sh/ 。
如何一一下載特定文件夾中存在的所有.mp4文件(沒有.zip )?
作為參考,創建了一個包含三個.mp4 文件的文件夾 - https://www.dropbox.com/sh/r85vzhq0xxa146s/AAASRlyR-C9ITAd0Cww0Sr9Za?dl=0
如果您有指向包含文件的文件夾的共享鏈接,而不僅僅是指向特定文件的共享鏈接,您可以使用files_list_folder
和files_list_folder_continue
列出該文件夾的內容。 您可以通過在 files_list_folder 的shared_link
參數中files_list_folder
來實現。
結果將包含有關內容的信息,例如每個內容的name
。
然后,您可以使用它來構建傳遞給sharing_get_shared_link_file
以下載每個所需文件的path
。
根據您現有的代碼和 URL,這看起來像這樣:
import dropbox
dbx = dropbox.Dropbox("ACCESS_TOKEN")
shared_link = dropbox.files.SharedLink(url="https://www.dropbox.com/sh/r85vzhq0xxa146s/AAASRlyR-C9ITAd0Cww0Sr9Za?dl=0")
listing = dbx.files_list_folder(path="", shared_link=shared_link)
# todo: add implementation for files_list_folder_continue
for entry in listing.entries:
if entry.name.endswith(".mp4"):
with open(entry.name, "wb") as f:
# note: this simple implementation only works for files in the root of the folder
metadata, res = dbx.sharing_get_shared_link_file(url=shared_link.url, path="/" + entry.name)
f.write(res.content)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.