Created
December 13, 2012 19:31
-
-
Save benphelps/4279060 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
## Config | |
SCP_HOST = "96.44.143.82" | |
SCP_USER = "phelps" | |
SCP_PATH = "public_html/s.64bits.co/" | |
WWW_BASE = "http://s.64bits.co/" | |
IMG_PATH = '/Desktop/' | |
IMG_GLOB = 'Screen Shot*.png' | |
LEN_RAND = 4 | |
ALL_GLOB = '_*' | |
require 'rvm' | |
RVM.gemset_use! 'default' | |
require 'net/scp' | |
require 'directory_watcher' | |
require 'clipboard' | |
require 'trollop' | |
require 'daemons' | |
require 'image_optim' | |
require 'terminal-notifier' | |
OPTS = Trollop::options do | |
banner "ScreenWatch: Watch a directory for screenshots. - http://benphelps.me/" | |
opt :daemon, "Fork to the background as a daemon." | |
end | |
class ScreenWatch | |
def start | |
io = ImageOptim.new(:pngout => false, optipng: false, :advpng => {level: 1}, :nice => '0', :threads => 8) | |
begin | |
if OPTS[:daemon] == true | |
puts "Forking to the background..." | |
Daemons.daemonize | |
end | |
dw = DirectoryWatcher.new Dir.home+IMG_PATH, :glob => IMG_GLOB | |
dw.interval = 0.2 | |
dw.add_observer { | |
|*args| args.each { | |
|event| | |
if event.type == :added | |
fsize = '%.2f' % (File.size(event.path).to_f / 1024) | |
io.optimize_image!(event.path) | |
fsize = '%.2f' % (File.size(event.path).to_f / 1024) | |
new_file_rand = rand(36**LEN_RAND).to_s(36) | |
new_file_name = new_file_rand + '.png' | |
Net::SCP.upload!(SCP_HOST, SCP_USER, event.path, SCP_PATH + new_file_name) | |
Clipboard.copy "#{WWW_BASE}?#{new_file_rand}" | |
TerminalNotifier.notify('Upload Successful!', :title => 'Screen Watch', :group => 'SCRW') | |
File.delete(event.path) | |
end | |
} | |
} | |
dw.start | |
while true | |
sleep 30 | |
end | |
rescue Interrupt | |
dw.stop | |
end | |
end | |
end | |
watcher = ScreenWatch.new | |
watcher.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment