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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# included script to provide platter search
# artist, album, song, date, number of random tracks to take,
# scope (misc or album), inventory
#
# returns list of matching lines
package Search;
use Globals;
use Globals qw(@CUSTOM);
use Globals qw($delim);
use Globals qw($debug);
use Globals qw(%PLATTERS);
use strict;
my $artist = undef;
my $title = undef;
my $album = undef;
my $dateFrom = undef;
my $dateTo = undef;
my $scope = undef;
my $random = undef;
my $exclTypes = undef;
my $exact = 0;
my $searchFor = undef; #artist|album|track
my @inv = ();
my @filter = ();
my %ABCS;
return 1;
sub setArtist {
my ($thisartist) = @_;
$thisartist =~ s/^The //i;
$thisartist =~ s/, The$//i;
$artist = $thisartist;
}
sub setTitle {
$title = $_[0];
}
sub setAlbum {
$album = $_[0];
}
sub setDateFrom {
($dateFrom) = @_ unless ($_[0] =~ /^1900$/);
}
sub setDateTo {
($dateTo) = @_ unless ($_[0] =~ /^2525$/);
}
sub setScope {
($scope) = @_;
$scope = 'all' if (!$scope);
}
sub setRandom {
($random) = @_;
}
sub setInv {
@inv = @_;
}
sub setExact {
($exact) = @_;
}
sub setFilter {
@filter = @_;
}
sub setExclTypes {
($exclTypes) = @_;
}
sub searchFor {
($searchFor) = @_;
}
sub go {
my @result = ();
#load abcs
if ($searchFor eq 'artist') { %ABCS = getABCs() }
# decide if we can skip opening the inventories
my $pureArtistSearch = ($searchFor eq 'artist' && !$title && !$album && !$dateFrom && !$dateTo && $scope eq 'all');
# loop through each platter
@inv = keys %PLATTERS if (!defined @inv);
foreach (@inv) {
my $platter = $_;
my $base;
# Check the artist cache
if (open (ARTIST, "../inventory/".$platter.".artist")) {
my $useplatter;
while (<ARTIST>) {
chomp;
my $thisArtist = $_;
if (!$artist ||
($exact && # want exact match
$thisArtist =~ /^\Q$artist\E$/i) ||
(!$exact && # not exact
$thisArtist =~ /\Q$artist\E/i)) {
if (!$exact && # but we weren't doing an exact search
$thisArtist =~ /^\Q$artist\E$/i &&
$searchFor eq 'artist' && $artist) { # bail out just returning the artist name for redirect
return (makeNice($thisArtist));
}
if ($pureArtistSearch) { # store in result
push (@result, makeABC($thisArtist));
}
else { # examine this platter further
$useplatter = 1;
print "Using $platter\n" if ($debug);
last;
}
}
}
next if !$useplatter;
}
if (!$pureArtistSearch) { # Only continue with full inv search if artist cache didn't populate the results
# Open the inventory
open (PLATTER, "../inventory/".$platter.".inv");
while (<PLATTER>) {
chomp;
my $raw = $_;
if ($raw =~ /^http\:\/\//) { # is the base url
$base = $raw;
next;
}
my ($folder, $trackartist, $trackalbum, $year, $track, $url) = split ($delim, $raw);
# apply filter
my $filetype = substr($url, rindex($url, '.')+1);
next if ($exclTypes =~ /$filetype,/);
if ($#filter > -1) {
my $match = 0;
#print "$filetype\n";
foreach( @filter ) {
if (/^$filetype$/i) { $match = 1; last; }
}
if (!$match) { next; }
}
# Match Scope
if ($scope && $scope ne "all") {
if (length ($trackalbum) == 0) {
next if ($scope eq 'album');
}
else {
next if ($scope eq 'misc');
}
}
# Match Track Title
if ($title) {
unless (($exact && # want exact match
$track =~ /^(\d{2} - |)\Q$title\E(\.[^\.]*|)$/i)
||
(!($exact) && # not exact
$track =~ /\Q$title\E/i)) {
next;
}
}
# Match Artist
if ($artist) {
unless ($exact && # want exact match
($folder =~ /^\Q$artist\E$/i || # artist folder
($trackartist =~ /^\Q$artist\E$/i ))
# track artist (not on album searches)
||
(!($exact) && # not exact
($folder =~ /\Q$artist\E/i || # artist folder
$trackartist =~ /\Q$artist\E/i))) { # track artist
next;
}
}
# Set the album to misc if needed
if (!$trackalbum) {
$trackalbum = 'Miscellany';
}
# Match Album Title
if ($album) {
unless (($exact && # want exact match
($trackalbum =~ /^\Q$album\E\//i ||
$album =~ /^\Q$trackalbum\E(\/|$)/i)) # allow subfolders and parents
||
(!$exact && # not exact
$trackalbum =~ /\Q$album\E/i)) {
next;
}
}
# Match Dates
if (($dateFrom || $dateTo)) {
if (defined $year && (($dateFrom && $year < $dateFrom )
|| ($dateTo && $year > $dateTo ))) {
next;
}
}
# Store Result
if ($searchFor eq 'artist') {
if ($folder) {
foreach (@CUSTOM) {
$folder = undef if (/^$folder$/)
}
if ($folder &&
!($trackartist
&& $scope eq 'misc')) {push(@result, makeABC($folder))}
}
if ($trackartist &&
$trackartist ne $folder &&
!($scope eq 'album')) { push(@result, makeABC($trackartist)); }
}
elsif ($searchFor eq 'album') {
my $partalbum = $trackalbum;
while ($partalbum =~ s/(.*)\/.+/$1/) {
my $parentalbum = $1; my $parentyear;
if ($parentalbum =~ / \((\d{4})\)(\/|)/) {
$parentyear = $1;
}
push(@result, "$parentyear$delim$parentalbum$delim$platter");
}
# misc is always merged
my $albumline = "$year$delim$trackalbum$delim";
$albumline.=$platter unless ($trackalbum eq "Miscellany");
push(@result, $albumline);
}
elsif ($searchFor eq 'track') {
push(@result, "$raw$delim$platter");
}
}
close PLATTER;
}
}
# remove duplicates from artist and album searches
#print join ("\n", keys %ABCS);
@result = removeDup(@result) if ($searchFor ne 'track');
# take random selection of results
if ($random>0) {
my %done;
my @randomlist;
if ($random > $#result+1) {
$random = $#result+1;
}
while ($random > 0) {
my $index;
do {
$index = int(rand($#result+1));
} while (exists $done{$index});
push(@randomlist, $result[$index]);
$done{$index} = 1;
$random--;
}
return @randomlist;
}
else {
return @result;
}
}
sub removeDup( @_ ) {
my @result = sort( @_ );
my $last;
my $i=0;
while ($i<=$#result) {
if ($last eq $result[$i]) {
splice @result, $i, 1;
}
else {
$last = $result[$i];
$i++;
}
}
return @result;
}
sub makeABC( @_ ) {
my ($entry) = @_;
$entry =~ s/^\W*//;
if ($ABCS{$entry}) { $entry = ucfirst($ABCS{$entry}.$delim.$entry); }
return $entry;
}
|