Skip to content

Instantly share code, notes, and snippets.

@primalmotion
Forked from cho45/script
Created December 7, 2011 18:18
Show Gist options
  • Save primalmotion/1443935 to your computer and use it in GitHub Desktop.
Save primalmotion/1443935 to your computer and use it in GitHub Desktop.
Gyazo.app
#!/usr/bin/env ruby
require 'fileutils'
require 'pathname'
require 'uri'
require 'net/http'
require 'securerandom'
Net::HTTP.version_1_2
include FileUtils
DROPBOX = Pathname.new("~/Dropbox/Public/Screenshots").expand_path
URLBASE = URI.parse("http://dl.dropbox.com/u/673746/Screenshots/")
tmpfile = "/tmp/image_upload#{$$}.png"
imagefile = ARGV[1]
if imagefile && File.exist?(imagefile)
system "sips", "-s", "format", "png", imagefile, "--out", tmpfile
else
system "screencapture", "-i", tmpfile
if File.exist?(tmpfile)
system "sips", "-d", "profile", "--deleteColorManagementProperties", tmpfile
end
end
if !File.exist?(tmpfile) then
exit
end
filename = "#{Time.now.strftime("%Y-%m-%d.%H-%M-%S")}.(#{SecureRandom.hex(10)}).png"
mv tmpfile, DROPBOX + filename
uri = URLBASE + URI.escape(filename)
try = 0
begin
sleep 1
res = Net::HTTP.start(uri.host, uri.port) {|http| http.get(uri.request_uri) }
case res.code
when "200"
# do nothing
when "404"
raise "Uploading"
else
warn "Unexpected Error: %s" % res.inspect
end
rescue
warn "Uploading..."
if try < 3
try += 1
retry
end
end
system "open", uri.to_s
s = IO.popen('pbpaste', 'r+').read
IO.popen('pbcopy', 'w').puts uri.to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment