Автоматическая загрузка в Dropbox на Raspberry Pi - PullRequest
0 голосов
/ 07 мая 2018

Я установил скрипт загрузки Dropbox для Raspberry pi и смог успешно загрузить файл в Dropbox, но теперь мне нужно настроить автоматический скрипт для загрузки файлов в определенную папку назначения в Dropbox. Недавно я нашел скрипт, который бы делал именно это, но моя проблема в том, что я не могу указать папку назначения в Dropbox. Другие пользователи ответили на сообщение на форуме с просьбой ввести данные для папки назначения, но сообщение неактивно в течение нескольких месяцев.

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

Я исследовал другие сообщения, связанные с этой проблемой, но они не будут работать в моих обстоятельствах. Оба сценария работают отлично, но я хотел бы посмотреть, можно ли изменить сценарий, чтобы указать папку назначения в раскрывающемся списке.

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 голосов
/ 09 сентября 2018

При использовании сценария dropbox_uploader.sh вы указываете папку для сохранения файла в учетной записи Dropbox. Однако это ограничивается теми настройками, которые вы задали «приложению» в настройках Dropbox для получения вашего токена доступа. Вы можете разрешить чтение / запись в любом месте вашей учетной записи Dropbox или только в определенной папке.

Найдите «Тип разрешения» и «Имя папки приложения» на странице настройки приложений Dropbox: https://www.dropbox.com/developers/apps

...