Skip to content

Instantly share code, notes, and snippets.

@rakuishi
Last active August 29, 2015 14:13
Show Gist options
  • Save rakuishi/bac8817eab96eb91c2c6 to your computer and use it in GitHub Desktop.
Save rakuishi/bac8817eab96eb91c2c6 to your computer and use it in GitHub Desktop.
# -*- 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
@rakuishi
Copy link
Author

    gist/
    ├── tinypng.rb
    ├── ARGV[0]/
    └── tinypng_ + ARGV[0]/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment