Last active
August 29, 2015 14:13
-
-
Save rakuishi/bac8817eab96eb91c2c6 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
# -*- coding: utf-8 -*- | |
# $ ruby tinypng.rb drawable/ | |
require 'fileutils' | |
require 'net/https' | |
require 'uri' | |
API_KEY = '***' | |
i_dir = ARGV[0] | |
o_dir_prefix = 'tinypng_' | |
FileUtils.mkdir_p(o_dir_prefix + i_dir) | |
Dir.glob(i_dir + '*.png').each do |image| | |
uri = URI.parse('https://api.tinypng.com/shrink') | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
request = Net::HTTP::Post.new(uri.request_uri) | |
request.basic_auth("api", API_KEY) | |
response = http.request(request, File.binread(image)) | |
if response.code == "201" | |
puts "Compression OK: " + image + " => " + o_dir_prefix + image | |
File.binwrite(o_dir_prefix + image, http.get(response["location"]).body) | |
else | |
puts "Compression NG: " + image | |
end | |
end |
Author
rakuishi
commented
Mar 26, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment