#!/usr/local/bin/perl -w
use strict;
use WWW::Mechanize;
use Data::Dumper;
my $agent = WWW::Mechanize->new( autocheck => 1 );
for my $genre (qw(techno punk)) {
print "Genre: $genre\n";
chdir("/home/awwaiid/media/music/garageband/$genre");
$agent->get("http://www.garageband.com/genre/$genre");
my $content = $agent->content;
if($content =~ s/^.*Current $genre Top 10 Chart.*?(
).*$/$1/is) {
$content =~ s/<\/?(font)[^>]*>//igs;
my @artists = ($content =~ m{\s*([^<]+?)\s*}igs);
my @songs = ($content =~ m{\s*([^<]+?)\s*}igs);
while(@songs) {
my $url = shift @songs;
my $title = shift @songs;
my $artist = shift @artists;
$agent->get($url);
$content = $agent->content;
if($content =~ m{"(http://www.garageband.com/mp3/(.*?\.mp3)\?[^"]+)"}) {
my ($song_url, $filename) = ($1, $2);
unless(-e $filename) {
print "Downloading mp3 for $artist - $title\n";
`wget --quiet -nc -O "$filename" "$song_url"`;
if(`file "$filename"` !~ /MP3/) {
print " ... $filename isn't an mp3, removing\n";
`rm "$filename"`;
}
} else {
print "Already have $artist - $title\n";
}
}
}
} else {
print "ERROR: chart not found!\n";
}
}