#!/usr/bin/env ruby # setting browser_cmd = 'xdg-open' clipboard_cmd = 'xclip' def file_should_be_present_or_die(fname, packagename) if !File.exist?(fname) then puts "This script requires the tool #{fname} to be present. It is ordinary a part of the package '#{packagename} ." puts "In Debian based distros like Ubuntu or Mint, you can install it, using the following command:" puts "sudo apt-get install #{packagename} " exit end end file_should_be_present_or_die('/usr/bin/xclip', 'xclip') file_should_be_present_or_die('/usr/bin/xdotool', 'xdotool') file_should_be_present_or_die('/usr/bin/xdg-open', 'xdg-utils') file_should_be_present_or_die('/usr/bin/xprop', 'x11-utils') file_should_be_present_or_die('/usr/bin/import', 'graphicsmagick-imagemagick-compat') require 'net/http' require 'open3' require 'openssl' require 'json' require 'yaml' # get id idfile = ENV['HOME'] + "/.gyazo.id" id = '' if File.exist?(idfile) then id = File.read(idfile).chomp end # get active window name active_window_id = `xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)" | cut -d ' ' -f 5`.chomp out, err, status = Open3.capture3 "xwininfo -id #{active_window_id} | grep \"xwininfo: Window id: \"|sed \"s/xwininfo: Window id: #{active_window_id}//\"" active_window_name = out.chomp out, err, status = Open3.capture3 "xprop -id #{active_window_id} | grep \"_NET_WM_PID(CARDINAL)\" | sed s/_NET_WM_PID\\(CARDINAL\\)\\ =\\ //" pid = out.chomp application_name = `ps -p #{pid} -o comm=`.chomp # capture a jpg file (it will be smaller and so faster to transfer than png in most cases ...) tmpfile = "/tmp/image_upload#{$$}.jpg" imagefile = ARGV[0] if imagefile && File.exist?(imagefile) then system "convert '#{imagefile}' '#{tmpfile}'" else configfile = "#{ENV['HOME']}/.gyazo.config.yml" command = (File.exist?(configfile) && YAML.load_file(configfile)['command']) || '/usr/bin/import -quality 85 ' system "#{command} '#{tmpfile}'" end if !File.exist?(tmpfile) then exit end imagedata = File.read(tmpfile) File.delete(tmpfile) xuri = "" if application_name =~ /(chrom(ium|e)|firefox|iceweasel)/ xuri = `xdotool windowfocus #{active_window_id}; xdotool key "ctrl+l"; xdotool key "ctrl+c"; xclip -o` end # upload boundary = '----BOUNDARYBOUNDARY----' # endpoint https://gyazo.com/api/docs ##HOST = 'upload.gyazo.com' ##CGI = '/upload.cgi' HOST = 'url4e.com' CGI = '/gyazo/upload.php' UA = 'Gyazo/1.2' metadata = JSON.generate({ app: active_window_name, title: active_window_name, url: xuri, note: "#{active_window_name}\n#{xuri}" }) data = < data.length.to_s, 'Content-type' => "multipart/form-data; boundary=#{boundary}", 'User-Agent' => UA } env = ENV['http_proxy'] if env then uri = URI(env) proxy_host, proxy_port = uri.host, uri.port else proxy_host, proxy_port = nil, nil end https = Net::HTTP::Proxy(proxy_host, proxy_port).new(HOST,443) https.use_ssl = true https.verify_mode = OpenSSL::SSL::VERIFY_PEER https.verify_depth = 5 https.start{ res = https.post(CGI,data,header) url = res.response.body puts url if system "which #{clipboard_cmd} >/dev/null 2>&1" then system "echo -n '#{url}' | #{clipboard_cmd}" end system "#{browser_cmd} '#{url}'" # save id newid = res.response['X-Gyazo-Id'] if newid and newid != "" then if !File.exist?(File.dirname(idfile)) then Dir.mkdir(File.dirname(idfile)) end if File.exist?(idfile) then File.rename(idfile, idfile+Time.new.strftime("_%Y%m%d%H%M%S.bak")) end File.open(idfile,"w").print(newid) end }