DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

ITunes DB Parser

Snippets Manager user avatar by
Snippets Manager
·
Mar. 19, 07 · · Code Snippet
Like (0)
Save
Tweet
1.10K Views

Join the DZone community and get the full member experience.

Join For Free
// 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


require 'time'

class Album < Array
  def age
    map { |track| track.age }.max
  end
  def score
    total / Math.log(age)
  end
  def total
    inject(0.0) do |total_score, track|
      total_score + track.score
    end
  end
  def self.parse(file)
    today = Time.now
    d = {}
    library = Hash.new { |h,k| h[k] = Album.new }

    IO.foreach(File.expand_path(file)) do |line|
      if line =~ /(Name|Artist|Album|Date Added|Play Count|Rating)<\/key><.*?>(.*)<\/.*?>/ then
        key = $1.downcase.split.first
        val = $2
        d[key.intern] = val

        if d.size == 6 then
          date = d[:date].sub(/T.*/, '')
          key = "#{d[:album]} by #{d[:artist]}"
          age = ((today - Time.parse(date)) / 86400.0).to_i
          library[key] << Track.new(age, d[:play].to_i, d[:rating].to_i / 20)

          d.clear
        end
      end
    end
    library
  end
end

Track = Struct.new(:age, :count, :rating)
class Track
  def score
    rating * count.to_f
  end
end

max = (ARGV.shift || 10).to_i
file = ARGV.shift || "~/Music/iTunes/iTunes Music Library.xml"
library = Album.parse(file)

top = library.sort_by { |h,k| -k.score }[0...max]
top.each_with_index do |(artist_album, album), c|
  puts "%-3d = (%4d tot, %5.2f adj): %s" % [c+1, album.total, album.score, artist_album,]
  album.each do |t|
    puts "  #{t.age} days old, #{t.count} count, #{t.rating} rating = #{t.score}"
  end if $DEBUG
end
Parser (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 11 Reasons To Use Selenium for Automation Testing
  • OpenTelemetry in Action: Identifying Database Dependencies
  • Why Great Money Doesn’t Retain Great Devs w/ Stack Overflow, DataStax & Reprise
  • 6 Best Books to Learn Multithreading and Concurrency in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo