Skip to content

Instantly share code, notes, and snippets.

@kohashi
Created May 22, 2012 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohashi/2770553 to your computer and use it in GitHub Desktop.
Save kohashi/2770553 to your computer and use it in GitHub Desktop.
Rubyっぽいバッチスクリプトでgyazoに投稿
rem gyazowinR.bat
rem Gyazo client for Windows (Ruby script version)
rem by snaka (http://d.hatena.ne.jp/snaka72/)
ruby -x "%~f0" "%~s1"
pause
exit
#! ruby
require 'net/http'
require 'yaml'
# スクリプトが格納されている場所にidファイルを作る
script_dir = File.expand_path(File.dirname(__FILE__))
idfile = "#{script_dir}\\gyazowin.bat.id"
id = ''
if File.exist?(idfile) then
id = File.read(idfile).chomp
else
id = Time.new.strftime("%Y%m%d%H%M%S")
File.open(idfile, "w").print( id + "\n")
end
imagefile = ARGV[0]
imagedata = open(imagefile, 'rb') {|f| f.read }
boundary = '----BOUNDARYBOUNDARY----'
data = <<EOF
--#{boundary}\r
content-disposition: form-data; name="id"\r
\r
#{id}\r
--#{boundary}\r
content-disposition: form-data; name="imagedata"; filename="gyazo.com"\r
\r
#{imagedata}\r
--#{boundary}--\r
EOF
HOST = 'gyazo.com'
PORT = 80
CGI = '/upload.cgi'
UA = 'Gyazo/1.0'
header ={
'Content-Length' => data.length.to_s,
'Content-type' => "multipart/form-data; boundary=#{boundary}",
'User-Agent' => UA
}
# load proxy settings (optional)
proxy_setting = "#{script_dir}\\proxy.yaml"
proxy = {}
if (FileTest.exist?(proxy_setting))
proxy = YAML.load_file(proxy_setting)
puts "proxy settings: #{proxy['address']}, #{proxy['port']}"
end
Net::HTTP::Proxy(proxy['address'], proxy['port']).start(HOST, port=PORT){|http|
res = http.post(CGI, data, header)
url = res.response.body
system("start #{url}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment