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