37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/rob.rb', line 37
def self.import!(indir, libdir="#{ ENV['HOME']}/Music")
puts "Imporing all mp3s in: #{ indir }"
puts "Now importing to #{ libdir }"
Dir[File.join(indir, '**.mp3')].sort.each do |file|
if File.extname(file) == '.mp3'
song = ID3Lib::Tag.new file
begin
artist = song.artist
album = song.album
track = song.track[0]
title = song.title
dest = File.join(libdir, artist, album)
rescue
artist = Iconv.conv('UTF-8', 'UTF-16BE', song.artist)
album = Iconv.conv('UTF-8', 'UTF-16BE', song.album)
track = Iconv.conv('UTF-8', 'UTF-16BE', song.track)[0]
title = Iconv.conv('UTF-8', 'UTF-16BE', song.title)
end
out_file = filename(track, title)
dest = File.join(libdir, artist, album, out_file)
artistdir = File.join(libdir, artist)
albumdir = File.join(artistdir, album)
if !File.exists? dest
if !Dir.exists? artistdir
puts "Creating artist directory #{ artistdir }"
Dir.mkdir artistdir
end
if !Dir.exists? albumdir
puts "Creating album directory #{ albumdir }"
Dir.mkdir albumdir
end
puts "Now importing #{ title } to #{ dest }"
FileUtils.cp file, dest
else
puts "Destination file #{ dest } exists, skipping..."
end
else
end
end
end
|