Skip to content

Instantly share code, notes, and snippets.

@kkosuge
Created June 16, 2014 15:30
Show Gist options
  • Save kkosuge/83fee7c3782cad84ca57 to your computer and use it in GitHub Desktop.
Save kkosuge/83fee7c3782cad84ca57 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'net/http'
require 'twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = "YOUR_CONSUMER_KEY"
config.consumer_secret = "YOUR_CONSUMER_SECRET"
config.access_token = "YOUR_ACCESS_TOKEN"
config.access_token_secret = "YOUR_ACCESS_SECRET"
end
# get id
user = IO.popen("whoami", "r+").gets.chomp
program = ARGV[0].to_s
idfile = "/Users/#{user}/Library/Gyazo/id"
old_idfile = File.dirname(program) + "/gyazo.app/Contents/Resources/id"
id = ''
if File.exist?(idfile) then
id = File.read(idfile).chomp
elsif File.exist?(old_idfile) then
id = File.read(old_idfile).chomp
end
# capture png file
tmpfile = "/tmp/image_upload#{$$}.png"
imagefile = ARGV[1]
sound_file = File.dirname(program) + "/shutter.aiff"
if imagefile && File.exist?(imagefile) then
system "sips -s format png \"#{imagefile}\" --out \"#{tmpfile}\""
else
system "screencapture -x -i \"#{tmpfile}\""
if File.exist?(tmpfile) then
system "afplay #{sound_file}"
system "sips -d profile --deleteColorManagementProperties \"#{tmpfile}\""
dpiWidth = `sips -g dpiWidth "#{tmpfile}" | awk '/:/ {print $2}'`
dpiHeight = `sips -g dpiHeight "#{tmpfile}" | awk '/:/ {print $2}'`
pixelWidth = `sips -g pixelWidth "#{tmpfile}" | awk '/:/ {print $2}'`
pixelHeight = `sips -g pixelHeight "#{tmpfile}" | awk '/:/ {print $2}'`
if (dpiWidth.to_f > 72.0 and dpiHeight.to_f > 72.0) then
width = pixelWidth.to_f * 72.0 / dpiWidth.to_f
height = pixelHeight.to_f* 72.0 / dpiHeight.to_f
system "sips -s dpiWidth 72 -s dpiHeight 72 -z #{height} #{width} \"#{tmpfile}\""
end
end
end
if !File.exist?(tmpfile) then
exit
end
res = client.update_with_media('', File.open(tmpfile))
display_url = res.attrs[:entities][:media][0][:display_url]
url = "http://#{display_url}"
File.delete(tmpfile)
IO.popen("pbcopy","r+") do |io|
io.write url
io.close
end
system "open #{url}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment