Make perlcritic tests pass
[app-musicexpo.git] / lib / App / MusicExpo.pm
CommitLineData
7eb41a1b 1package App::MusicExpo;
a5dd5c0d 2use 5.014000;
75c564d8 3use strict;
21cd9240
MG
4use warnings;
5
1904b54c 6our $VERSION = '1.002001';
7eb41a1b 7
21cd9240
MG
8use HTML::Template::Compiled qw//;
9use Memoize qw/memoize/;
21cd9240 10
fb40d666 11use Carp qw/carp/;
21cd9240 12use DB_File qw//;
b8e18bed 13use Encode qw/encode/;
21cd9240
MG
14use File::Basename qw/fileparse/;
15use Fcntl qw/O_RDWR O_CREAT/;
16use Getopt::Long;
17use Storable qw/thaw freeze/;
a535e879 18use sort 'stable';
21cd9240
MG
19
20##################################################
21
75c564d8
MG
22my $default_template;
23
21cd9240 24our $prefix='/music/';
fb015c82 25our $cache='';
75c564d8 26our $template='';
21cd9240
MG
27
28GetOptions (
a5dd5c0d
MG
29 'template:s' => \$template,
30 'prefix:s' => \$prefix,
31 'cache:s' => \$cache,
21cd9240
MG
32);
33
21cd9240 34sub flacinfo{
6a1f7df9
MG
35 my $file=$_[0];
36 my $flac=Audio::FLAC::Header->new($file);
6a1f7df9
MG
37
38 freeze +{
39 format => 'FLAC',
b484a129
MG
40 title => $flac->tags('TITLE'),
41 artist => $flac->tags('ARTIST'),
42 year => $flac->tags('DATE'),
43 album => $flac->tags('ALBUM'),
44 tracknumber => $flac->tags('TRACKNUMBER'),
45 tracktotal => $flac->tags('TRACKTOTAL'),
46 genre => $flac->tags('GENRE'),
b8e18bed 47 file => scalar fileparse $file,
6a1f7df9 48 }
21cd9240
MG
49}
50
51sub mp3info{
6a1f7df9 52 my $file=$_[0];
f7964165 53 my %tag = map { encode 'UTF-8', $_ } %{MP3::Info::get_mp3tag $file};
a5dd5c0d 54 my @trkn = split m#/#s, $tag{TRACKNUM} // '';
6a1f7df9
MG
55
56 freeze +{
57 format => 'MP3',
b484a129
MG
58 title => $tag{TITLE},
59 artist => $tag{ARTIST},
60 year => $tag{YEAR},
61 album => $tag{ALBUM},
62 tracknumber => $trkn[0],
63 tracktotal => $trkn[1],
64 genre => $tag{GENRE},
b8e18bed 65 file => scalar fileparse $file,
6a1f7df9 66 }
21cd9240
MG
67}
68
513a3718 69sub vorbisinfo{
6a1f7df9
MG
70 my $file=$_[0];
71 my $ogg=Ogg::Vorbis::Header::PurePerl->new($file);
6a1f7df9
MG
72
73 freeze +{
74 format => 'Vorbis',
fbeeac22
MG
75 title => scalar $ogg->comment('TITLE'),
76 artist => scalar $ogg->comment('artist'),
77 year => scalar $ogg->comment('DATE'),
78 album => scalar $ogg->comment('ALBUM'),
79 tracknumber => scalar $ogg->comment('TRACKNUMBER'),
80 tracktotal => scalar $ogg->comment('TRACKTOTAL'),
81 genre => scalar $ogg->comment('GENRE'),
b8e18bed 82 file => scalar fileparse $file,
6a1f7df9 83 }
513a3718
MG
84}
85
a5dd5c0d 86sub mp4_format ($){ ## no critic (ProhibitSubroutinePrototypes)
ddd1adc5
MG
87 my $encoding = $_[0];
88 return 'AAC' if $encoding eq 'mp4a';
89 return 'ALAC' if $encoding eq 'alac';
90 "MP4-$encoding"
91}
92
93sub mp4info{
94 my $file=$_[0];
f7964165
MG
95 my %tag = map { ref() ? $_ : encode 'UTF-8', $_ } %{MP4::Info::get_mp4tag $file};
96 my %info = %{MP4::Info::get_mp4info $file};
ddd1adc5
MG
97
98 freeze +{
99 format => mp4_format $info{ENCODING},
b484a129
MG
100 title => $tag{TITLE},
101 artist => $tag{ARTIST},
102 year => $tag{YEAR},
103 album => $tag{ALBUM},
104 tracknumber => $tag{TRACKNUM},
105 tracktotal => ($tag{TRKN} ? $tag{TRKN}->[1] : undef),
106 genre => $tag{GENRE},
b8e18bed 107 file => scalar fileparse $file,
ddd1adc5
MG
108 };
109}
110
8c158a92
MG
111sub opusinfo {
112 my $file = $_[0];
e2ce550b
MG
113 my $of = Audio::Opusfile->new_from_file($file);
114 my $tags = $of->tags;
8c158a92
MG
115
116 my %data = (
117 format => 'Opus',
e2ce550b
MG
118 title => $tags->query('TITLE'),
119 artist => $tags->query('ARTIST'),
120 year => $tags->query('DATE'),
121 album => $tags->query('ALBUM'),
122 tracknumber => $tags->query('TRACKNUMBER'),
123 tracktotal => $tags->query('TRACKTOTAL'),
124 genre => $tags->query('GENRE'),
fb40d666 125 file => scalar fileparse $file,
8c158a92
MG
126 );
127
128 freeze \%data;
129}
130
f7964165
MG
131my @optional_modules = (
132 [ 'Audio::FLAC::Header', \&flacinfo, '.flac' ],
133 [ 'MP3::Info', \&mp3info, '.mp3' ],
134 [ 'Ogg::Vorbis::Header::PurePerl', \&vorbisinfo, '.ogg', '.oga' ],
135 [ 'MP4::Info', \&mp4info, '.mp4', '.aac', '.m4a' ],
fb40d666 136 [ 'Audio::Opusfile', \&opusinfo, '.opus' ],
3f12a322
MG
137);
138
f7964165
MG
139my %info;
140
141for (@optional_modules) {
142 my ($module, $coderef, @extensions_handled) = @$_;
143 if (eval "require $module") {
144 $info{$_} = $coderef for @extensions_handled
145 }
146}
147
148unless (%info) {
fb40d666 149 carp 'No tags-reading module detected. Install one of the following modules: ' . join ', ', map { $_->[0] } @optional_modules;
f7964165
MG
150}
151
21cd9240 152sub normalizer{
6a1f7df9 153 "$_[0]|".(stat $_[0])[9]
21cd9240
MG
154}
155
447902f9
MG
156sub make_fragment{ join '-', map { lc =~ y/a-z0-9/_/csr } @_ }
157
f7964165
MG
158sub extensions_handled { keys %info }
159
21cd9240 160sub run {
cc5d06b5 161 if ($cache) {
a5dd5c0d 162 tie my %cache, 'DB_File', $cache, O_RDWR|O_CREAT, 0644; ## no critic (ProhibitTie)
3f12a322 163 $info{$_} = memoize $info{$_}, INSTALL => undef, NORMALIZER => \&normalizer, LIST_CACHE => 'FAULT', SCALAR_CACHE => [HASH => \%cache] for keys %info;
cc5d06b5 164 }
6a1f7df9
MG
165
166 my %files;
167 for my $file (@ARGV) {
3f12a322 168 my ($basename, undef, $suffix) = fileparse $file, keys %info;
f7964165 169 next unless $suffix;
6a1f7df9 170 $files{$basename} //= [];
3f12a322 171 push @{$files{$basename}}, thaw scalar $info{$suffix}->($file);
6a1f7df9
MG
172 }
173
174 my $ht=HTML::Template::Compiled->new(
175 default_escape => 'HTML',
176 global_vars => 2,
177 $template eq '' ? (scalarref => \$default_template) : (filename => $template),
178 );
179
180 my @files;
a535e879
MG
181 for (sort keys %files) {
182 my @versions = @{$files{$_}};
b484a129 183 my %entry = (formats => [], map { $_ => '?' } qw/title artist year album tracknumber tracktotal genre/);
9d6d04a4 184 for my $ver (@versions) {
c08ff73c
MG
185 push @{$entry{formats}}, {format => $ver->{format}, file => $ver->{file}};
186 for my $key (keys %$ver) {
a71c6c1e 187 $entry{$key} = $ver->{$key} if $ver->{$key} && $ver->{$key} ne '?';
9d6d04a4
MG
188 }
189 }
190 delete $entry{$_} for qw/format file/;
447902f9 191 $entry{fragment} = make_fragment @entry{qw/artist title/};
6a1f7df9
MG
192 push @files, \%entry
193 }
194
6dcbbf44 195 @files = sort { $a->{title} cmp $b->{title} } @files;
95979388 196 $ht->param(files => \@files, prefix => $prefix);
a5dd5c0d 197 print $ht->output; ## no critic (RequireCheckedSyscalls)
21cd9240
MG
198}
199
75c564d8
MG
200$default_template = <<'HTML';
201<!DOCTYPE html>
202<title>Music</title>
203<meta charset="utf-8">
de93afc5 204<link rel="stylesheet" href="musicexpo.css">
9b989f96
MG
205<script async defer type="application/javascript" src="player.js"></script>
206
207<div id="player"></div>
75c564d8
MG
208
209<table border>
210<thead>
211<tr><th>Title<th>Artist<th>Album<th>Genre<th>Track<th>Year<th>Type
212<tbody><tmpl_loop files>
447902f9 213<tr><td class="title"><a href="#<tmpl_var fragment>" data-hash="#<tmpl_var fragment>"><tmpl_var title></a><td class="artist"><tmpl_var artist><td class="album"><tmpl_var album><td class="genre"><tmpl_var genre><td class="track"><tmpl_var tracknumber>/<tmpl_var tracktotal><td class="year"><tmpl_var year><td class="formats"><tmpl_loop formats><a href="<tmpl_var ...prefix><tmpl_var ESCAPE=URL file>"><tmpl_var format></a> </tmpl_loop></tmpl_loop>
75c564d8
MG
214</table>
215HTML
216
21cd9240
MG
2171;
218
219__END__
220
bb86a416
MG
221=encoding utf-8
222
21cd9240
MG
223=head1 NAME
224
225App::MusicExpo - script which generates a HTML table of music tags
226
227=head1 SYNOPSIS
228
229 use App::MusicExpo;
230 App::MusicExpo->run;
231
232=head1 DESCRIPTION
233
234App::MusicExpo creates a HTML table from a list of songs.
235
236The default template looks like:
237
238 | Title | Artist | Album | Genre | Track | Year | Type |
239 |---------+---------+-----------------+---------+-------+------+------|
240 | Cellule | Silence | L'autre endroit | Electro | 01/09 | 2005 | FLAC |
241
d5aa2e89
MG
242where the type is a download link. If you have multiple files with the same
243basename (such as C<cellule.flac> and C<cellule.ogg>), they will be treated
244as two versions of the same file, so a row will be created with two download
245links, one for each format.
21cd9240
MG
246
247=head1 OPTIONS
248
249=over
250
251=item B<--template> I<template>
252
75c564d8 253Path to the HTML::Template::Compiled template used for generating the music table. If '' (empty), uses the default format. Is empty by default.
21cd9240
MG
254
255=item B<--prefix> I<prefix>
256
257Prefix for download links. Defaults to '/music/'.
258
259=item B<--cache> I<filename>
260
fb015c82 261Path to the cache file. Created if it does not exist. If '' (empty), disables caching. Is empty by default.
21cd9240
MG
262
263=back
264
265=head1 AUTHOR
266
267Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
268
269=head1 COPYRIGHT AND LICENSE
270
60376c0c 271Copyright (C) 2013-2016 by Marius Gavrilescu
21cd9240
MG
272
273This library is free software; you can redistribute it and/or modify
274it under the same terms as Perl itself, either Perl version 5.14.2 or,
275at your option, any later version of Perl 5 you may have available.
This page took 0.034212 seconds and 4 git commands to generate.