J
jean
Here is a script written in PERL that will take the library.xml file
that iTunes outputs in an export and cleans it up. The output from this
script is a clean Text file with the fields TAB delimited.
The script just takes to parameters. The input XML file and the output
text file you want it to write.
Enjoy
Posted By: Jean-Marie Vaneskahian
----------- Begin PERL Code Here -----------
MAIN:
{
my($InputXMLFile, $OutputTXTFile) = @ARGV;
if (-e $InputXMLFile) {
open(File,$InputXMLFile);
open(FileOut,">$OutputTXTFile");
print FileOut join("\t", 'Album', 'Artist', 'Song', 'Track
Number', 'Genre', 'Running Time', 'File Size', 'File Type', 'File
Location') . "\n";
my(%RecordHash);
while (my $Line = <File>) {
chomp($Line);
$Line = &TrimAndDecode($Line);
if ($Line =~ /\<key\>\d+\<\/key\>/) {
if ($RecordHash{'Song'} ne "") {
if ($RecordHash{'Song'} eq "") {
$RecordHash{'Song'} = "Empty Field";
}
if ($RecordHash{'Artist'} eq "") {
$RecordHash{'Artist'} = "Empty Field";
}
if ($RecordHash{'Album'} eq "") {
$RecordHash{'Album'} = "Empty Field";
}
if ($RecordHash{'File Type'} eq "") {
$RecordHash{'File Type'} = "Empty Field";
}
if ($RecordHash{'Genre'} eq "") {
$RecordHash{'Genre'} = "Empty Field";
}
if ($RecordHash{'Track Number'} eq "") {
$RecordHash{'Track Number'} = "0";
}
if ($RecordHash{'File Size'} eq "") {
$RecordHash{'File Size'} = "0";
}
if ($RecordHash{'Running Time'} eq "") {
$RecordHash{'Running Time'} = "0";
}
my $RecordEntry = join("\t", $RecordHash{'Album'},
$RecordHash{'Artist'}, $RecordHash{'Song'}, $RecordHash{'Track
Number'}, $RecordHash{'Genre'}, $RecordHash{'Running Time'},
$RecordHash{'File Size'}, $RecordHash{'File Type'}, $RecordHash{'File
Location'});
print FileOut $RecordEntry . "\n";
}
undef(%RecordHash);
} elsif($Line =~ /\<key\>(.*)\<\/key\>\<.*\>(.*)\<\/.*\>/)
{
my $Field = $1;
my $Value = $2;
if ($Field =~ /Name/i) {
$RecordHash{'Song'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Artist/i) {
$RecordHash{'Artist'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Album/i) {
$RecordHash{'Album'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Genre/i) {
$RecordHash{'Genre'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Kind/i) {
$RecordHash{'File Type'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Size/i) {
$RecordHash{'File Size'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Total Time/i) {
$RecordHash{'Running Time'} =
&TrimAndDecode($Value);
} elsif ($Field =~ /Track Number/i) {
$RecordHash{'Track Number'} =
&TrimAndDecode($Value);
} elsif ($Field =~ /Location/i) {
$Value =~ s/file\:\/\/localhost\///;
$RecordHash{'File Location'} =
&TrimAndDecode($Value);
}
}
}
close(FileOut);
close(File);
}
}
sub TrimAndDecode
{
my($String);
($String) = @_;
$String =~ s/^\s+//;
$String =~ s/\s+$//;
$String =~ tr/+/ /;
$String =~ s/%([a-fA-F0-9]{2})/pack "H2", $1/eg;
$String =~ s/^\s+//;
$String =~ s/\s+$//;
return($String);
}
----------- End PERL Code Here -----------
that iTunes outputs in an export and cleans it up. The output from this
script is a clean Text file with the fields TAB delimited.
The script just takes to parameters. The input XML file and the output
text file you want it to write.
Enjoy
Posted By: Jean-Marie Vaneskahian
----------- Begin PERL Code Here -----------
MAIN:
{
my($InputXMLFile, $OutputTXTFile) = @ARGV;
if (-e $InputXMLFile) {
open(File,$InputXMLFile);
open(FileOut,">$OutputTXTFile");
print FileOut join("\t", 'Album', 'Artist', 'Song', 'Track
Number', 'Genre', 'Running Time', 'File Size', 'File Type', 'File
Location') . "\n";
my(%RecordHash);
while (my $Line = <File>) {
chomp($Line);
$Line = &TrimAndDecode($Line);
if ($Line =~ /\<key\>\d+\<\/key\>/) {
if ($RecordHash{'Song'} ne "") {
if ($RecordHash{'Song'} eq "") {
$RecordHash{'Song'} = "Empty Field";
}
if ($RecordHash{'Artist'} eq "") {
$RecordHash{'Artist'} = "Empty Field";
}
if ($RecordHash{'Album'} eq "") {
$RecordHash{'Album'} = "Empty Field";
}
if ($RecordHash{'File Type'} eq "") {
$RecordHash{'File Type'} = "Empty Field";
}
if ($RecordHash{'Genre'} eq "") {
$RecordHash{'Genre'} = "Empty Field";
}
if ($RecordHash{'Track Number'} eq "") {
$RecordHash{'Track Number'} = "0";
}
if ($RecordHash{'File Size'} eq "") {
$RecordHash{'File Size'} = "0";
}
if ($RecordHash{'Running Time'} eq "") {
$RecordHash{'Running Time'} = "0";
}
my $RecordEntry = join("\t", $RecordHash{'Album'},
$RecordHash{'Artist'}, $RecordHash{'Song'}, $RecordHash{'Track
Number'}, $RecordHash{'Genre'}, $RecordHash{'Running Time'},
$RecordHash{'File Size'}, $RecordHash{'File Type'}, $RecordHash{'File
Location'});
print FileOut $RecordEntry . "\n";
}
undef(%RecordHash);
} elsif($Line =~ /\<key\>(.*)\<\/key\>\<.*\>(.*)\<\/.*\>/)
{
my $Field = $1;
my $Value = $2;
if ($Field =~ /Name/i) {
$RecordHash{'Song'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Artist/i) {
$RecordHash{'Artist'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Album/i) {
$RecordHash{'Album'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Genre/i) {
$RecordHash{'Genre'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Kind/i) {
$RecordHash{'File Type'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Size/i) {
$RecordHash{'File Size'} = &TrimAndDecode($Value);
} elsif ($Field =~ /Total Time/i) {
$RecordHash{'Running Time'} =
&TrimAndDecode($Value);
} elsif ($Field =~ /Track Number/i) {
$RecordHash{'Track Number'} =
&TrimAndDecode($Value);
} elsif ($Field =~ /Location/i) {
$Value =~ s/file\:\/\/localhost\///;
$RecordHash{'File Location'} =
&TrimAndDecode($Value);
}
}
}
close(FileOut);
close(File);
}
}
sub TrimAndDecode
{
my($String);
($String) = @_;
$String =~ s/^\s+//;
$String =~ s/\s+$//;
$String =~ tr/+/ /;
$String =~ s/%([a-fA-F0-9]{2})/pack "H2", $1/eg;
$String =~ s/^\s+//;
$String =~ s/\s+$//;
return($String);
}
----------- End PERL Code Here -----------