#!/usr/bin/perl # ---------------------------------------------------------------------- # STOREITUNESINFO -- PERL PROGRAM # ---------------------------------------------------------------------- # Initial author: Howard () - Tue Feb 24 2004 # This program # ---------------------------------------------------------------------- $opt_cache = "$ENV{HOME}/.StoreiTunesInfo.cache"; # ---------------------------------------------------------------------- # Use GetOptions to parse the command line options... use Getopt::Long; $autoabbrev=1; $result = GetOptions("template=s", "cache=s", "output=s", "cgi!", "scp=s", "help", "debug"); if ($opt_help or !$result) { use Pod::Text; $Pod::Text::termcap = 1; pod2text("$0"); exit; } # ---------------------------------------------------------------------- my @previous = (); if (-r $opt_cache) { open (IN, $opt_cache); while () { chomp; push @previous, $_; } close(IN); } # ---------------------------------------------------------------------- my $results = ""; srand; use Mac::AppleScript qw(RunAppleScript); $getiTunes = << 'EOF'; tell application "iTunes" if player state is not playing then return "none" end if set current_track to current track set playlist_name to name of current playlist set track_title to name of current_track as string set track_artist to artist of current_track as string set track_album to album of current_track as string set track_comment to comment of current_track as string set track_composer to composer of current_track as string set track_genre to genre of current_track as string set track_year to year of current_track as string return playlist_name & ":" & Â track_title & ":" & track_artist & ":" & Â track_album & ":" & track_composer & ":" & Â track_comment & ":" & track_genre & ":" & track_year end tell EOF my $itunes_output = RunAppleScript($getiTunes); if (! $itunes_output or $itunes_output eq "\"none\"") { my $num = int(rand(4)); if ($num == 1) { $results .= "Currently not playing anything."; } elsif ($num == 2) { $results .= "Not playing anything at all."; } elsif ($num == 2) { $results .= "iTunes isn't playing anything."; } else { $results .= "Not listening to music right now."; } display($results . previous_listing(1) ); exit 0; } $itunes_output =~ s|^\"||; $itunes_output =~ s|\"$||; ( $playlist, $title, $artist, $album, $composer, $comment, $genre, $year ) = split (':', $itunes_output); # ---------------------------------------------------------------------- if (! $previous[0] or $previous[0] ne "$title:$artist") { unshift @previous, "$title:$artist"; # print "DEBUG: @previous\n"; open (OUT, ">$opt_cache"); for (my $i = 0; $i < 7; $i++) { print OUT "$previous[$i]\n"; } close(OUT); } # ---------------------------------------------------------------------- if ($opt_template) { my %var; $var{title} = $title; $var{artist} = $artist; $var{album} = $album; $var{composer} = $composer; $var{comment} = $comment; $var{genre} = $genre; $var{playlist} = $playlist; eval { require Text::Template }; die "We need the Text::Template module if you give it a template\n" if ($@); display( fill_in_file( $template, %var )); } else # Build the output as a string of text { my $g = "O"; $g = "C" if ($genre =~ /^classical/i); $g = "R" if ($genre =~ /^rock/i or $genre =~ /^pop/i or $genre =~ /pop$/i or $genre =~ /^blues/i or $genre =~ /r&b/i or $genre =~ /reggae/i or $genre =~ /disco/i or $genre =~ /alternat/i); $g = "J" if ($genre =~ /^jazz/i or $genre =~ /^fusion/i); # Let's start building up our resulting string ... my $num = int(rand(4)); if ($num == 1) { $results .= "Currently listening to "; } elsif ($num == 2) { $results .= "Currently enjoying "; } elsif ($num == 3) { $results .= "Currently playing "; } else { $results .= "I'm currently listening to "; } if ($g eq "R") { if ($num == 1) { $results .= "Currently listening to "; } elsif ($num == 2) { $results .= "Currently jamming to "; } elsif ($num == 3) { $results .= "Currently spinning "; } else { $results .= "Currently rockin' to "; } } $results .= "Currently groovin' to " if ($genre =~ /disco/i); $results .= "Currently jammin' mon to " if ($genre =~ /reggae/i); if ($composer ne "" and $composer ne $artist) { if ($g eq "C") { $results .= $composer . "'s work, "; } else { $num = int(rand(3)); if ($num == 1) { $results .= $composer . "'s tune, "; } elsif ($num == 2) { $results .= $composer . "'s song, "; } else { $results .= $composer . "'s number, "; } } } $results .= "" . $title . ""; if ($artist) { if (! $composer or $composer eq $artist) { $results .= " by " . $artist; } else { $results .= " as performed by " . $artist; } } if ($album ne "") { $num = int(rand(3)); if ($g eq "C" or $num < 1) { $results .= " from " . $album . ""; } else { if ($year) { $results .= " from the $year album, " . $album . ""; } else { $results .= " from the album, " . $album . ""; } } } if ($comments ne "") { $results .= "\n
$comments
"; } $results .= "\n
[From my “$playlist” playlist]
" if ($playlist and $playlist ne "Library"); display($results . previous_listing(0) ); } # ---------------------------------------------------------------------- # This function will return a string containing the previously # played tracks. It attempts to make the listing more # context-sensitive ... for instance, if all of ths songs are # from the same artist, it won't list the name of the artist # multiple times ... sub previous_listing { my ($type) = @_; my $message; if (@previous) { # Let's do a quick perusal ... my $same_artist = 1; my $doer = $1 if ($previous[0] =~ /:(.*?)$/); $doer = $1 if ($previous[1] =~ /:(.*?)$/); for (my $i = 1; $i < $#previous; $i++) { $same_artist = 0 if ($previous[$i] !~ /$doer$/); } $message .= "\n
\n"; if ($type == 1) { $message .= "However, earlier I was listening to"; } else { $message .= "Previously, I was listening to"; } if ($same_artist == 1) { $message .= " the following $doer songs:\n"; } else { $message .= ":\n"; } $message .= "
    \n"; foreach $line (@previous) { next if (! $line); ($song, $doer) = split(':', $line); next if ($song eq $title and $doer eq $artist); $message .= "
  • $song"; $message .= " - $doer" if ($same_artist != 1); $message .= "
  • \n"; } $message .= "
\n
\n"; } return $message; } # ---------------------------------------------------------------------- # This is the final method that takes the string to output and does # just that ... sub display { my ($message) = @_; # If we are being called as a CGI, then print the standard header # and just output the text ... if ($opt_cgi or $ENV{'SERVER_PROTOCOL'}) { print "Content-Type: text/html; charset=ISO-8859-1\n\n"; print "$message\n"; } # If we were given a file to output the results to ... then write # it out to that file. Does this mean we should transfer it to a # we server as well? elsif ($opt_output) { open (OUT, ">$opt_output") or die "Can't write to $opt_output- $!\n"; print OUT "$message\n"; close(OUT); # TODO: Need to have more ways of transferring the file. # The --scp option should specify the destination. # Note: We can't deal with passwords this way, so you have to # have copied up your public key to the server ... if ($opt_scp) { system ("scp $opt_output $opt_scp >/dev/null"); } } # Otherwise, just print out the contents to the screen ... else { print "$message\n"; } }