# Blosxom Plugin: allmusic
# Author: Ben Blackburne (bpb@lysozyme.net)
package allmusic;
use LWP::Simple;
use FileHandle;
my $fh = new FileHandle;
# --- Configurable variables -----
#directory to cache info in
my $album_dir = "$blosxom::plugin_state_dir/allmusic";
$albumfile= "$blosxom::datadir/amg";
$imgsize="75";
# --------------------------------
sub start {
$fh->open("<$albumfile");
@albums= <$fh>;
chomp @albums;
for (my $a=0; $a < @albums; $a++){
#remove comments and whitespace;
$albums[$a]=~s/\#.*//;
if ($albums[$a]=~/^T\ /i){$albums[$a]=~s/^T\ //i; $type{$albums[$a]}="T";}
if ($albums[$a]=~/^C\ /i){$albums[$a]=~s/^C\ //i; $type{$albums[$a]}="C";}
$albums[$a]=~s/^the\ //i;
$albums[$a]=~s/\s//g;
}
$fh->close;
if ( !-e $album_dir ) {
my $mkdir_r = mkdir("$album_dir", 0755);
$mkdir_r or return 0;
}
foreach $album (@albums){
if ( $fh->open("<$album_dir/$album") ) {
$display= $display.<$fh>;
$fh->close;
}
else {
if ($fh->open(">$album_dir/$album") ){
my $data;
if ($type{$album} eq 'C'){
$data = get("http://www.allmusic.com/cg/amg.dll?p=amg&sql=$album");
}
else {
$data = get("http://www.allmusic.com/cg/amg.dll?p=amg&opt1=2&sql=$album");
}
if ($data=~/albums with titles like/){#fail
print STDERR "Album $album not found. Try searching manually\n";
next;
}
($artist,$title,$image) = amgparse($data);
if ($type{$album} eq 'C'){
$output="
";
}else
{
$output="
";
}
print $fh $output;
$display=$display.$output;
$fh->close;
}
}
}
return 1;
}
sub amgparse {
my $data=$_[0];
$data=~s/\n//g;
my $artist=$data;
my $image=$data;
my $title=$data;
$image=~s/(http:\/\/image.allmusic.com\S*.jpg).*/$1/;
$image=~s/.*(http:\/\/image)/$1/;
$title=~s/.*