Skip to content

Instantly share code, notes, and snippets.

@rtimmons
Created September 13, 2012 16:50
Show Gist options
  • Save rtimmons/3715736 to your computer and use it in GitHub Desktop.
Save rtimmons/3715736 to your computer and use it in GitHub Desktop.
How much time have you spent listening to...
#!/usr/bin/env perl -w
=pod
Use: Count the amount of time spent listening to a particular artist
Example usage:
./itunes-count.pl \
--libxml=$HOME/Music/iTunes/iTunes\ Music\ Library.xml \
--artist="Carbon Leaf"
=cut
use strict;
use warnings;
use Carp::Heavy;
use Mac::iTunes::Library::XML;
use Data::Dumper;
use Getopt::Long;
my $libxml;
my $artist = 'Carbon Leaf';
my $opts = GetOptions(
"libxml=s" => \$libxml,
"artist=s" => \$artist
);
my $library = Mac::iTunes::Library::XML->parse(
$libxml
);
my $milliseconds = 0;
my %its = $library->items();
my $leaf_albums = $its{$artist};
foreach my $album (values %$leaf_albums) {
foreach my $song (@$album) {
# print Dumper($song);
my $time = $song->totalTime();
my $plays = $song->playCount();
my $total = $plays ? $time * $plays : 0;
# print Dumper({plays=>$plays, time=>$time, total=>$total});
$milliseconds += $total;
}
}
my $seconds = $milliseconds / 1000;
my $minutes = $seconds / 60;
my $hours = $minutes / 60;
my $days = $hours / 24;
print Dumper {
seconds => $seconds,
minutes => $minutes,
hours => $hours,
days => $days
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment