Created
May 1, 2013 15:03
-
-
Save tomwitkin/5495798 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/ruby | |
# encoding: utf-8 | |
# | |
# Retrieve podcast artwork at the highest available resolution | |
# All arguments are combined to create an iTunes search | |
# The artwork for the first result, if found, is written to a filename based on search terms | |
# Based off itunesartwork.rb by Brett Terpstra (ttscoff) | |
# | |
# example: | |
# $ podcastartwork hypercritical | |
%w[net/http open-uri cgi].each do |filename| | |
require filename | |
end | |
def find_icon(terms) | |
url = URI.parse("http://itunes.apple.com/search?term=#{CGI.escape(terms)}&entity=podcast") | |
res = Net::HTTP.get_response(url).body | |
match = res.match(/"artworkUrl600":"(.*?)",/) | |
unless match.nil? | |
return match[1] | |
else | |
return false | |
end | |
end | |
terms = ARGV.join(" ") | |
icon_url = find_icon(terms) | |
unless icon_url | |
puts "Failed to get iTunes url" | |
exit | |
end | |
url = URI.parse(icon_url) | |
target = terms.gsub(/[^a-z0-9]+/i,'-')+"."+icon_url.match(/\.(jpg|png)$/)[1] | |
begin | |
open(url) do |f| | |
File.open(target,'w+') do |file| | |
file.puts f.read | |
end | |
puts "File written to #{target}." | |
end | |
rescue | |
puts "Failed to write artwork." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment