Python 从raspberry pi自动上传到dropbox?
我在raspberry pi上使用github的“Dropbox Uploader”脚本在我的Dropbox帐户上上传视频 它的工作很好,但现在我想我的树莓皮上传视频自动dropbox。为此,我编写了python脚本Python 从raspberry pi自动上传到dropbox?,python,raspberry-pi,dropbox,Python,Raspberry Pi,Dropbox,我在raspberry pi上使用github的“Dropbox Uploader”脚本在我的Dropbox帐户上上传视频 它的工作很好,但现在我想我的树莓皮上传视频自动dropbox。为此,我编写了python脚本 import os path="/tmp/motion/" def upload_files(): if not os.path.exists(path): return os.stdir(path) for files in os.li
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步
-
您还需要从Dropbox.com获取Accesstoken
()
你需要注册一个帐户。
我不知道是否有必要但你需要在MyApps中有一个文件,
并命名为“Raspi upload”
+我的内部设置:
现状:发展
开发用户:只有你
权限类型:完整Dropbox
允许隐式授权:允许
-
那你就有钥匙了:
我认为您需要在步骤2中使用此密钥,或者只需要生成一个访问令牌
在下一个选项卡[品牌]上选择一个名称。
- 它拍摄一张照片每个Python都对空格敏感,您必须正确缩进代码才能运行。也就是说,为什么要编写一个Python脚本来启动一个shell脚本?还有,“它不起作用”是什么意思?它整天坐在沙发上吗?它不会在我的dropbox帐户上上传视频。请阅读Stack Overflow的指南。您应该提取一个最小但完整的示例和一个精确的错误描述。有一大堆事情在进行,例如,程序在什么时候停止了预期的行为?
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
#!/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']