-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathcheck-cds.pl
129 lines (113 loc) · 3.29 KB
/
check-cds.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/perl
use strict;
use warnings;
my $usage = "Usage:
$0 -h|--help
print this message
$0 [OPTS] [REGEX]
query klomp-db for each line in 'cds'
ensure at least one FLAC file, with an acoustid, is found for each cd
-look at all lines in './cds'
-if REGEX is given, skip all lines that do not match REGEX
-parse each line as one of these formats:
*<ALBUM> (one section)
*<ARTIST>|<ALBUM> (two sections, none empty)
*<ARTIST>|<ALBUM>|<EXTRAS> (three or more sections, none empty)
*<ALBUM>||<EXTRAS> (three or more sections, second is empty)
-if line does not match one of the above formats, FAIL
-if line does not start with '*', FAIL
-split <ARTIST> (if present) into words (\\w+) and prepand '\@a'
-split <ALBUM> into words (\\w+) and prepend '\@l'
-run klomp-db -s '<ARTIST_WORDS> <ALBUM_WORDS'
e.g.: klomp-db -s '\@aEdith \@aPiaf \@lLa \@lVie \@len \@lRose'
-ignore all non-flac songs returned
-if no songs returned, FAIL
-check each song returned for an acoustid
-if no acoustids returned, FAIL
OPTS
-v | --verbose
print the klomp-db lookup query
";
sub main(@){
if(@_ == 1 and $_[0] =~ /^(-h|--help)$/){
print $usage;
exit 0;
}
my $verbose = 0;
while(@_ > 0 and $_[0] =~ /^-/){
my $opt = shift;
if($opt =~ /^(-v|--verbose)$/){
$verbose = 1;
}else{
die "$usage\nunknown opt: $opt\n";
}
}
my $regex;
if(@_ == 0){
$regex = undef;
}elsif(@_ == 1){
$regex = $_[0];
}else{
die $usage;
}
for my $cd(`cat cds`){
chomp $cd;
next if $cd =~ /^\s*$/;
if(defined $regex and $cd !~ /$regex/i){
print " SKIPPED: $cd\n";
next;
}
if($cd =~ /^[^*]/){
die "cd not marked as raptured: $cd\n";
}
if($cd !~ /^\*([^|]+)(?:\|([^|]*))?(?:\|(.*))?$/){
die "malformed cd: $cd";
}else{
my ($part1, $part2, $extras) = ($1, $2, $3);
my ($album, $artist);
if(defined $part2 and length $part2 > 0){
$artist = $part1;
$album = $part2;
}else{
$album = $part1;
$artist = "";
}
my @artistWords = split /\W+/, $artist;
my @albumWords = split /\W+/, $album;
my @queryWords;
@queryWords = (@queryWords, map {"\@a$_"} @artistWords);
@queryWords = (@queryWords, map {"\@l$_"} @albumWords);
my @cmd = ("klomp-db", "-s", "@queryWords");
print "@cmd\n" if $verbose;
open CMD, "-|", @cmd
or die "could not run @cmd\n";
my @songs = <CMD>;
close CMD;
chomp foreach @songs;
@songs = grep /\.flac$/, @songs;
if(@songs == 0){
die "missing CD: $cd\n";
}
my $anyAcoustid;
for my $song(@songs){
open CMD, "-|", "klomp-db", "-i", $song
or die "could not run klomp-db\n";
my $info = join '', <CMD>;
close CMD;
my $acoustid = $1 if $info =~ /acoustid=(.*)/;
if(defined $acoustid and $acoustid =~ /^[0-9a-f\-]+$/){
$anyAcoustid = $acoustid;
last;
}
}
if(not defined $anyAcoustid){
die "no acoustids: $cd\n";
}
print "$cd\n";
print "$_\n" foreach @songs;
print "\n\n\n";
}
}
print "SUCCESS\n";
}
&main(@ARGV);