Last active
May 28, 2017 19:36
-
-
Save jroes/8cb48afd924bfa1b849d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
# | |
# Download 1136x1136 screenshots and 512x512 icon from iTunes for a list of | |
# search terms. | |
# | |
# Jon Roes <[email protected]> | |
# | |
# 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