Skip to content

Commit

Permalink
Add a script so I don’t have to scan using HP ‘not so’ Smart
Browse files Browse the repository at this point in the history
  • Loading branch information
tuzz committed Aug 5, 2020
1 parent 784a53c commit 94962c6
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions chef/node.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"tools",
"zz",
"chrome",
"scan",
"slack",
"keyboard",
"menubar",
Expand Down
76 changes: 76 additions & 0 deletions chef/scan/files/scan
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env ruby

require "net/http"
require "fileutils"

DOTS_PER_INCH = 300
JPEG_QUALITY = 75
TMP_DIRECTORY = "/tmp/scans"
OUT_DIRECTORY = "~/Dropbox/Paperwork/_ToBeSorted"
SCAN_COOLDOWN = 5

ip = nil

1.upto(255) do |i|
ip = "192.168.0.#{i}"
uri = URI.parse("http://#{ip}/eSCL/ScannerCapabilities")
break if Net::HTTP.get(uri).include?("ScannerCapabilities")
rescue Errno::ECONNREFUSED
end

if ip.nil?
puts "Failed to discover scanner on the network."
exit(1)
end

puts "Found scanner at #{ip}..."

# TODO: Install escl-scan-cli with zz once my PR is merged.
# https://github.com/ElXreno/escl-scan/pull/16

FileUtils.rm_rf(TMP_DIRECTORY)
FileUtils.mkdir_p(OUT_DIRECTORY)

(1..).each do |doc|
dir = "#{TMP_DIRECTORY}/#{doc}"
FileUtils.mkdir_p(dir)

time = nil
(1..).each do |page|
system("escl-scan-cli #{ip} #{dir}/#{page}.jpg --dpi #{DOTS_PER_INCH}")
time = Time.now.to_f

print "Add page to existing document? [Y/n] "
input = gets.strip.downcase

break if input.start_with?("n")

# Wait for the scanner to reset.
elapsed = Time.now.to_f - time
duration = SCAN_COOLDOWN - elapsed
sleep duration if duration > 0
end

puts "Converting to PDF with quality #{JPEG_QUALITY}..."
out_path = "#{OUT_DIRECTORY}/#{Time.now.to_i}.pdf"

system("convert #{dir}/*.jpg -quality #{JPEG_QUALITY} -sampling-factor 4:2:0 -strip #{out_path}")
puts "Written to #{out_path}"

print "Start a new document? [Y/n] "
input = gets.strip.downcase

break if input.start_with?("n")

elapsed = Time.now.to_f - time
duration = SCAN_COOLDOWN - elapsed
sleep duration if duration > 0
end

puts "Done. Scans are in #{OUT_DIRECTORY}"

print "Open directory in Finder? [Y/n] "
input = gets.strip.downcase

exit if input.start_with?("n")
system("open #{OUT_DIRECTORY}")
5 changes: 5 additions & 0 deletions chef/scan/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cookbook_file "install scan script" do
source "scan"
path ZZ::Path.scan
mode "755"
end
4 changes: 4 additions & 0 deletions lib/zz/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def public_gpg_key
to("chef/gpg/files/public_gpg_key")
end

def scan
File.expand_path("/usr/local/bin/scan")
end

def ssh_directory
File.expand_path("~/.ssh")
end
Expand Down

0 comments on commit 94962c6

Please sign in to comment.