che*_*run 10 ruby python inotify dropbox
第一个:我知道pyinotify.
我想要的是使用Dropbox上传服务到我的家庭服务器.
我的家庭服务器上有一个Dropbox的共享文件夹.每当共享该文件夹的其他人将任何东西放入该文件夹时,我希望我的家庭服务器等到它完全上传并将所有文件移动到另一个文件夹,并从Dropbox文件夹中删除这些文件,从而节省Dropbox空间.
这里的事情是,我不能只跟踪文件夹中的更改并立即移动文件,因为如果有人上传大文件,Dropbox将已开始下载,因此显示我家庭服务器上文件夹的更改.
有一些解决方法吗?使用Dropbox API可以实现这种方式吗?
我自己没有尝试过,但Dropbox CLI版本似乎有一个'filestatus'方法来检查当前文件状态.当我自己尝试时会报告回来.
这是一个 Ruby 版本,它不会等待 Dropbox 空闲,因此实际上可以在同步时开始移动文件。它还忽略了.和..。它实际上检查给定目录中每个文件的文件状态。
然后我会作为 cronjob 或在单独的屏幕中运行此脚本。
directory = "path/to/dir"
destination = "location/to/move/to"
Dir.foreach(directory) do |item|
next if item == '.' or item == '..'
fileStatus = `~/bin/dropbox.py filestatus #{directory + "/" + item}`
puts "processing " + item
if (fileStatus.include? "up to date")
puts item + " is up to date, starting to move file now."
# cp command here. Something along this line: `cp #{directory + "/" + item + destination}`
# rm command here. Probably you want to confirm that all copied files are correct by comparing md5 or something similar.
else
puts item + " is not up to date, moving on to next file."
end
end
Run Code Online (Sandbox Code Playgroud)
这是完整的脚本,我最终得到的是:
# runs in Ruby 1.8.x (ftools)
require 'ftools'
directory = "path/to/dir"
destination = "location/to/move/to"
Dir.glob(directory+"/**/*") do |item|
next if item == '.' or item == '..'
fileStatus = `~/bin/dropbox.py filestatus #{item}`
puts "processing " + item
puts "filestatus: " + fileStatus
if (fileStatus.include? "up to date")
puts item.split('/',2)[1] + " is up to date, starting to move file now."
`cp -r #{item + " " + destination + "/" + item.split('/',2)[1]}`
# remove file in Dropbox folder, if current item is not a directory and
# copied file is identical.
if (!File.directory?(item) && File.cmp(item, destination + "/" + item.split('/',2)[1]).to_s)
puts "remove " + item
`rm -rf #{item}`
end
else
puts item + " is not up to date, moving to next file."
end
end
Run Code Online (Sandbox Code Playgroud)