Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Last active January 13, 2022 10:07
Show Gist options
  • Save ttscoff/5477280 to your computer and use it in GitHub Desktop.
Save ttscoff/5477280 to your computer and use it in GitHub Desktop.
Retrieve a 512 x 512px icon for an iOS app
#!/usr/bin/ruby
# encoding: utf-8
#
# Updated 2017-10-25:
# - Defaults to large size (512)
# - If ImageMagick is installed:
# - rounds the corners (copped from @bradjasper, https://github.com/bradjasper/Download-iTunes-Icon/blob/master/itunesicon.rb)
# - replace original with rounded version, converting to png if necessary
#
# 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
#
# If ImageMagick is installed (available through Homebrew), rounded
# corners will be added and a transparent PNG will be output.
#
# example:
# $ itunesicon super monsters ate my condo
#
# Use size param ~s/~small/~m/~medium/~l/~large to specify size:
# $ itunesicon super monsters ate my condo ~small
#
# http://brettterpstra.com/2013/04/28/instantly-grab-a-high-res-icon-for-any-ios-app/
# http://brettterpstra.com/2013/12/18/icon-grabber-updated-to-search-any-platform/
%w[net/http open-uri cgi fileutils].each do |filename|
require filename
end
def find_icon(terms, entity, size)
url = URI.parse("http://itunes.apple.com/search?term=#{CGI.escape(terms)}&entity=#{entity}")
res = Net::HTTP.get_response(url).body
match = res.match(/"#{size}":"(.*?)",/)
unless match.nil?
return match[1]
else
return false
end
end
terms = ARGV.join(" ")
entity = "iPadSoftware"
type = "_ipad"
if terms =~ /[\#@](ipad|iphone|mac)/i
if terms =~ /[\#@]iphone/i
entity = "software"
type = "_iphone"
elsif terms =~ /[\#@]mac/i
entity = "macSoftware"
type = "_mac"
end
terms.gsub!(/[\#@](ipad|iphone|mac)/i, "").gsub!(/\s+/," ")
end
format = "artworkUrl512"
size = "l"
if terms =~ /~(s(mall)?|m(edium)?|l(arge)?)/i
size = $1[0]
format = case size
when /s(mall)?/ then "artworkUrl60"
when /m(edium)?/ then "artworkUrl100"
else "artworkUrl512"
end
terms.gsub!(/~(s(mall)?|m(edium)?|l(arge)?)/i, "").gsub!(/\s+/," ")
end
def magick(filename, size)
begin
i = case size
when /s/ then 60
when /m/ then 100
else 512
end
rect = "#{i.to_s}x#{i.to_s}"
round = (i * 0.15625 + 3).round.to_s
target = filename.sub(/\.\w+$/,'_round.png')
%x{convert -size #{rect} xc:none -fill white -draw 'roundRectangle 0,0 #{i},#{i} #{round},#{round}' #{filename} -compose SrcIn -composite #{target} &> /dev/null}
if File.exists? target
FileUtils.rm(filename)
filename.sub!(/\.\w+$/,'.png')
FileUtils.mv(target,filename)
end
return filename
rescue
return filename
end
end
terms.strip!
icon_url = find_icon(terms, entity, format)
unless icon_url
puts "Error: failed to locate iTunes url. You may need to adjust your search terms."
exit
end
url = URI.parse(icon_url)
target = File.expand_path("~/Desktop/"+terms.gsub(/[^a-z0-9]+/i,'_')+"_"+size+"."+icon_url.match(/\.(jpg|png)$/)[1])
begin
open(url) do |f|
File.open(target,'w+') do |file|
file.puts f.read
end
target = magick(target,size)
print target
end
rescue Exception => e
# puts e.backtrace
# p e
puts "Error: failed to save icon."
end
@vigo
Copy link

vigo commented Jun 10, 2014

super kool! thank you!

@jroes
Copy link

jroes commented Sep 11, 2014

Hi! I've forked a slightly rewritten version that pulls 1136x1136 screenshots for a list of terms:

https://gist.github.com/jroes/8cb48afd924bfa1b849d

@touchthedesign
Copy link

Hello, I can't seem to get this working.

After downloading the file, navigating to file directory, making it executable (chmod a+x itunesicon.rb), and attempting to run - 'itunesicon.rb twitter'; I subsequently get this: "-bash: itunesicon.rb: command not found"

Please help!

@bradjasper
Copy link

I've forked this to round the corners in ImageMagick and generate @2x and @3x assets, which I needed for my use case:

https://github.com/bradjasper/Download-iTunes-Icon

Thanks for the snippet @ttscoff

@pkotla
Copy link

pkotla commented Jan 15, 2015

@bradjasper Thank you for the tool but i am not getting the rounded icons nor 2x, 3x images. Am i doing anything wrong?

@ttscoff
Copy link
Author

ttscoff commented Oct 25, 2017

@bradjasper I added a simple version of your code (just adds rounded corners to whatever size was downloaded, converting to png if needed). If ImageMagick isn't installed, it will just silently fail that call and proceed as normal.

Thanks for that!

@Kiryous
Copy link

Kiryous commented Jan 13, 2020

@ttscoff hi there! Thanks for your script. It would be great if some comment will be added on how to use size param, maybe this way — https://gist.github.com/Kiryous/709ab1a33971072232c1e1471c240c8c

@ttscoff
Copy link
Author

ttscoff commented Jan 13, 2020

@ttscoff hi there! Thanks for your script. It would be great if some comment will be added on how to use size param, maybe this way — https://gist.github.com/Kiryous/709ab1a33971072232c1e1471c240c8c

@Kiryous done, thanks!

@welby12
Copy link

welby12 commented Feb 19, 2021

I'm trying to grab the icon for the dating app Bumble. When I search Bumble, it is downloading the icon for Plenty Of Fish's app. When I looked into it, and searched the App Store on my iPhone, I noticed that the first promoted search is an ad for Plenty Of Fish. So it's giving me the advertisement's icon??

@ttscoff
Copy link
Author

ttscoff commented Feb 19, 2021

I'm trying to grab the icon for the dating app Bumble. When I search Bumble, it is downloading the icon for Plenty Of Fish's app. When I looked into it, and searched the App Store on my iPhone, I noticed that the first promoted search is an ad for Plenty Of Fish. So it's giving me the advertisement's icon??

The script just grabs the first result from the search, so if Apple is sticking sponsored results into the API, there's not much it can do about it...

@davemacdo
Copy link

The ImageMagick steps are not working for me anymore. I'm on Big Sur. I've tried reinstalling ImageMagick with Homebrew. Do I need to do something to make it available to this Automator workflow?

@ttscoff
Copy link
Author

ttscoff commented May 17, 2021

The ImageMagick steps are not working for me anymore. I'm on Big Sur. I've tried reinstalling ImageMagick with Homebrew. Do I need to do something to make it available to this Automator workflow?

I noticed I wasn't getting rounded corners anymore, but hadn't had time to look into it. I don't have an answer offhand, but will try to dig in next time I have some free time (new job).

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