All pastes #1249785 Raw Edit

Download script

public text v1 · immutable
#1249785 ·published 2008-11-09 21:07 UTC
rendered paste body
#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;
use Music::Tag;
use WWW::Mechanize;

my $url = 'http://www.machinaesupremacy.com/masusite/flacmusic.html';

my $bot = WWW::Mechanize->new(
   agent => 'ELinks/0.12',
   autocheck => 1,
);

$bot->get($url);

my $root = "machinae_supremacy";
mkdir $root if not -d $root;

for my $link ($bot->links) {
   if ($link->url =~
         m!
            files/
            (.+?)_compilation # Album
            /flac/
            (\d+)-machinae_supremacy-(.+?) # Track
            \.flac
         !x) {
      my $album = fix($1);
      my $track = $2;
      my $title = fix($3);

      my $dir = "$root/$album";
      mkdir $dir if not -d $dir;

      my $filename = "$dir/$track".'_'."$title.flac";

      my $url = $link->url_abs->as_string;

      # Download the file
      print "Downloading '".$url."'\n";
      $bot->get($url, ":content_file" => $filename);

      # Tag the file properly
      my $info = Music::Tag->new($filename, {}, "FLAC");
      $info->get_tag();

      $info->strip_tag;
      $info->artist('Machinae Supremacy');
      $info->album(fix_tag($album));
      $info->genre('Bitpop');
      $info->track($track);
      $info->title(fix_tag($title));

      $info->set_tag;
      $info->close;
   }
}

sub fix {
   my ($str) = @_;
   
   for ($str) {
      s/(\w)/\l$1/g;
      tr/ \-'./____/;
   }

   return $str;
}

sub fix_tag {
   my ($str) = @_;
   
   for ($str) {
      s/_(\w)/ \u$1/g;
      s/^(\w)/\u$1/;
   }

   return $str;
}