60 lines
1.3 KiB
Ruby
60 lines
1.3 KiB
Ruby
class Track < ActiveRecord::Base
|
|
set_table_name 'musicbrainz.bh_track'
|
|
|
|
@@bad_names = [
|
|
'[silence]'
|
|
]
|
|
|
|
def self.getReleaseTracks(release_ids, group_by_release = false)
|
|
|
|
tracks = Track
|
|
.all(:conditions => ['release_id IN(?)', release_ids], :order => 'release_id ASC, position ASC')
|
|
.map{|track| track.length = (track.length /= 1000).round; track}
|
|
|
|
if group_by_release
|
|
g_tracks = {}
|
|
tracks.each do |track|
|
|
if g_tracks[track.release_id].nil?
|
|
g_tracks[track.release_id] = {}
|
|
end
|
|
g_tracks[track.release_id][track.id] = track
|
|
end
|
|
return g_tracks
|
|
end
|
|
|
|
tracks
|
|
end
|
|
|
|
def self.getUniqueReleaseTracks(release_ids)
|
|
tracks = self.getReleaseTracks(release_ids, true)
|
|
|
|
unique_tracks = []
|
|
track_list = {
|
|
'album' => [],
|
|
'bonus' => []
|
|
}
|
|
|
|
tracks.shift[1].each do |track|
|
|
unless @@bad_names.include?(track[1].name)
|
|
track_list['album'] << track[1]
|
|
unique_tracks << self.clearName(track[1].name)
|
|
end
|
|
end
|
|
|
|
tracks.each do |group|
|
|
group.each do |track|
|
|
unless @@bad_names.include?(track[1].name) && unique_tracks.include?(self.clearName(track[1].name))
|
|
track_list['bonus'] << track[1]
|
|
unique_tracks << self.clearName(track[1].name)
|
|
end
|
|
end
|
|
end
|
|
|
|
track_list
|
|
end
|
|
|
|
def self.clearName(name)
|
|
name.gsub('&', 'and').gsub(/[^a-z0-9]/, '').downcase
|
|
end
|
|
end
|