8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/itunes_csv.rb', line 8
def self.invoke(args)
options = {}
opts = OptionParser.new do |opts|
options[:fields] = ['name', 'artist']
opts.on( '-f', '--fields a,b,c', Array, "List of fields (default: #{options[:fields].join(',')})" ) do |list|
options[:fields] = list
end
options[:path] = '~/Music/iTunes/iTunes Music Library.xml'
opts.on( '-p', '--path PATH', "Path to iTunes XML file (default: #{options[:path]})" ) do |path|
options[:path] = path
end
end
opts.parse!
library_path = File.expand_path(options[:path])
library = ITunes::Library.load(library_path)
csv_string = CSV.generate do |csv|
csv << options[:fields]
library.music.tracks.each do |t|
row = options[:fields].map do |f|
unless t.respond_to?(f.to_sym)
puts "Unknown field: #{f}"
exit
end
t.send(f.to_sym)
end
csv << row
end
end
puts csv_string
end
|