從樹莓派自動上傳到保管箱? (automatic upload on dropbox from raspberry pi?)


問題描述

從樹莓派自動上傳到保管箱? (automatic upload on dropbox from raspberry pi?)

我在我的樹莓派上使用來自 github 的“Dropbox‑Uploader”腳本將視頻上傳到我的 Dropbox 帳戶。

https://github.com/andreafabrizi/Dropbox‑Uploader

它工作得很好,但現在我希望我的樹莓派能夠在 dropbox 上自動上傳視頻。為此,我編寫了 python 腳本

import os
path="/tmp/motion/"

def upload_files():
    if not os.path.exists(path):
        return
    os.stdir(path)
    for files in os.listdir("."):
        if files.endswith(".avi"):
            cmd = "/home/pi/dropbox_uploader.sh upload " + path + files
            os.system(cmd)
            os.system("sudo rm /tmp/motion/" + files)


if _name_ == "_main_":
    upload_files()

並將其設置為cronjob,但它不起作用,它沒有在我的帳戶上上傳任何內容。任何幫助將不勝感激


參考解法

方法 1:

I write a Script from many different Scripts and finally its working great. Some things first to Install: ‑you make a complete Update, Upgrade I don't know right now all unneccary steps but I will write it down all here, and you can edit it by your own‑ but it still works and takes no harm on your Device.

STEP #1

copy + paste it to your terminal step by step:

sudo apt‑get install python3‑picamera
sudo apt‑get install python3‑pip
sudo apt‑get install python‑pip
pip3 install unicornhat
pip install unicornhat
sudo apt‑get update
sudo apt‑get upgrade ‑f
sudo apt‑get upgrade
sudo apt‑get install pip
sudo pip install dropbox

STEP #2

git clone https://github.com/andreafabrizi/Dropbox‑Uploader.git

curl "https://raw.githubusercontent.com/andreafabrizi/Dropbox‑Uploader/master/dropbox_uploader.sh" ‑o dropbox_uploader.sh

chmod +x dropbox_uploader.sh

./dropbox_uploader.sh

‑ You also need to get an Accesstoken from Dropbox.com ( https://www.dropbox.com/developers/apps )

You need to register an Account.

And I don't know if it is necessary but you need to have a file in MyApps, and give it a name " Raspi‑upload " + my Settings inside: Status: Development Development User: Only you Permission Type: Full Dropbox Allow implicit grant: Allow

‑ Then you have your KEY:

I think you need this Key in Step #2 or You only need to Generate an ACCESS TOKEN

On the next TAB [Branding] choose a Name.

  • It takes a Photo every <10 seconds and uploaded it to your Dropbox app on your Phone.

  • The Photo that is taken will be deleted after the Upload is finished, so you don't waste Space on your SD‑card.

Here is the Script: #######TOP

#!/usr/bin/env python3.4

import dropbox
from dropbox.exceptions import ApiError, AuthError
import time
import datetime
import picamera
import sys, os
from time import sleep
import dropcamloop1

# Authorisation token
TOKEN = 'HERE IS YOUR ACCESS TOKEN'

# Format photo will be saved as e.g. jpeg
PHOTOFORMAT = 'jpeg'



# Create a camera object and capture image using generated filename
def camCapture(filename):
    with picamera.PiCamera() as camera:
        camera.resolution = (1920, 1080)
        print("Photo: %s"%filename + "." + PHOTOFORMAT)
        time.sleep(2)
        camera.capture(filename + '.' + PHOTOFORMAT, format=PHOTOFORMAT)



        print("Photo captured and saved ...")

        return filename + '.' + PHOTOFORMAT




# Generate timestamp string generating name for photos
def timestamp():
    tstring = datetime.datetime.now()
    print("Filename generated ...")
    return tstring.strftime("%Y%m%d_%H%M%S")
    #return tstring.strftime("image")


# Upload localfile to Dropbox
def uploadFile(localfile):

    # Check that access tocken added
    if (len(TOKEN) == 0):
        sys.exit("ERROR: Missing access token. "
                 "try re‑generating an access token from the app console at dropbox.com.")

    # Create instance of a Dropbox class, which can make requests to API
    print("Creating a Dropbox object...")
    dbx = dropbox.Dropbox(TOKEN)

    # Check that the access token is valid
    try:
        dbx.users_get_current_account()
    except AuthError as err:
        sys.exit("ERROR: Invalid access token; try re‑generating an "
                 "access token from the app console at dropbox.com.")

    # Specify upload path
    uploadPath = '/' + localfile

    # Read in file and upload
    with open(localfile, 'rb') as f:
        print("Uploading " + localfile + " to Dropbox as " + uploadPath + "...")

        try:
            dbx.files_upload(f.read(), uploadPath)
        except ApiError as err:
            # Check user has enough Dropbox space quota
            if (err.error.is_path() and
                    err.error.get_path().error.is_insufficient_space()):
                sys.exit("ERROR: Cannot upload; insufficient space.")
            elif err.user_message_text:
                print(err.user_message_text)
                sys.exit()
            else:
                print(err)
                sys.exit()



# Delete file
def deleteLocal(file):
    os.system("rm " + file)
    print("File: " + file + " deleted ...")



def main():

    # Generate name for file based on current time
    filename = timestamp()

    # Capture photo
    file = camCapture(filename)

    # Upload file
    uploadFile(file)

    # Delete local file
    deleteLocal(file)

    print("Done")
    #time.sleep(4)

    #x=0
    #while(x <4):
    #os.execv(sys.executable, ['/home/pi/Adafruit_Python_DHT/examples/dropcamloop1.py'])

if __name__ == '__main__':
    main()

    time.sleep(4)


    x=3
    while(x <4):
        #os.execv(sys.executable, [sys.executable] )
        execfile('dropcamloop.py')
    else:
        start ['dropcamloop1.py']

END = dropcamloop1.py is the same file just the name is different.

IF You got any Question maybe I can help you there but aim not 100% sure.

(by A.Mujeebhesham ah)

參考文件

  1. automatic upload on dropbox from raspberry pi? (CC BY‑SA 2.5/3.0/4.0)

#dropbox #Python #raspberry-pi






相關問題

獲取 Dropbox 共享鏈接的文件列表 (Get list of files for dropbox shared link)

將文件上傳到保管箱時,方法出錯 (while uploading a file to dropbox getting an error with method)

Dropbox Api putFile php 發送文件失敗 (Dropbox Api putFile php fail send file)

Tiếp tục quá trình tải lên trong IOS nền (Continue uploading process in background IOS)

API Dropbox: phát hiện cập nhật tệp và nhận URL đến tệp? (Dropbox API: detect file updates, and get a URL to a file?)

Mac OS X 文件的 st_flags(用戶定義的標誌)是什麼? (What are st_flags (user defined flags) for a file Mac OS X?)

Dropbox如何監控? (How does dropbox monitoring?)

如何在 Dropbox 中使用 Bazaar? (How to use Bazaar with Dropbox?)

在 Python 中使用 OAuth2 連接到保管箱時出錯 (Error using OAuth2 to connect to dropbox in Python)

從樹莓派自動上傳到保管箱? (automatic upload on dropbox from raspberry pi?)

識別 macOS 上的託管文件夾 (Identify managed folders on macOS)

我們如何在下載/上傳 dropbbox 方法中獲取文件 mime-type/content-type? (How can we get files mime-type/content-type in download/upload dropbbox methods?)







留言討論