Last active
December 30, 2015 18:49
-
-
Save Kilobyte22/7870011 to your computer and use it in GitHub Desktop.
This file contains 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
=begin | |
Script to upload screenshots one a press of a button | |
warning: for this script you need the 'ruby-dbus' gem, also, this will most likely never work on Windows or OSX. Its written for linux after all. By default it utilizes KDE tools. i'll happily add more if requested. | |
LICENSE: GPL | |
=end | |
CFGNAME = "#{Dir.home}/.uploadscreenshot.yml" | |
require 'dbus' | |
require 'optparse' | |
require 'yaml' | |
require 'tempfile' | |
require 'net/ftp' | |
$BUS = DBus::SessionBus.instance | |
def dbg(text) | |
if defined?($DEBUG) && $DEBUG | |
puts text | |
end | |
end | |
options = { | |
daemon: false, | |
tray: false, | |
gencfg: nil, | |
fork: false, | |
quiet: false | |
} | |
$options = options | |
# Define parser options | |
parser = OptionParser.new do |opts| | |
opts.banner = 'Usage: uploadscreenshot [flags] [command]' | |
opts.on('-d', '--daemon', 'Run in background') do | |
options[:daemon] = true | |
dbg('daemon mode') | |
end | |
opts.on('-h', '--help', 'Shows this text and exits') do | |
puts opts | |
exit | |
end | |
opts.on('-g', '--genconfig [filename]', 'Creates a new configuration file') do |file| | |
options[:gencfg] = file || "#{Dir.home}/.uploadscreenshot.yml" | |
dbg('generating config') | |
end | |
opts.on('-t', '--trayicon', 'Makes it show a tray icon. Only has effect if used with -d') do | |
options[:tray] = true | |
dbg('tray icon') | |
end | |
opts.on('-f', '--fork', 'Fork into background') do | |
options[:fork] = true | |
dbg('fork mode') | |
end | |
opts.on('-q', '--quiet', 'Disables all output') do | |
options[:quiet] = true | |
dbg('quiet mode') | |
end | |
end.parse! | |
unless options[:key].nil? | |
cfg = { | |
'clipboard' => 'klipper', | |
'screenshot_util' => 'ksnapshot', | |
'before_command' => 'kdialog --title "Screenshot Uploader" --passivepopup "Uploading screenshot... Please wait"', | |
'post_command' => 'kdialog --title "Screenshot uploaded" --passivepopup "@URL@"', | |
'uploader' => 'ftp', | |
'ftp' => { | |
'host' => 'ftp.example.org', | |
'path' => '/screenshot', | |
'password' => 'secret', | |
'user' => 'someone' | |
} | |
} | |
File.write(options[:gencfg], cfg.to_yaml) | |
exit | |
end | |
$CONFIG = YAML.load_file(CFGNAME) | |
def out(text) | |
puts text unless $options[:quiet] | |
end | |
case $CONFIG['clipboard'] | |
when 'klipper' | |
klipper_service = $BUS.service('org.kde.klipper') | |
klipper_object = klipper_service.object('/klipper') | |
klipper_object.introspect | |
$klipper_iface = klipper_object['org.kde.klipper.klipper'] | |
def to_clipboard(text) | |
$klipper_iface.setClipboardContents(text) | |
end | |
else | |
puts 'ERROR: INVALID CLIPBOARD' | |
end | |
case $CONFIG['screenshot_util'] | |
when 'ksnapshot' | |
ksnapname = nil | |
$BUS.proxy.ListNames[0].each do |el| | |
if el =~ /^org\.kde\.ksnapshot.*/ | |
dbg el | |
ksnapname = el | |
end | |
end | |
if ksnapname.nil? | |
puts 'No KSnapshot instance found' | |
else | |
dbg "KSnapshot name: #{ksnapname}" | |
end | |
ksnapshot_service = $BUS.service(ksnapname) | |
ksnapshot_object = ksnapshot_service.object('/KSnapshot') | |
ksnapshot_object.introspect | |
$ksnapshot_iface = ksnapshot_object['org.kde.ksnapshot'] | |
def save_screenshot(filename) | |
unless $ksnapshot_iface.save filename | |
out 'ERROR: could not save screenshot' | |
exit | |
end | |
end | |
else | |
puts 'ERROR: INVALID SCREENSHOT UTIL' | |
end | |
case $CONFIG['uploader'] | |
when 'debug' | |
def upload(file) | |
puts file | |
"file://#{file}" | |
end | |
when 'ftp' | |
def upload(file) | |
ftpcfg = $CONFIG['ftp'] | |
remote_file = "screenshot_#{DateTime.now.strftime('%Y-%m-%d_%H-%M-%S')}.png" | |
=begin | |
ftpline = "ftp -u ftp://#{ftpcfg['user']}:#{ftpcfg['password']}@#{ftpcfg['host']}#{ftpcfg['path']}/#{remote_file} #{file}" | |
dbg ftpline | |
out `#{ftpline}` | |
=end | |
ftp = Net::FTP.new(ftpcfg['host']) | |
ftp.login ftpcfg['user'], ftpcfg['password'] | |
ftp.putbinaryfile(file, "#{ftpcfg['path']}/#{remote_file}") | |
ftp.close | |
ftpcfg['url'].gsub '@FILE@', remote_file | |
end | |
else | |
puts 'ERROR: INVALID UPLOADER' | |
end | |
def daemon_main | |
end | |
def on_demand_main | |
system($CONFIG['before_command']) | |
file = Dir::Tmpname.make_tmpname ["/tmp/uploadscreenshot", 'png'], nil | |
save_screenshot(file) | |
dbg("Saved as #{file}") | |
url = upload(file) | |
to_clipboard(url) | |
File.delete file | |
system($CONFIG['post_command'].gsub '@URL@', url) | |
end | |
if options[:daemon] | |
if options[:fork] | |
# Fork into background | |
child = Process.fork do | |
daemon_main | |
end | |
Process.detach child | |
else | |
daemon_main | |
end | |
else | |
on_demand_main | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment