Make perlcritic tests pass
[app-musicexpo.git] / lib / App / MusicExpo.pm
... / ...
CommitLineData
1package App::MusicExpo;
2use 5.014000;
3use strict;
4use warnings;
5
6our $VERSION = '1.002001';
7
8use HTML::Template::Compiled qw//;
9use Memoize qw/memoize/;
10
11use Carp qw/carp/;
12use DB_File qw//;
13use Encode qw/encode/;
14use File::Basename qw/fileparse/;
15use Fcntl qw/O_RDWR O_CREAT/;
16use Getopt::Long;
17use Storable qw/thaw freeze/;
18use sort 'stable';
19
20##################################################
21
22my $default_template;
23
24our $prefix='/music/';
25our $cache='';
26our $template='';
27
28GetOptions (
29 'template:s' => \$template,
30 'prefix:s' => \$prefix,
31 'cache:s' => \$cache,
32);
33
34sub flacinfo{
35 my $file=$_[0];
36 my $flac=Audio::FLAC::Header->new($file);
37
38 freeze +{
39 format => 'FLAC',
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'),
47 file => scalar fileparse $file,
48 }
49}
50
51sub mp3info{
52 my $file=$_[0];
53 my %tag = map { encode 'UTF-8', $_ } %{MP3::Info::get_mp3tag $file};
54 my @trkn = split m#/#s, $tag{TRACKNUM} // '';
55
56 freeze +{
57 format => 'MP3',
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},
65 file => scalar fileparse $file,
66 }
67}
68
69sub vorbisinfo{
70 my $file=$_[0];
71 my $ogg=Ogg::Vorbis::Header::PurePerl->new($file);
72
73 freeze +{
74 format => 'Vorbis',
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'),
82 file => scalar fileparse $file,
83 }
84}
85
86sub mp4_format ($){ ## no critic (ProhibitSubroutinePrototypes)
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];
95 my %tag = map { ref() ? $_ : encode 'UTF-8', $_ } %{MP4::Info::get_mp4tag $file};
96 my %info = %{MP4::Info::get_mp4info $file};
97
98 freeze +{
99 format => mp4_format $info{ENCODING},
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},
107 file => scalar fileparse $file,
108 };
109}
110
111sub opusinfo {
112 my $file = $_[0];
113 my $of = Audio::Opusfile->new_from_file($file);
114 my $tags = $of->tags;
115
116 my %data = (
117 format => 'Opus',
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'),
125 file => scalar fileparse $file,
126 );
127
128 freeze \%data;
129}
130
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' ],
136 [ 'Audio::Opusfile', \&opusinfo, '.opus' ],
137);
138
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) {
149 carp 'No tags-reading module detected. Install one of the following modules: ' . join ', ', map { $_->[0] } @optional_modules;
150}
151
152sub normalizer{
153 "$_[0]|".(stat $_[0])[9]
154}
155
156sub make_fragment{ join '-', map { lc =~ y/a-z0-9/_/csr } @_ }
157
158sub extensions_handled { keys %info }
159
160sub run {
161 if ($cache) {
162 tie my %cache, 'DB_File', $cache, O_RDWR|O_CREAT, 0644; ## no critic (ProhibitTie)
163 $info{$_} = memoize $info{$_}, INSTALL => undef, NORMALIZER => \&normalizer, LIST_CACHE => 'FAULT', SCALAR_CACHE => [HASH => \%cache] for keys %info;
164 }
165
166 my %files;
167 for my $file (@ARGV) {
168 my ($basename, undef, $suffix) = fileparse $file, keys %info;
169 next unless $suffix;
170 $files{$basename} //= [];
171 push @{$files{$basename}}, thaw scalar $info{$suffix}->($file);
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;
181 for (sort keys %files) {
182 my @versions = @{$files{$_}};
183 my %entry = (formats => [], map { $_ => '?' } qw/title artist year album tracknumber tracktotal genre/);
184 for my $ver (@versions) {
185 push @{$entry{formats}}, {format => $ver->{format}, file => $ver->{file}};
186 for my $key (keys %$ver) {
187 $entry{$key} = $ver->{$key} if $ver->{$key} && $ver->{$key} ne '?';
188 }
189 }
190 delete $entry{$_} for qw/format file/;
191 $entry{fragment} = make_fragment @entry{qw/artist title/};
192 push @files, \%entry
193 }
194
195 @files = sort { $a->{title} cmp $b->{title} } @files;
196 $ht->param(files => \@files, prefix => $prefix);
197 print $ht->output; ## no critic (RequireCheckedSyscalls)
198}
199
200$default_template = <<'HTML';
201<!DOCTYPE html>
202<title>Music</title>
203<meta charset="utf-8">
204<link rel="stylesheet" href="musicexpo.css">
205<script async defer type="application/javascript" src="player.js"></script>
206
207<div id="player"></div>
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>
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>
214</table>
215HTML
216
2171;
218
219__END__
220
221=encoding utf-8
222
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
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.
246
247=head1 OPTIONS
248
249=over
250
251=item B<--template> I<template>
252
253Path to the HTML::Template::Compiled template used for generating the music table. If '' (empty), uses the default format. Is empty by default.
254
255=item B<--prefix> I<prefix>
256
257Prefix for download links. Defaults to '/music/'.
258
259=item B<--cache> I<filename>
260
261Path to the cache file. Created if it does not exist. If '' (empty), disables caching. Is empty by default.
262
263=back
264
265=head1 AUTHOR
266
267Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
268
269=head1 COPYRIGHT AND LICENSE
270
271Copyright (C) 2013-2016 by Marius Gavrilescu
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.010923 seconds and 4 git commands to generate.