-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcddb-query.pl
executable file
·603 lines (432 loc) · 15.1 KB
/
cddb-query.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#!/usr/bin/perl -w
# cddb-query.pl
# CDDB search
#use lib '/opt/local/lib/perl5/site_perl/5.16.3' ;
use strict ;
use CGI ;
use English ; #for $PROGRAM_NAME
use Config::Simple ;
#use utf8 ;
#use Text::Unaccent::PurePerl ;
use TokenizeNames ;
use Cddb ;
use CddbMp3 ;
#forward declarations
sub output_header_and_titles ;
sub output_list_args ;
sub output_footer;
sub output_results;
sub print_result_lines ;
sub print_result_albums ;
sub get_disc ;
sub get_track ;
sub pattern_accents ;
sub escape_chars ;
sub sort_i ;
sub lib_tokenize_anchors_artist ;
sub loosen_accent ;
#symbolic constants
my $re_dig = '[[:digit:]]' ; #shorthand; man grep for details
#indexes of fields in the track info list structure
my $IDX_TITLE = 0 ;
my $IDX_ARTIST = 1 ;
my $IDX_COMPOSER = 2 ;
#globals
my $cddb_dir = '/your/cddb/directory/' ;
my $image_dir = "../cddb_images/" ;
my $image_thumbs_dir = "../cddb_images/thumbs/" ;
my $image_favicon_dir = "../cddb_images/favicon/" ;
my $rcfilepath = '.cddbrc' ;
my %composer_of ;
#options
my $g_debug = 1 ;
my $g_is_admin = 0 ;
##############################################################
# main starts here
my $cfg = new Config::Simple('.cddbrc') ;
$cddb_dir = $cfg->param('cddb_dir') ;
#who am I?
my $cgi_url = $PROGRAM_NAME ;
$cgi_url =~ s|(.*/)|| ;
# Get the CGI variables
my $cgi = CGI->new() ;
my @names = $cgi->param ;
my $server_name = $cgi->server_name() ;
my $remote_addr = $cgi->remote_addr() ;
my $remote_host = $cgi->remote_host() ;
$g_is_admin = 1 if $remote_addr eq 'localhost' ;
my $artist = $cgi->param('artist') ;
my $title = $cgi->param('title') ;
my $composer = $cgi->param('composer') ;
my $sortby = $cgi->param('sortby') ;
$cgi->charset('utf-8');
###############################
# start output here
my $doc_title = "CDDB Query" ;
print $cgi->header ;
print $cgi->start_html(
-title=>$doc_title,
-style=>'../css/cddb.css',
-script=>[
{ -type => 'text/javascript',
-src => '../js/sorttable.js'
}
]) ;
print '<div id="program_description">' ;
print $cgi->h1($doc_title) ;
#print "<h2>\$remote_addr:$remote_addr</h2>\n" if $g_debug ;
#print "<h2>\$remote_host:$remote_host</h2>\n" if $g_debug ;
print '</div>' ;
#run in different modes
if (!@names) { #no query, just input form
print_query_form() ;
} else { #process query
print_query_form() ;
my ($tag, $querystring, $query_albums) ;
my @queries = create_queries() ;
my $grep_cmd_query = pop @queries ;
($tag, $querystring) = split "\t", $grep_cmd_query ;
my $grep_cmd_tracks = grep_command_line($tag, $querystring, 0) ;
my $grep_cmd_albums = grep_command_line($tag, $querystring, 1) ;
print '<div id="found_tracks">' ;
print $cgi->h2('Tracks') ;
print "<p><code>$grep_cmd_tracks</code><p>" if $g_debug ;
my @found_grep = grep_results($grep_cmd_tracks) ;
#push @found_grep, $tag ;
my @found_tracks = grep_output_to_trackinfo(\@found_grep, $tag) ;
foreach my $query (@queries) {
@found_tracks = apply_query(\@found_tracks, $query) ;
}
my @sorted_tracks ;
my $sortref = sort_func($tag) ;
@sorted_tracks = sort $sortref @found_tracks ;
print_result_tracks(@sorted_tracks) ;
print '</div>' ;
my @found_albums ;
@found_albums = grep_results($grep_cmd_albums) ;
my $ignore_category = 'user' ;
@found_albums = grep (!/\/user\//, @found_albums) ;
print '<div id="found_albums">' ;
print $cgi->h2('Albums') ;
print $cgi->code($grep_cmd_albums) if $g_debug ;
print_result_albums(@found_albums) ;
print '</div>' ;
}
print $cgi->end_html ;
##### end of main
##############################################################
sub print_query_form {
$title = '' if !$title ;
$composer = '' if !$composer ;
$artist = '' if !$artist ;
print<<END_HERE
<div id="query_form">
<form method=GET class="CDDBQuery">
<div>
Title: <input name='title' value="$title">
</div>
<div>
Artist: <input name='artist' value="$artist">
</div>
<div>
Composer: <input name='composer' value="$composer">
</div>
<input type=submit value='Search'>
</form>
</div>
END_HERE
}
sub create_queries {
#return a list of references to query structures, which are tag-querystring pairs.
my ($tag, $querystring) ;
my @queries = () ;
# Order is important here. Since an artist query may return albums but no tracks,
# further queries on composer or title will not work,
# even though we want to query on composer/title for all the tracks in that album.
# So we always place artist at the bottom of the stack.
# Title will probably return fewer hits than composer, so we place that at the top.
push @queries, "TARTIST\t$artist" if ($artist) ;
push @queries, "EXTT\t$composer" if ($composer) ;
push @queries, "TTITLE\t$title" if ($title) ;
return @queries ;
}
sub apply_query {
my ($tracksref, $query) = @_ ;
my @trackinfo_refs = @{$tracksref} ;
my($tag, $querystring) = split "\t", $query ;
my $idx ;
$idx = $IDX_TITLE if ($tag eq 'TTITLE') ;
$idx = $IDX_ARTIST if ($tag eq 'TARTIST') ;
$idx = $IDX_COMPOSER if ($tag eq 'EXTT') ;
# foreach my $ref (@trackinfo_refs) {
# my @track = @{$ref} ;
# print join ':', @track ;
# print '<br>' ;
# }
return grep {
my @track = @{$_} ;
$track[$idx] =~ /$querystring/i ;
} @trackinfo_refs ;
}
sub grep_command_line {
#tags
my($tag, $querystring, $is_album_query) = @_ ;
$querystring = escape_chars(loosen_accent(loosen_punctuation($querystring))) ;
#also escape parentheses
my $query_pattern ;
if ($is_album_query) {
if ($tag eq 'TTITLE' or $tag eq 'EXTT') { # Look for composer in disc title, e.g., "Eliane Elias / Plays the Songs of Jobim"
$query_pattern = "\"DTITLE=.* / .*$querystring\"" ;
} elsif ($tag eq 'TARTIST') {
$query_pattern = "\"DTITLE=.*$querystring.* / \"" ;
}
} else {
if ($querystring =~ /\^(.+)/) {
$query_pattern = "\"$tag$re_dig$re_dig?=\\(?$1\"" ; #optional parentheses wrapping composer
} else {
$query_pattern = "\"$tag$re_dig$re_dig?=.*$querystring\"" ;
}
}
my $cddb_spec = '*' ; #genre subdirectories
# my $locale_spec = qq(LANG="ja_JP.UTF-8") ;
#TODO: maybe use Perl locale module?
# my $locale_spec = qq(LANG="en_US.UTF-8") ;
my $locale_spec = qq(LANG="pt_BR.UTF-8") ;
return qq(export $locale_spec ; egrep -i -d recurse -e $query_pattern $cddb_dir$cddb_spec) ;
#return qq(egrep -i -d recurse -e $query_pattern $cddb_dir$cddb_spec) ;
}
sub grep_results {
my $egrep_cmd = shift ;
#filter out backup files. Or make grep recurse with a filter on filenames
my @found_grep = grep m|/[0-9a-z]{8}:|, `$egrep_cmd` ;
return @found_grep ;
}
sub output_list_args {
print "<ol>\n" ;
foreach my $name (@names) {
my $val = $cgi->param($name) ;
print "<li>$name : $val\n" ;
}
print "</ol>\n" ;
}
sub get_track_info {
# Given a line of output from a grep search of CDDB files, return a list of all CDDB info for that track:
# cddb_path, title, artist, composer, album, track_number
my ($tag, $grepline) = @_ ;
#print "$tag :: $grepline<br>" ;
#return ;
#parse a line of grep output with found TTITLE/TARTIST/EXTT
my($cddb_path, $track_num, $query_match) = split /:$tag|=/, $grepline ;
my $album = get_disc('DTITLE', $cddb_path) ;
my ($album_artist, $album_title) = split ' / ', $album ;
my($title, $artist, $composer) ;
if ($tag eq 'EXTT') {
$composer = $query_match ;
} else {
$composer = get_track_line_value('EXTT', $cddb_path, $track_num) ;
}
if ($tag eq 'TTITLE') {
$title = $query_match ;
} else {
$title = get_track_line_value('TTITLE', $cddb_path, $track_num) ;
}
if ($tag eq 'TARTIST') {
$artist = $query_match ;
} else {
$artist = get_track_line_value('TARTIST', $cddb_path, $track_num) ;
}
$composer =~ tr/\(\)//d ; #composers in EXTT are enclosed in parentheses
if (!$artist) { $artist = $album_artist ; }
my $uniq = 0 ; #Hack
if($uniq) {
$title =~ s/( \(.*\))// ; # strip out alternate titles
$title =~ s/( \[.*\])// ; # strip out track info
if ($composer_of{uc $title}) {
next ;
} else {
$composer_of{uc $title} = $composer ;
}
}
$track_num++ ; #done with indexing, so now normalize to 1-based for printing
return($title, $artist, $composer, $album, $track_num, $cddb_path) ;
}
sub sort_by_title { @{$a}[$IDX_TITLE] cmp @{$b}[$IDX_TITLE]; }
sub sort_by_artist { @{$a}[$IDX_ARTIST] cmp @{$b}[$IDX_ARTIST]; }
sub sort_by_composer { @{$a}[$IDX_COMPOSER] cmp @{$b}[$IDX_COMPOSER]; }
sub grep_output_to_trackinfo
#convert array of string output from grep to a list of track info records
{
# my $tag = pop @_ ;
# my @foundgrep = @_ ;
my($foundgrep_ref, $tag) = @_ ;
my @foundgrep = @$foundgrep_ref ;
@foundgrep or return ;
my @tracks = () ;
foreach my $grepline (@foundgrep) {
chomp $grepline ;
# $grepline =~ s/^$cddb_dir// ;
#parse the grep output
#$cddb_path, $title, $artist, $composer, $album, $track_num
my $cddb_path = (split(':', $grepline))[0] ;
#the "user" directory is created by libcddb for genres that do not fit into their predifined
#list of music genres.
#I use it only to alias to other genres, so skip those softlinks
if($cddb_path =~ m|/user/|) { next ; }
my @info = get_track_info($tag, $grepline) ;
push @tracks, \@info ;
}
return @tracks ;
}
sub sort_func
{
my $sortref ;
my $tag = shift ;
#first let the tag determine the sort
$sortref = \&sort_by_title if ($tag eq 'TTITLE') ;
$sortref = \&sort_by_artist if ($tag eq 'TARTIST') ;
$sortref = \&sort_by_composer if ($tag eq 'EXTT') ;
$sortby = 'title' unless $sortby ;
#but let it be overridden with the 'sortby' param
if ($sortby eq 'title') {
$sortref = \&sort_by_title ;
}
if ($sortby eq 'artist') {
$sortref = \&sort_by_artist ;
}
if ($sortby eq 'composer') {
$sortref = \&sort_by_composer ;
}
return $sortref ;
}
sub print_result_tracks
{
# print results in trackinfo list format as an HTML table
#
my @tracks = @_ ;
my @output_lines ;
##########
##########
foreach my $inforef (@tracks) {
my ($title, $artist, $composer, $album, $track_num, $cddb_path) = @{$inforef} ;
my $cddb = $cddb_path ;
$cddb =~ s|.+/|| ;
my $thumbnail_path = $image_thumbs_dir . $cddb . '_th.png';
#print $thumbnail_path ;
my $thumbnail_link = '' ;
if(-f $thumbnail_path) {
$thumbnail_link = "<img src=\"$thumbnail_path\">" ;
}
my $cddb_genre_and_id = Cddb::genre_and_id($cddb_path) ;
my $album_view_anchor = $thumbnail_link . "<div><a href=cddb-format.pl?cddb=$cddb_genre_and_id>$album</a></div>" ;
my @artist_and_album = split(' / ', $album) ;
my $track_num_zero = sprintf("%02d", $track_num) ;
my $mp3_path = CddbMp3::find_mp3_file($artist_and_album[0], $artist_and_album[1], $track_num_zero, $title) ;
my $mp3_anchor = '' ;
if (-f $mp3_path) {
$mp3_anchor = "<a href=\"$mp3_path\">[mp3]</a>" ;
}
my $title_html =
'<b>' .
tokenize_anchors_title($title) .
$mp3_anchor
#
. '</b>' ;
my $composer_html = '<i>'.tokenize_anchors_composer($composer).'</i>' ;
my $artist_html = tokenize_anchors_artist($artist) ;
my $li_html ;
my $checkbox = '';
$checkbox = "<input type='checkbox' name=\"$cddb\">" if $g_is_admin ;
# $li_html = "<li>$title_html : $composer_html : $artist_html -- $disc_html [$track_num] : \n" ;
$li_html = "<tr>" .
"<td>$title_html</td>" .
"<td>$composer_html</td>" .
"<td>$artist_html</td>" .
"<td>$checkbox$album_view_anchor</td>" .
"<td>$track_num</td>" .
'</tr>' ;
my $uniq = 0 ; #Hack
# if (!$uniq) { $li_html .= " : <a href=\"cddb-query.pl?artist=$artist\">$artist</a>" }
push @output_lines, "$li_html" ;
}
#
print "<form method=GET action=\"cddb-collect.cgi\">\n" ;
my ($header_anchor_title, $header_anchor_composer, $header_anchor_artist) ;
#$header_anchor_title = "<a href=\"cddb-query.pl?title=$title&artist=$artist&composer=$composer&sortby=title\">" ;
#$header_anchor_composer = "<a href=\"cddb-query.pl?title=$title&artist=$artist&composer=$composer&sortby=composer\">" ;
#$header_anchor_artist = "<a href=\"cddb-query.pl?title=$title&artist=$artist&composer=$composer&sortby=artist\">" ;
my ($classtag_title, $classtag_composer, $classtag_artist) = ('', '', '');
#$classtag_artist =' class="SortKey"' if($sortby eq 'artist') ;
#$classtag_composer =' class="SortKey"' if($sortby eq 'composer') ;
#$classtag_title =' class="SortKey"' if($sortby eq 'title') ;
print '<table class="sortable" id="found_tracks">' ;
#table header
print "\t<tr>" ;
print "<th>Title</th>" ;
print "<th>Composer</th>" ;
print "<th>Artist</th>" ;
print "<th>Album</th><th>Track No.</th></tr>\n" ;
print join '', @output_lines ;
print "</table>\n" ;
print "<input type=submit value='Add to collection'>\n" ;
print "</form>\n" ;
}
sub print_result_albums()
{
my $sep = '{' ; #something that will never appear in an artist or album name
my @albums = @_ ;
my @sorted = sort sort_i map { local $_ = $_ ; s|(.+):DTITLE=(.+) / (.+)|$2$sep$3$sep$1| ; $_ } @albums ;
print "<ul>";
foreach my $line (@sorted) {
print '<li>';
$line =~ m|(.+)$sep(.+)$sep(.+)| ;
my ($artist, $album, $cddb_path) = ($1, $2, $3) ;
my $cddb = $cddb_path ;
$cddb =~ s/.*\/// ;
my $thumbnail_path = $image_thumbs_dir . $cddb . '_th.png';
#print $thumbnail_path . "<br/>" ;
my $thumbnail_link = '' ;
if(-f $thumbnail_path) {
$thumbnail_link = "<img src=\"$thumbnail_path\">" ;
print $thumbnail_link ;
}
my $cddb_genre_and_id = Cddb::genre_and_id($cddb_path) ;
print '<div>' ;
print tokenize_anchors_artist($artist) ;
print " : <a href=\"cddb-format.pl?cddb=${cddb_genre_and_id}\">" ;
print $album ;
print '</a>' ;
print '</div>' ;
print '</li>' ;
}
print "</ul>";
}
sub get_track_line_value() {
my ($tag, $cddb_path, $track_num) = @_ ;
my $pat = "$tag$track_num=" ;
# some EXTTs are multi-line
my $title = join('', `grep --text $pat "$cddb_path"`) ;
#$title = "PAT: $pat" ;
# $title = "grep $pat $cddb_path" ;
# print $title ;
chomp $title ;
$title =~ s/\n//g ;
$title =~ s/\\n/\n/g ;
$title =~ s/$pat//g ;
return $title ;
}
sub get_disc()
{
my ($tag, $cddb_path) = @_ ;
my $disc = `grep --text $tag "$cddb_path"` ;
chomp $disc ;
$disc =~ s/$tag=// ;
return $disc ;
}
sub escape_chars() {
my $string = shift ;
$string =~ s/\(/\\(/g ;
$string =~ s/\)/\\)/g ;
return $string ;
}
sub sort_i { lc($a) cmp lc($b);
}