// description of your code here
This script sifts through your iTunes DB
From:
http://blog.zenspider.com/archives/2007/03/that_stupid_thing_i_wrote_the_other_day_part_2.html
1
2 require 'time'
3
4 class Album < Array
5 def age
6 map { |track| track.age }.max
7 end
8 def score
9 total / Math.log(age)
10 end
11 def total
12 inject(0.0) do |total_score, track|
13 total_score + track.score
14 end
15 end
16 def self.parse(file)
17 today = Time.now
18 d = {}
19 library = Hash.new { |h,k| h[k] = Album.new }
20
21 IO.foreach(File.expand_path(file)) do |line|
22 if line =~ /<key>(Name|Artist|Album|Date Added|Play Count|Rating)<\/key><.*?>(.*)<\/.*?>/ then
23 key = $1.downcase.split.first
24 val = $2
25 d[key.intern] = val
26
27 if d.size == 6 then
28 date = d[:date].sub(/T.*/, '')
29 key = "#{d[:album]} by #{d[:artist]}"
30 age = ((today - Time.parse(date)) / 86400.0).to_i
31 library[key] << Track.new(age, d[:play].to_i, d[:rating].to_i / 20)
32
33 d.clear
34 end
35 end
36 end
37 library
38 end
39 end
40
41 Track = Struct.new(:age, :count, :rating)
42 class Track
43 def score
44 rating * count.to_f
45 end
46 end
47
48 max = (ARGV.shift || 10).to_i
49 file = ARGV.shift || "~/Music/iTunes/iTunes Music Library.xml"
50 library = Album.parse(file)
51
52 top = library.sort_by { |h,k| -k.score }[0...max]
53 top.each_with_index do |(artist_album, album), c|
54 puts "%-3d = (%4d tot, %5.2f adj): %s" % [c+1, album.total, album.score, artist_album,]
55 album.each do |t|
56 puts " #{t.age} days old, #{t.count} count, #{t.rating} rating = #{t.score}"
57 end if $DEBUG
58 end