Created
December 8, 2008 22:31
-
-
Save tenderlove/33654 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'nokogiri' | |
class EyeTunes | |
attr_reader :tracks | |
def initialize file | |
@artists = Hash.new { |artists,artist| | |
artists[artist] = Hash.new { |albums,album| | |
albums[album] = Hash.new { |tracks,track| | |
tracks[track] = Struct.new( | |
:name, | |
:play_count, | |
:time | |
).new(track, 0, 0) | |
} | |
} | |
} | |
@tracks = [] | |
@doc = Nokogiri::XML(File.open(file, 'rb')) | |
@doc.xpath('/plist/dict/dict/dict').each do |thing| | |
artist = thing.at('./key[text() = "Artist"]').next.text rescue "" | |
album = thing.at('./key[text() = "Album"]').next.text rescue "" | |
track = thing.at('./key[text() = "Name"]').next.text | |
rating = thing.at('./key[text() = "Rating"]').next rescue nil | |
rating.content = 100 if rating | |
time = thing.at('./key[text() = "Total Time"]').next.text.to_i rescue 0 | |
count = thing.at('./key[text() = "Play Count"]').next.text.to_i rescue 0 | |
track = @artists[artist][album][track] | |
track.time = time | |
track.play_count = count | |
@tracks << track | |
end | |
File.open('whatever.xml', 'wb') { |f| f.write(@doc.to_s) } | |
end | |
def artists | |
@artists.values | |
end | |
def albums | |
@artists.map { |k,v| v.values }.flatten | |
end | |
def playtime | |
time = 0 | |
tracks.map { |track| | |
track.time * track.play_count | |
}.each { |milliseconds| | |
time += milliseconds | |
} | |
time / 1000.0 | |
end | |
def top_awesome_tracks | |
@artists.find_all { |artist,album| | |
artist =~ /phil collins/i | |
}.map { |k,v| v.values }.flatten | |
end | |
end | |
if __FILE__ == $0 | |
eye_tunes = EyeTunes.new(ARGV[0]) | |
puts "Artist count: #{eye_tunes.artists.length}" | |
puts "Album count: #{eye_tunes.albums.length}" | |
puts "Track count: #{eye_tunes.tracks.length}" | |
puts "Total playtime (in seconds): #{eye_tunes.playtime}" | |
puts "#{eye_tunes.top_awesome_tracks.length} awesome songs" # Ryan has 0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment