Skip to content

Instantly share code, notes, and snippets.

@ashaw
Created January 16, 2011 21:15
Show Gist options
  • Save ashaw/782154 to your computer and use it in GitHub Desktop.
Save ashaw/782154 to your computer and use it in GitHub Desktop.
For when there are a lot of mp3s on a web page
#!/usr/bin/env ruby
require 'rubygems'
require 'rest_client'
require 'nokogiri'
USER_AGENT = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.231 Safari/534.10"
def filename(url)
URI::parse(url).path.gsub(/^.+\/(.+)$/,'\1')
end
if !ARGV[0] || ARGV[0] == "-h" || ARGV[0] == "--help"
puts "== Usage: mp3scrape <URL>"
exit 1
end
html = Nokogiri::HTML(RestClient.get(ARGV[0]))
mp3s = html.css("a").select {|q|
q if q.attr("href") && q.attr("href").match(/\.mp3$/)
}.collect {|q|
q.attr("href")
}
if mp3s.size < 1
raise "no MP3s found!"
end
mp3s.each_with_index do |url,idx|
puts "== [#{idx + 1}/#{mp3s.size}] #{url}"
`cd /tmp && curl "#{url}" -O -# -A "#{USER_AGENT}"`
`cd /tmp && open -a iTunes #{filename(url)}`
end
puts "== cleaning up"
sleep 2
mp3s.each do |url|
`cd /tmp && rm -rf #{filename(url)}`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment