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