]>
Commit | Line | Data |
---|---|---|
7eb41a1b | 1 | package App::MusicExpo; |
21cd9240 | 2 | use v5.14; |
75c564d8 | 3 | use strict; |
21cd9240 MG |
4 | use warnings; |
5 | ||
7eb41a1b MG |
6 | our $VERSION = 0.003; |
7 | ||
21cd9240 | 8 | use Audio::FLAC::Header qw//; |
21cd9240 MG |
9 | use HTML::Template::Compiled qw//; |
10 | use Memoize qw/memoize/; | |
11 | use MP3::Tag qw//; | |
513a3718 | 12 | use Ogg::Vorbis::Header::PurePerl; |
21cd9240 MG |
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 | ||
75c564d8 MG |
22 | my $default_template; |
23 | ||
21cd9240 | 24 | our $prefix='/music/'; |
fb015c82 | 25 | our $cache=''; |
75c564d8 | 26 | our $template=''; |
21cd9240 MG |
27 | |
28 | GetOptions ( | |
29 | "template=s" => \$template, | |
30 | "prefix=s" => \$prefix, | |
31 | "cache=s" => \$cache, | |
21cd9240 MG |
32 | ); |
33 | ||
34 | ||
35 | sub fix{ | |
b2f791f6 MG |
36 | my $copy = $_[0]; |
37 | utf8::decode($copy); | |
38 | $copy | |
21cd9240 MG |
39 | } |
40 | ||
41 | sub flacinfo{ | |
42 | my $file=$_[0]; | |
43 | my $flac=Audio::FLAC::Header->new($file); | |
7ad441f0 | 44 | $file = scalar fileparse $file; |
21cd9240 MG |
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') // '?'), | |
7ad441f0 | 55 | file => $file, |
21cd9240 MG |
56 | } |
57 | } | |
58 | ||
59 | sub mp3info{ | |
60 | my $file=$_[0]; | |
61 | my $mp3=MP3::Tag->new($file); | |
7ad441f0 | 62 | $file = scalar fileparse $file; |
21cd9240 MG |
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) || '?', | |
7ad441f0 | 73 | file => $file, |
21cd9240 MG |
74 | } |
75 | } | |
76 | ||
513a3718 MG |
77 | sub vorbisinfo{ |
78 | my $file=$_[0]; | |
79 | my $ogg=Ogg::Vorbis::Header::PurePerl->new($file); | |
7ad441f0 | 80 | $file = scalar fileparse $file; |
513a3718 MG |
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')) || '?', | |
7ad441f0 | 91 | file => $file, |
513a3718 MG |
92 | } |
93 | } | |
94 | ||
21cd9240 MG |
95 | sub normalizer{ |
96 | "$_[0]|".(stat $_[0])[9] | |
97 | } | |
98 | ||
99 | sub run { | |
fb015c82 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 ''; | |
513a3718 | 103 | memoize 'vorbisinfo' , NORMALIZER => \&normalizer, LIST_CACHE => 'MERGE', SCALAR_CACHE => [HASH => \%cache] unless $cache eq ''; |
21cd9240 | 104 | |
5be1651c | 105 | my %files; |
21cd9240 | 106 | for my $file (@ARGV) { |
5be1651c MG |
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; | |
21cd9240 MG |
115 | } |
116 | ||
e0fa3070 MG |
117 | my $ht=HTML::Template::Compiled->new( |
118 | default_escape => 'HTML', | |
0a4b389c | 119 | global_vars => 2, |
e0fa3070 MG |
120 | $template eq '' ? (scalarref => \$default_template) : (filename => $template), |
121 | ); | |
5be1651c MG |
122 | |
123 | my @files; | |
124 | for (values %files) { | |
125 | my @versions = @$_; | |
126 | my %entry = (%{$versions[0]}, formats => []); | |
7ad441f0 | 127 | push $entry{formats}, {format => $_->{format}, file => $_->{file}} for @versions; |
5be1651c MG |
128 | push @files, \%entry |
129 | } | |
130 | ||
7ad441f0 | 131 | $ht->param(files=>[sort { $a->{title} cmp $b->{title} } @files], prefix => $prefix); |
21cd9240 MG |
132 | print $ht->output; |
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> |
147 | HTML | |
148 | ||
21cd9240 MG |
149 | 1; |
150 | ||
151 | __END__ | |
152 | ||
153 | =head1 NAME | |
154 | ||
155 | App::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 | ||
164 | App::MusicExpo creates a HTML table from a list of songs. | |
165 | ||
166 | The 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 |
172 | where the type is a download link. If you have multiple files with the same |
173 | basename (such as C<cellule.flac> and C<cellule.ogg>), they will be treated | |
174 | as two versions of the same file, so a row will be created with two download | |
175 | links, one for each format. | |
21cd9240 MG |
176 | |
177 | =head1 OPTIONS | |
178 | ||
179 | =over | |
180 | ||
181 | =item B<--template> I<template> | |
182 | ||
75c564d8 | 183 | Path 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 | ||
187 | Prefix for download links. Defaults to '/music/'. | |
188 | ||
189 | =item B<--cache> I<filename> | |
190 | ||
fb015c82 | 191 | Path 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 | ||
197 | Marius Gavrilescu, E<lt>marius@ieval.roE<gt> | |
198 | ||
199 | =head1 COPYRIGHT AND LICENSE | |
200 | ||
201 | Copyright (C) 2013 by Marius Gavrilescu | |
202 | ||
203 | This library is free software; you can redistribute it and/or modify | |
204 | it under the same terms as Perl itself, either Perl version 5.14.2 or, | |
205 | at your option, any later version of Perl 5 you may have available. |