#!/usr/bin/ruby # encoding: utf-8 # # Grab iTunes Icon - Brett Terpstra 2014 # # This version has been modified by Eric Dejonckheere %w[net/http open-uri cgi].each do |filename| require filename end class ItunesIcon def find_icon terms url = URI.parse("http://itunes.apple.com/search?term=#{CGI.escape(terms)}&entity=#{@entity}") res = Net::HTTP.get_response(url).body match = res.match(/"#{@icon_size}":"(.*?)",/) unless match.nil? return match[1] else return false end end def grab_small *args @entity = "macSoftware" @icon_size = "artworkUrl60" grab args end def grab_big *args @entity = "macSoftware" @icon_size = "artworkUrl100" grab args end def grab *args terms = args.join(" ") icon_url = find_icon terms if icon_url URI.parse(icon_url) else '' end end def download url, path begin open(url) do |f| File.open(path,'w+') do |file| file.puts f.read end puts "#{path}" end rescue puts "Error: failed to save icon." end end end