106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/kbl/cli.rb', line 106
def dump(input_filename=nil)
input = open_file(input_filename) || STDIN
if output_filename = options[:output]
output = open_file(output_filename, 'w')
if output.nil?
puts "Cannot open file #{output_filename} for output."
exit 1
end
else
output = STDOUT
end
ids = input.readlines
ids.each(&:chomp!).keep_if {|id| id.match(/\d+/) }
if ids.empty?
puts "No songs to export. Exit."
exit 2
end
meta_data = {}
db_files = Dir.glob(File.expand_path("~/Library/Application Support/KKBOX/*/Playlists2.db"))
db_files.each do |db_file|
db = SQLite3::Database.new db_file
sql = <<-SQL
SELECT song_id, song_name, artist_id, artist_name, album_id, album_name
FROM song_tracks
WHERE song_id IN (#{ids.map {|id|"'#{id}'"}.join(',')});
SQL
db.execute(sql) do |row|
data = MetaData.new(*row)
meta_data[data.song_id] = data
end
end
ids.each do |id|
if data = meta_data[id]
output.puts data.send(:"to_#{options[:format]}")
else
STDERR.puts "ID Not Found: #{id}"
end
end
end
|