Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
desc "Grab all the mp3/m4a files from a web page" task "grab:mp3s" do require 'hpricot' require 'open-uri' taskname = ARGV.shift if ARGV.empty? $stderr.puts "usage: #{File.basename($0)} #{taskname} <mp3dir-uri>" exit(1) end uri = ARGV.first doc = open(uri) {|f| Hpricot(f)} links = (doc/"a").map do |a| a.get_attribute("href") end.select do |link| link.match(/\.(mp3|m4a)$/) end.map { |link| URI.join(uri, link) } dirname = URI.unescape(File.basename(uri)) FileUtils.mkdir(dirname) links.each do |link| filename = File.join(dirname, URI.unescape(File.basename(link.to_s))) puts filename open(filename, 'w').write(link.read) end end
This paste will be private.
From the Design Piracy series on my blog: