]> iEval git - app-musicexpo.git/blame - lib/App/MusicExpo.pm
Use \. in extension regexes
[app-musicexpo.git] / lib / App / MusicExpo.pm
CommitLineData
7eb41a1b 1package App::MusicExpo;
21cd9240 2use v5.14;
75c564d8 3use strict;
21cd9240
MG
4use warnings;
5
b67c2762 6our $VERSION = '0.003003';
7eb41a1b 7
21cd9240 8use Audio::FLAC::Header qw//;
21cd9240
MG
9use HTML::Template::Compiled qw//;
10use Memoize qw/memoize/;
11use MP3::Tag qw//;
513a3718 12use Ogg::Vorbis::Header::PurePerl;
21cd9240
MG
13
14use DB_File qw//;
15use File::Basename qw/fileparse/;
16use Fcntl qw/O_RDWR O_CREAT/;
17use Getopt::Long;
dfe0cbe2 18use JSON::MaybeXS;
21cd9240
MG
19use Storable qw/thaw freeze/;
20
21##################################################
22
75c564d8
MG
23my $default_template;
24
21cd9240 25our $prefix='/music/';
fb015c82 26our $cache='';
75c564d8 27our $template='';
21cd9240
MG
28
29GetOptions (
598a92c5
MG
30 "template:s" => \$template,
31 "prefix:s" => \$prefix,
32 "cache:s" => \$cache,
21cd9240
MG
33);
34
35
36sub fix{
6a1f7df9
MG
37 my $copy = $_[0];
38 utf8::decode($copy);
39 $copy
21cd9240
MG
40}
41
42sub flacinfo{
6a1f7df9
MG
43 my $file=$_[0];
44 my $flac=Audio::FLAC::Header->new($file);
45 $file = scalar fileparse $file;
46
47 freeze +{
48 format => 'FLAC',
49 title => fix ($flac->tags('TITLE') // '?'),
50 artist => fix ($flac->tags('ARTIST') // '?'),
51 year => fix ($flac->tags('DATE') // '?'),
52 album => fix ($flac->tags('ALBUM') // '?'),
53 tracknumber => fix ($flac->tags('TRACKNUMBER') // '?'),
54 tracktotal => fix ($flac->tags('TRACKTOTAL') // '?'),
55 genre => fix ($flac->tags('GENRE') // '?'),
56 file => $file,
57 }
21cd9240
MG
58}
59
60sub mp3info{
6a1f7df9
MG
61 my $file=$_[0];
62 my $mp3=MP3::Tag->new($file);
63 $file = scalar fileparse $file;
64
65 freeze +{
66 format => 'MP3',
67 title => fix ($mp3->title || '?'),
68 artist => fix ($mp3->artist || '?'),
69 year => fix ($mp3->year || '?'),
70 album => fix ($mp3->album || '?'),
71 tracknumber => fix ($mp3->track1 || '?'),
72 tracktotal => fix ($mp3->track2 || '?'),
73 genre => fix ($mp3->genre) || '?',
74 file => $file,
75 }
21cd9240
MG
76}
77
513a3718 78sub vorbisinfo{
6a1f7df9
MG
79 my $file=$_[0];
80 my $ogg=Ogg::Vorbis::Header::PurePerl->new($file);
81 $file = scalar fileparse $file;
82
83 freeze +{
84 format => 'Vorbis',
85 title => fix($ogg->comment('TITLE') || '?'),
86 artist => fix ($ogg->comment('artist') || '?'),
87 year => fix ($ogg->comment('DATE') || '?'),
88 album => fix ($ogg->comment('ALBUM') || '?'),
89 tracknumber => fix ($ogg->comment('TRACKNUMBER') || '?'),
90 tracktotal => fix ($ogg->comment('TRACKTOTAL') || '?'),
91 genre => fix ($ogg->comment('GENRE')) || '?',
92 file => $file,
93 }
513a3718
MG
94}
95
21cd9240 96sub normalizer{
6a1f7df9 97 "$_[0]|".(stat $_[0])[9]
21cd9240
MG
98}
99
100sub run {
cc5d06b5
MG
101 if ($cache) {
102 tie my %cache, 'DB_File', $cache, O_RDWR|O_CREAT, 0644;
103 memoize $_, NORMALIZER => \&normalizer, LIST_CACHE => 'MERGE', SCALAR_CACHE => [HASH => \%cache] for qw/flacinfo mp3info vorbisinfo/;
104 }
6a1f7df9
MG
105
106 my %files;
107 for my $file (@ARGV) {
108 my $info;
1549062d
MG
109 $info = thaw flacinfo $file if $file =~ /\.flac$/i;
110 $info = thaw mp3info $file if $file =~ /\.mp3$/i;
111 $info = thaw vorbisinfo $file if $file =~ /\.og(?:g|a)$/i;
6a1f7df9
MG
112 next unless defined $info;
113 my $basename = fileparse $file, '.flac', '.mp3', '.ogg', '.oga';
114 $files{$basename} //= [];
115 push $files{$basename}, $info;
116 }
117
118 my $ht=HTML::Template::Compiled->new(
119 default_escape => 'HTML',
120 global_vars => 2,
121 $template eq '' ? (scalarref => \$default_template) : (filename => $template),
122 );
123
124 my @files;
125 for (values %files) {
126 my @versions = @$_;
127 my %entry = (%{$versions[0]}, formats => []);
128 push $entry{formats}, {format => $_->{format}, file => $_->{file}} for @versions;
129 push @files, \%entry
130 }
131
dfe0cbe2 132 my $json = JSON::MaybeXS->new(canonical => 1)->encode({files => \@files, prefix => $prefix});
95267d4f
MG
133 $json =~ s/</&lt;/g;
134 $json =~ s/>/&gt;/g;
135 $ht->param(files=>[sort { $a->{title} cmp $b->{title} } @files], prefix => $prefix, json => $json);
6a1f7df9 136 print $ht->output;
21cd9240
MG
137}
138
75c564d8
MG
139$default_template = <<'HTML';
140<!DOCTYPE html>
141<title>Music</title>
142<meta charset="utf-8">
143<link rel="stylesheet" href="/music.css">
144
145<table border>
146<thead>
147<tr><th>Title<th>Artist<th>Album<th>Genre<th>Track<th>Year<th>Type
148<tbody><tmpl_loop files>
95267d4f 149<tr><td><tmpl_var title><td><tmpl_var artist><td><tmpl_var album><td><tmpl_var genre><td><tmpl_var tracknumber>/<tmpl_var tracktotal><td><tmpl_var year><td><tmpl_loop formats><a href="<tmpl_var ...prefix><tmpl_var ESCAPE=URL file>"><tmpl_var format></a> </tmpl_loop></tmpl_loop>
75c564d8 150</table>
95267d4f
MG
151
152<pre id="json" style="display: none"><tmpl_var ESCAPE=0 json></pre>
75c564d8
MG
153HTML
154
21cd9240
MG
1551;
156
157__END__
158
bb86a416
MG
159=encoding utf-8
160
21cd9240
MG
161=head1 NAME
162
163App::MusicExpo - script which generates a HTML table of music tags
164
165=head1 SYNOPSIS
166
167 use App::MusicExpo;
168 App::MusicExpo->run;
169
170=head1 DESCRIPTION
171
172App::MusicExpo creates a HTML table from a list of songs.
173
174The default template looks like:
175
176 | Title | Artist | Album | Genre | Track | Year | Type |
177 |---------+---------+-----------------+---------+-------+------+------|
178 | Cellule | Silence | L'autre endroit | Electro | 01/09 | 2005 | FLAC |
179
d5aa2e89
MG
180where the type is a download link. If you have multiple files with the same
181basename (such as C<cellule.flac> and C<cellule.ogg>), they will be treated
182as two versions of the same file, so a row will be created with two download
183links, one for each format.
21cd9240
MG
184
185=head1 OPTIONS
186
187=over
188
189=item B<--template> I<template>
190
75c564d8 191Path to the HTML::Template::Compiled template used for generating the music table. If '' (empty), uses the default format. Is empty by default.
21cd9240
MG
192
193=item B<--prefix> I<prefix>
194
195Prefix for download links. Defaults to '/music/'.
196
197=item B<--cache> I<filename>
198
fb015c82 199Path to the cache file. Created if it does not exist. If '' (empty), disables caching. Is empty by default.
21cd9240
MG
200
201=back
202
203=head1 AUTHOR
204
205Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
206
207=head1 COPYRIGHT AND LICENSE
208
e83823ae 209Copyright (C) 2013-2014 by Marius Gavrilescu
21cd9240
MG
210
211This library is free software; you can redistribute it and/or modify
212it under the same terms as Perl itself, either Perl version 5.14.2 or,
213at your option, any later version of Perl 5 you may have available.
This page took 0.060194 seconds and 4 git commands to generate.