Skip to content

Instantly share code, notes, and snippets.

@martijnengler
Forked from ttscoff/itunesicon.rb
Last active December 17, 2015 01:29
Show Gist options
  • Save martijnengler/5529127 to your computer and use it in GitHub Desktop.
Save martijnengler/5529127 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# encoding: utf-8
#
# Retrieve an iOS app icon at the highest available resolution
# All arguments are combined to create an iTunes search
# The icon for the first result, if found, is written to a filename based on search terms
#
# example:
# $ itunesicon super monsters ate my condo
%w[net/http open-uri cgi].each do |filename|
require filename
end
def find_icon(terms, os)
os = "iPad" unless os
url = URI.parse("http://itunes.apple.com/search?term=#{CGI.escape(terms)}&entity=#{os}Software")
res = Net::HTTP.get_response(url).body
match = res.match(/"artworkUrl512":"(.*?)",/)
unless match.nil?
return match[1]
else
return false
end
end
os = ARGV.pop.gsub("xxx", "") if(ARGV.last == "xxxmac")
terms = ARGV.join(" ")
icon_url = find_icon(terms, os)
unless icon_url
puts "Failed to get iTunes url"
exit
end
url = URI.parse(icon_url)
ext = "." + icon_url.match(/\.(jpg|png)$/)[1]
basename = terms.gsub(/[^a-z0-9]+/i,'-')
masked = basename + ext
target = basename + "-original" + ext
mask_file = File.expand_path(File.dirname(__FILE__)) + "/mask1024.png"
begin
open(url) do |f|
File.open(target,'w+') do |file|
file.puts f.read
end
puts "Original file written to #{target}."
`convert #{target} #{mask_file} -gravity center -composite -format jpg -quality 90 #{masked}`
puts "File with mask written to #{masked}"
end
rescue
puts "Failed to write icon."
end
@martijnengler
Copy link
Author

Automatically adds a mask to the file (needs a file named mask1024.png in the same directory as the Ruby-file; I used the file mentioned here: http://brettterpstra.com/2013/04/28/instantly-grab-a-high-res-icon-for-any-ios-app/)

The second revision makes it "easy" to switch between OS X and iOS searches: if your last argument is 'xxxmac' it will search in the Mac Store. E.g.:

itunesicon.rb omnifocus will look for the iPad version of OmniFocus

itunesicon.rb omnifocus xxxmac will look for the Mac version of OmniFocus

It's maybe not the best way™, but it works for me. :)

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