-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.rb
282 lines (237 loc) · 8 KB
/
library.rb
1
2
3
4
5
6
7
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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
###########################################################################
# Copyright (C) 2006 by Paul Betts #
# paul.betts@gmail.com #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the #
# Free Software Foundation, Inc., #
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
###########################################################################
$:.unshift File.join(File.dirname(__FILE__))
# Ruby standard library
require 'rubygems'
require 'logger'
require 'gettext'
require 'pathname'
require 'digest/md5'
require 'zlib'
# Estelle
require 'platform'
require 'execute_list'
require 'song'
include GetText
$logging_level ||= Logger::ERROR
DefaultTaggerPath = File.join(File.dirname(__FILE__), 'taggers')
class Library < Logger::Application
public
attr_writer :is_soundtrack
attr_reader :taggers
def initialize
super(self.class.to_s())
self.level = $logging_level
@taggers = nil
@tag_info = nil
@album_info = nil
@md5_index = nil
end
def clear
@tag_info = nil
@album_info = nil
end
def empty?; (not @tag_info) or @tag_info.empty?; end
def size; (@tag_info ? @tag_info.size : 0); end
def load(files, progress_rate = 0.05)
load_taggers(DefaultTaggerPath) unless @taggers
return unless @taggers
load_cache
@tag_info = {}
log INFO, "Processing #{files.size} files..."
count = 0
yield_every = (files.length * progress_rate).to_i
yield_every = 1 if (yield_every == 0)
files.each do |current|
count += 1
yield(count.to_f / files.length) if (block_given? and (count % yield_every) == 0)
#log DEBUG, 'Reading %s..' % current
# First, see if we've already scanned this file before (save some time
# and only read the first 256K)
begin
md5sum = Digest::MD5.hexdigest(File.open(current, 'r') {|x| x.read(0x40000)})
if (@tag_info[current] = @md5_index[md5sum])
#log DEBUG, "Cache hit!"
next
end
rescue
log DEBUG, "Couldn't open file!"
@tag_info.delete current
next
end
# Check to see if we can load this file
loader = nil
@taggers.each { |x| break if (loader = x).get_tags? current }
unless loader
log DEBUG, "No one supports '#{current.to_s}'!"
next
end
yield(count.to_f / files.size) if (block_given? and (count % yield_every) == 0)
if (@tag_info[current] = loader.song_info(current))
@tag_info[current][:path] = current
@tag_info[current][:md5sum] = md5sum
next
else
log DEBUG, "Couldn't get tags for '#{current.to_s}'!"
end
# Can't be loaded, delete it and move on
@tag_info.delete current
end
File.open("fixture.yaml", 'w') {|x| x.puts self.to_yaml}
save_cache
log INFO, "Loaded #{@tag_info.size} files"
log DEBUG, "Exiting load"
end
def load_cache(target = nil)
infile = (target || File.join(Platform.home_dir, "libcache.yaml.gz"))
begin
a = nil; Zlib::GzipReader.open(infile) {|x| a = YAML::load(x.read)}
throw "Failed!" unless a.is_a? Hash and a.values.all? {|x| x.is_a? Hash}
@tag_info = {}; a.keys.each {|x| @tag_info[x] = Song.new(a[x])}
rescue
# TODO: Worst debugging message evar.
log DEBUG, "Couldn't load cache"
@tag_info = {}
end
@md5_index = {}
@tag_info.values.each do |x|
next unless x[:md5sum]
@md5_index[x[:md5sum]] = x
end
end
def save_cache(target = nil)
outfile = (target || File.join(Platform.home_dir, "libcache.yaml.gz"))
t = {}; @tag_info.keys.each {|x| t[x] = @tag_info[x].to_hash}
Zlib::GzipWriter.open(outfile) { |x| x.puts t.to_yaml() }
end
def find_soundtracks
@is_soundtrack ||= {}
log DEBUG, "Entering find_soundtracks"
build_tables unless @album_info
@album_info.keys.each do |curname|
artists = {}
current = @album_info[curname]
next if @is_soundtrack.has_key? curname
current.each { |track| artists[track[:canonical_artist]] ||= 0; artists[track[:canonical_artist]] += 1; }
histogram = artists.sort { |a,b| a[1] <=> b[1] } # Sort by value
#log DEBUG, "#{curname} - #{histogram.size} artists, leader has #{histogram[0][1]}"
next if histogram[0][1] > 0.75*histogram.size or histogram.size < 3
# Call out the handler
log DEBUG, "#{curname} may be a soundtrack; #{histogram.size} artists, leader has #{histogram[0][1]}"
@is_soundtrack[curname] = true if yield(curname)
end
log DEBUG, "Exiting find_soundtracks"
end
InvalidChars = /[:;\/<>]/
InvalidCharsString = ':;\/<>'
def create_action_list(target_root, musicformat, soundtrackformat)
log DEBUG, "Creating action list"
music_fstr,music_tags = create_format_string(musicformat)
sndtrk_fstr,sndtrk_tags = create_format_string(soundtrackformat)
# Iterate through all our files and create the list
list = []
@tag_info.values.each do |current|
if @is_soundtrack[ current[:album] ] != true
data = music_tags.collect do |x|
current.checked_tag(x, InvalidChars, lambda { |a,b,c| yield a,InvalidCharsString,c })
end
dest = File.join(target_root,
music_fstr % data)
else
data = sndtrk_tags.collect do |x|
current.checked_tag(x, InvalidChars, lambda { |a,b,c| yield a,InvalidCharsString,c})
end
dest = File.join(target_root,
sndtrk_fstr % data)
end
list << [ current[:path], dest, current ]
end
list
end
def execute_action_list(list, action, execute_class = DebugList, output_file = nil, only_execute = 0)
do_it = execute_class.new
do_it.begin output_file
count = 0
list.each do |x|
path = Pathname.new x[1]
do_it.mkdirs path.dirname
count = count + 1
break if only_execute > 0 and count > only_execute
begin
case action
when :copy
do_it.cp x[0], x[1], x[2]
when :move
do_it.mv x[0], x[1], x[2]
when :symlink
do_it.link x[0], x[1], x[2]
end
rescue
log ERROR, _("Failure processing %s") % x[0]
next
end
end
do_it.finish
end
def load_taggers(*paths)
paths.each do |x|
log DEBUG, "Trying to load #{x}"
Pathname.new(x).each_entry do |y|
file = File.join(x,y)
require file if y.extname == '.rb'
end
end
# Now reflect through the objects and find ones that match
@taggers = []
tagger_classes = ObjectSpace.each_object(Class) do |x|
pm = x.public_instance_methods(true)
next unless pm.include? 'get_tags?' and pm.include? 'song_info'
@taggers << x.instance
end
log DEBUG, "Loaded #{@taggers.size} taggers"
@taggers = nil unless @taggers.size > 0
end
private
def build_tables
@album_info = {}
@tag_info.values.each do |current|
album = current[:album]
@album_info[album] ||= []
@album_info[album] << current
end
end
First_Tag = /(<\S*?>)+[^<]*/
def create_format_string(format_string)
# Find the tags used in the string
s = format_string.downcase
tags_used = []
while First_Tag.match s do
tags_used << $1
s.sub! First_Tag, ''
end
# Sub them in
result = format_string.downcase
tags_used.each do |x|
result.gsub! x, '%s'
end
ret = [result, tags_used.collect {|x| x[1..x.length-2].chomp.to_sym } ]
log DEBUG, "fs = #{ret[0]}, tags = #{ret[1]}"; ret
end
end