Skip to content

Instantly share code, notes, and snippets.

@GrahamW
Created July 18, 2018 12:05
Show Gist options
  • Save GrahamW/229949cb5628ab79d8f3a50315f1ef0a to your computer and use it in GitHub Desktop.
Save GrahamW/229949cb5628ab79d8f3a50315f1ef0a 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
@GrahamW
Copy link
Author

GrahamW commented Jul 18, 2018

Simple script to automatically sync files from a Toshiba FlashAir SD card to a local dir. Linux.

• The card needs to be either in Station mode or Internet pass-through mode, so that it is discoverable on you LAN.
• Give the card a fixed IP address.

It loops forever, scanning every 10s for the card to appear on the LAN. If it does, it recurses through the directories present under /DCIM and if files not present on the server are found, they are copied across.

The sync is pretty simplistic, relying entirely on the filename.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment