#!/sw/arch/bin/perl -w use strict; use WWW::Mechanize; use Mac::AppleScript::Glue; my $mech = WWW::Mechanize->new(); my $controller = new Mac::AppleScript::Glue::Application('iTunes'); my $tracks = $controller->selection; my %cache; foreach my $track (@$tracks){ my $name = $track->name; my $artist = $track->artist; if ($artist !~ /Various Artists/i){ my $album = $track->album; my $comments; if (!exists $cache{$artist}{$album}){ $comments = getTones($artist,$album,$mech); $cache{$artist}{$album} = $comments; } else { $comments = $cache{$artist}{$album} } if ($comments){ my $current_comments = $track->comment; if ($current_comments =~ $comments){ print "Comments already set for '$name' from '$album' by '$artist'\n"; } else { if ($current_comments){ $current_comments .= "\n"; } $track->set(comment => $current_comments.$comments); print "Set '$name' from '$album' by '$artist' to '$comments'\n"; } } else { print "Couldn't find '$album' by '$artist'\n"; } } } sub getTones{ my ($artist,$album,$mech) = @_; my $url = "http://www.allmusic.com/index.html"; my @tones; $mech->get($url); $mech->form_name('d'); $mech->field('sql',$artist); $mech->field('opt1','1','1'); $mech->submit(); my $html = $mech->content(); if ($html =~ /artists with names like/){ #We need to select artist my $link = $mech->find_link( text_regex => qr/$artist/i ); if (!$link){ return 0 } my ($encrypt) = $link->url() =~ /javascript:z\(\'(\S+?)\'/; $url = "http://www.allmusic.com/cg/amg.dll?p=amg&sql=$encrypt"; $mech->get($url); $html = $mech->content(); } #Set tones from artist page if ($html =~ /Tones <\S+?><.+?>(.+?)find_link( text_regex => qr/$album.*/i ); if ($link){ my ($encrypt) = $link->url() =~ /javascript:z\(\'(\S+?)\'/; $url = "http://www.allmusic.com/cg/amg.dll?p=amg&sql=$encrypt"; $mech->get($url); $html = $mech->content(); if ($html =~ /Tones <\S+?><.+?>(.+?)