Created
November 25, 2010 17:34
-
-
Save djtal/715678 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' | |
| def parse_lib | |
| @list = [] | |
| @doc = Nokogiri::XML(File.open('lib.xml', 'r')) | |
| # Find each dictionary item and loop through it | |
| @doc.xpath('/plist/dict/dict/dict').each do |node| | |
| hash = {} | |
| last_key = nil | |
| # Stuff the key value pairs in to hash. We know a key is followed by | |
| # a value, so we'll just skip blank nodes, save the key, then when we | |
| # find the value, add it to the hash | |
| node.children.each do |child| | |
| next if child.blank? # Don't care about blank nodes | |
| if child.name == 'key' | |
| # Save off the key | |
| last_key = child.text | |
| else | |
| # Use the key we saved | |
| hash[last_key] = child.text | |
| end | |
| end | |
| @list << hash # push on to our list | |
| end | |
| end | |
| def genre_breakdown | |
| count = {} | |
| @list.each do |track| | |
| next if !track["Play Count"] || track["Play Count"] == "0" | |
| c = track["Play Count"] | |
| g = track["Genre"] || :undefined | |
| if count.keys.include?(g.to_sym) | |
| count[g.to_sym] += c.to_i | |
| else | |
| count[g.to_sym] = c.to_i | |
| end | |
| end | |
| count | |
| end | |
| def genre_analysis | |
| @genres = {} | |
| @list.each do |track| | |
| g = track["Genre"] || :undefined | |
| if genres.keys.include?(g.to_sym) | |
| genre = @genres[g.to_sym] | |
| else | |
| genres[g.to_sym] = {} | |
| genre = @genres[g.to_sym] | |
| end | |
| c = track["Play Count"] | |
| g = track["Genre"] || :undefined | |
| if count.keys.include?(g.to_sym) | |
| count[g.to_sym] += c.to_i | |
| else | |
| count[g.to_sym] = c.to_i | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment