在Raspberry Pi上自动上传到Dropbox

时间:2018-05-07 17:19:38

标签: python raspberry-pi dropbox

我为Raspberry pi设置了Dropbox上传程序脚本,并且能够成功将文件上传到Dropbox,但现在我需要设置一个自动脚本将文件上传到Dropbox中的特定目标文件夹。我最近发现了一个可以做到这一点的脚本,但我的问题是我无法在Dropbox中指定Destination文件夹。其他用户在论坛帖子上回复,要求输入目标文件夹,但该帖子已停用数月。

https://github.com/andreafabrizi/Dropbox-Uploader https://www.raspberrypi.org/forums/viewtopic.php?t=164166

我研究了与此问题相关的其他stackoverflow帖子,但它们在我的环境中不起作用。这两个脚本都运行良好,但我想看看是否可以更改脚本以在dropbox中指定目标文件夹。

syncdir是用于上传的本地文件夹。

我需要输入类似" / dropbox / TeamFolder"而不是直接将文件上传到我的Dropbox用户目录。

import os
import subprocess
from subprocess import Popen, PIPE

#The directory to sync
syncdir="/home/pi/Dropbox-Files/"
#Path to the Dropbox-uploaded shell script
uploader = "/home/pi/Dropbox-Uploader/dropbox_uploader.sh"

#If 1 then files will be uploaded. Set to 0 for testing
upload = 1
#If 1 then don't check to see if the file already exists just upload it, if 0 don't upload if already exists
overwrite = 0
#If 1 then crawl sub directories for files to upload
recursive = 1
#Delete local file on successfull upload
deleteLocal = 0



#Prints indented output
def print_output(msg, level):
    print((" " * level * 2) + msg)


#Gets a list of files in a dropbox directory
def list_files(path):
    p = Popen([uploader, "list", path], stdin=PIPE, stdout=PIPE, stderr=PIPE)
    output = p.communicate()[0].decode("utf-8")

    fileList = list()
    lines = output.splitlines()

    for line in lines:
        if line.startswith(" [F]"):
            line = line[5:]
            line = line[line.index(' ')+1:]
            fileList.append(line)

    return fileList


#Uploads a single file
def upload_file(localPath, remotePath):
    p = Popen([uploader, "upload", localPath, remotePath], stdin=PIPE, stdout=PIPE, stderr=PIPE)
    output = p.communicate()[0].decode("utf-8").strip()
    if output.startswith("> Uploading") and output.endswith("DONE"):
        return 1
    else:
        return 0


#Uploads files in a directory
def upload_files(path, level):
    fullpath = os.path.join(syncdir,path)
    print_output("Syncing " + fullpath,level)
    if not os.path.exists(fullpath):
        print_output("Path not found: " + path, level)
    else:

        #Get a list of file/dir in the path
        filesAndDirs = os.listdir(fullpath)

        #Group files and directories

        files = list()
        dirs = list()

        for file in filesAndDirs:
            filepath = os.path.join(fullpath,file)
            if os.path.isfile(filepath):
                files.append(file)       
            if os.path.isdir(filepath):
                dirs.append(file)

        print_output(str(len(files)) + " Files, " + str(len(dirs)) + " Directories",level)

        #If the path contains files and we don't want to override get a list of files in dropbox
        if len(files) > 0 and overwrite == 0:
            dfiles = list_files(path)

        #Loop through the files to check to upload
        for f in files:                                 
            print_output("Found File: " + f,level)   
            if upload == 1 and (overwrite == 1 or not f in dfiles):
                fullFilePath = os.path.join(fullpath,f)
                relativeFilePath = os.path.join(path,f)  
                print_output("Uploading File: " + f,level+1)   
                if upload_file(fullFilePath, relativeFilePath) == 1:
                    print_output("Uploaded File: " + f,level + 1)
                    if deleteLocal == 1:
                        print_output("Deleting File: " + f,level + 1)
                        os.remove(fullFilePath)                        
                else:
                    print_output("Error Uploading File: " + f,level + 1)

        #If recursive loop through the directories   
        if recursive == 1:
            for d in dirs:
                print_output("Found Directory: " + d, level)
                relativePath = os.path.join(path,d)
                upload_files(relativePath, level + 1)





#Start
upload_files("",1)

1 个答案:

答案 0 :(得分:0)

使用dropbox_uploader.sh脚本时,请指定文件夹以将文件保存到Dropbox帐户上。但是,这仅限于您在Dropbox设置中为“ app”提供的用于获取访问令牌的设置。您可以将其设置为允许在您的Dropbox帐户中或仅在特定文件夹中的任何位置进行读取/写入。

在Dropbox应用设置页面上查找“权限类型”和“应用文件夹名称”:https://www.dropbox.com/developers/apps