require 'rake' require 'id3lib' TOTAL_PARTS = "2" CHAPTERS_IN_PART1 = "52" CHAPTERS_IN_PART2 = "74" def fix_tag(filename, part, total_parts, chapter, total_chapters_in_part) puts "Fixing ID3 tag of #{filename}..." tag = ID3Lib::Tag.new(filename) tag.strip! tag.title = "Parte #{part} - Capitulo #{chapter}" tag.album = "Don Quijote de la Mancha" tag.artist = "Miguel de Cervantes" tag.disc = "#{part}/#{total_parts}" tag.track = "#{chapter}/#{total_chapters_in_part}" tag.update! end desc "Download all files" task :download do ("01"..CHAPTERS_IN_PART1).each do |chapter| `wget http://www.aularagon.org/files/espa/elquijote/p1/Parte%201%20Cap%C3%ADtulo-#{chapter}.mp3` end ("01"..CHAPTERS_IN_PART2).each do |chapter| `wget http://www.aularagon.org/files/espa/elquijote/p2/Parte%202%20Cap%C3%ADtulo-#{chapter}.mp3` end end desc "Rename files to remove accents and whitespaces" task :rename do Dir.foreach(".") do |f| unless (m = f.match(/Parte\s(.*)\sCap.+tulo-(.*)\.mp3/)).nil? File.rename(f.to_s, "P" + m[1] + "-Capitulo-" + m[2] + ".mp3") puts "#{f.to_s}: Removed accents and whitespaces from filename" end end end desc "Fix ID3 tags" task :fixid3 do ("01"..CHAPTERS_IN_PART1).each do |chapter| fix_tag("P1-Capitulo-#{chapter}.mp3", "1", TOTAL_PARTS, chapter, CHAPTERS_IN_PART1) end ("01"..CHAPTERS_IN_PART2).each do |chapter| fix_tag("P2-Capitulo-#{chapter}.mp3", "2", TOTAL_PARTS, chapter, CHAPTERS_IN_PART2) end puts "DONE!" end desc "Default: Download, rename and fix ID3 tags" task :all => [:download, :rename, :fixid3] task :default => [:all]