gpt4 book ai didi

python - 从树莓派自动上传到dropbox?

转载 作者:太空宇宙 更新时间:2023-11-03 17:11:49 26 4
gpt4 key购买 nike

我正在我的树莓派上使用来自 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 步

逐步将其复制并粘贴到您的终端:

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

第 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

-您还需要从 Dropbox.com 获取 Accesstoken(https://www.dropbox.com/developers/apps)

您需要注册一个帐户。

我不知道是否有必要,但你需要在 MyApps 中有一个文件,并将其命名为“Raspi-upload”+ 我的设置里面:状态: 开发开发用户:只有你权限类型:完整 Dropbox允许隐式授予:允许

-然后你就有了你的 key :

我认为您在步骤 #2 中需要此 key ,或者您只需要生成一个访问 token

在下一个选项卡 [品牌] 上选择一个名称。

  • 每 <10 秒拍摄一张照片并将其上传到手机上的 Dropbox 应用。

  • 上传完成后拍摄的照片将被删除,因此您不会浪费 SD 卡上的空间。

这是脚本:#######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 是同一个文件,只是名称不同。

如果您有任何问题,也许我可以帮助您,但目标不是 100% 确定。

关于python - 从树莓派自动上传到dropbox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34005143/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com