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