Skip to content

Instantly share code, notes, and snippets.

@Hackbard
Forked from GrahamW/flashair_sync.rb
Created June 30, 2019 13:36
Show Gist options
  • Save Hackbard/05ab607703121fed52c52f66ed4df7cc to your computer and use it in GitHub Desktop.
Save Hackbard/05ab607703121fed52c52f66ed4df7cc to your computer and use it in GitHub Desktop.
require 'restclient'
require 'fileutils'
@base_url = ENV['FLASHAIR_URL'] || 'http://192.168.1.66'
@target_dir = ENV['FLASHAIR_SYNC_DIR'] || '~/FlashAirSync'
def get_dir(root)
puts "Getting dir #{root}..."
res = RestClient::Request.execute(
method: :get,
url: "#{@base_url}/command.cgi",
timeout: 10,
headers: {params: {op: 100, DIR: root}})
res.code == 200 ? res.body : nil
rescue => e
puts e
nil
end
def get_file(path)
puts "Getting file #{path}..."
raw = RestClient::Request.execute(
method: :get,
url: "#{@base_url}/#{path}",
raw_response: true)
if raw.code == 200
puts "Saved."
FileUtils.mv(raw.file.path, "#{@target_dir}/#{path.split('/').last}")
end
rescue => e
puts e
nil
end
def fetch(root)
resp = get_dir(root)
puts resp
return if resp.nil?
resp.each_line do |d|
_dir, name, _size, attr = d.split(',')
full_path = "#{root}/#{name}"
puts "name: #{name} attr: #{attr.to_i.to_s(2)}"
if (attr.to_i & (1 << 4)) > 0
fetch(full_path)
elsif !File.exist?("#{@target_dir}/#{name}")
get_file(full_path)
end
end
end
loop do
fetch('DCIM')
sleep(10)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment