Skip to content

Instantly share code, notes, and snippets.

@jroes
Last active May 28, 2017 19:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jroes/8cb48afd924bfa1b849d to your computer and use it in GitHub Desktop.
Save jroes/8cb48afd924bfa1b849d to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
#
# Download 1136x1136 screenshots and 512x512 icon from iTunes for a list of
# search terms.
#
# Jon Roes <jroes@heroku.com>
#
# Example:
# $ iosgfx search_terms.txt
#
# search_terms.txt should contain each search term on a new line. Only graphics
# for the top 5 results will be downloaded.
#
# Based on:
# Grab iTunes Icon - Brett Terpstra 2014 <http://brettterpstra.com>
# https://gist.github.com/ttscoff/5477280
%w[net/http open-uri cgi].each do |filename|
require filename
end
require 'json'
require 'fileutils'
def debug?; !ENV['DEBUG'].nil?; end
def save_url(terms, app_name, icon_url, type)
url = URI.parse(icon_url)
FileUtils.mkdir_p File.expand_path("~/Desktop/#{app_name}")
random = Random.new.rand(1...999999)
ext = File.extname(icon_url)
target = File.expand_path("~/Desktop/#{app_name}/#{type}-#{random}#{ext}")
begin
open(url) do |f|
File.open(target,'w+') do |file|
file.puts f.read
end
puts "#{target}"
end
rescue
puts "Error: failed to save #{type} image at url #{icon_url}."
end
end
def find_and_save_icons(terms, entity)
url = URI.parse("http://itunes.apple.com/search?term=#{CGI.escape(terms)}&entity=#{entity}")
puts "Fetching #{url}" if debug?
body = Net::HTTP.get_response(url).body
json = JSON.parse(body)
puts json['resultCount'].to_s + " results found for #{terms}." if debug?
json['results'].first(5).each do |result|
result['screenshotUrls'].each do |screenshot_url|
if screenshot_url.include? '1136x1136'
puts "Downloading screenshot #{screenshot_url}" if debug?
save_url(terms, result['trackName'], screenshot_url, 'screenshot')
else
puts "Skipping screenshot (not 1136x1136) #{screenshot_url}" if debug?
end
end
# Save high res icon
puts "Downloading icon #{result['artworkUrl512']}" if debug?
save_url(terms, result['trackName'], result['artworkUrl512'], 'icon')
end
end
entity = "software" # iphone :)
puts "Reading lines from #{ARGV[0]}" if debug?
File.open(ARGV[0]).readlines.each do |terms|
puts "Searching for #{terms}"
find_and_save_icons(terms, entity)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment