Skip to content

Instantly share code, notes, and snippets.

@callmeradical
Created June 1, 2011 06:48
Show Gist options
  • Save callmeradical/1001898 to your computer and use it in GitHub Desktop.
Save callmeradical/1001898 to your computer and use it in GitHub Desktop.
search for and download album art from amazon by movesmyers
#!/usr/bin/ruby -w
# search amazon for album art.
# run this from the directory where the album resides.
require 'rubygems'
require 'hpricot'
require 'open-uri'
# ****************************************************************
# Additional functionality
# ****************************************************************
check = false
until check do
workingdirectory = "notadirectory123"
Dir.chdir()
until File::directory?("#{workingdirectory}") do # Check workingdirectory is a directory
puts "enter album directory:"
workingdirectory = gets.chomp # Get input from user; for directory
workingdirectory = File.expand_path("#{workingdirectory}") # Normalize input to navigate to path
workingdirectory = "#{workingdirectory}".gsub("\\ ", " ")
workingdirectory = workingdirectory.squeeze(" ").strip
if File::directory?("#{workingdirectory}")
Dir.chdir("#{workingdirectory}")
puts "#{workingdirectory}" # Change to specified directory
else
puts "#{workingdirectory} is not a valid directory, try again."
end
end
# ****************************************************************
# This is the original code
# ****************************************************************
puts "enter artist name: "
artist = gets.chomp
puts "enter album name: "
album = gets.chomp
url = "http://www.google.com/search?q=site:amazon.com" + "+" + "#{artist}" + "+" + "#{album}" + "+&btnI=745"
url.gsub!(" ", "+")
raw = Hpricot(open("#{url}"))
ele = raw.search("img[@src*=jpg]").first
img = ele.to_s.match("(\")(.*?)(\")").captures[1]
open("#{album}.jpg", 'wb') do |file|
file << open(img.to_s).read
end
# ****************************************************************
# Additional functionality
# ****************************************************************
puts "created #{album}.jpg in #{workingdirectory}"
until check do # Created loop to check input and repeat for good input
puts "Do you wish to continue ? (y)(n) : " # Only accept y or n
choice = gets.chomp
if choice == 'n'
check = true
puts "Good Bye!"
elsif choice == 'y'
break if choice == 'y'
else choice != 'n' or 'y'
puts "#{choice} is not correct input, try again."
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment