Use the escaping in HTML::Template::Compiled
[app-musicexpo.git] / lib / App / MusicExpo.pm
CommitLineData
21cd9240
MG
1package App::MusicExpo 0.001;
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//;
21cd9240
MG
10
11use DB_File qw//;
12use File::Basename qw/fileparse/;
13use Fcntl qw/O_RDWR O_CREAT/;
14use Getopt::Long;
15use Storable qw/thaw freeze/;
16
17##################################################
18
75c564d8
MG
19my $default_template;
20
21cd9240
MG
21our $prefix='/music/';
22our $cache='cache.db';
23our $caching=1;
75c564d8 24our $template='';
21cd9240
MG
25
26GetOptions (
27 "template=s" => \$template,
28 "prefix=s" => \$prefix,
29 "cache=s" => \$cache,
30 "caching!" => \$caching,
31);
32
33
34sub fix{
35 utf8::decode($_[0]);
21cd9240
MG
36}
37
38sub flacinfo{
39 my $file=$_[0];
40 my $flac=Audio::FLAC::Header->new($file);
e0fa3070 41 $file = $prefix . 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') // '?'),
52 path => $file,
53 }
54}
55
56sub mp3info{
57 my $file=$_[0];
58 my $mp3=MP3::Tag->new($file);
e0fa3070 59 $file = $prefix . 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) || '?',
70 path => $file,
71 }
72}
73
74sub normalizer{
75 "$_[0]|".(stat $_[0])[9]
76}
77
78sub run {
79 tie my %cache, 'DB_File', $cache, O_RDWR|O_CREAT, 0644 if $caching;
80 memoize 'flacinfo', NORMALIZER => \&normalizer, LIST_CACHE => 'MERGE', SCALAR_CACHE => [HASH => \%cache] if $caching;
81 memoize 'mp3info' , NORMALIZER => \&normalizer, LIST_CACHE => 'MERGE', SCALAR_CACHE => [HASH => \%cache] if $caching;
82
83 my @files;
84 for my $file (@ARGV) {
85 push @files, thaw flacinfo $file if $file =~ /.flac$/i;
86 push @files, thaw mp3info $file if $file =~ /.mp3$/i;
87 }
88
e0fa3070
MG
89 my $ht=HTML::Template::Compiled->new(
90 default_escape => 'HTML',
91 $template eq '' ? (scalarref => \$default_template) : (filename => $template),
92 );
21cd9240
MG
93 $ht->param(files=>[sort { $a->{title} cmp $b->{title} } @files]);
94 print $ht->output;
95}
96
75c564d8
MG
97$default_template = <<'HTML';
98<!DOCTYPE html>
99<title>Music</title>
100<meta charset="utf-8">
101<link rel="stylesheet" href="/music.css">
102
103<table border>
104<thead>
105<tr><th>Title<th>Artist<th>Album<th>Genre<th>Track<th>Year<th>Type
106<tbody><tmpl_loop files>
e0fa3070 107<tr><td><a href="<tmpl_var ESCAPE=URL path>"><tmpl_var title></a><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_var format></tmpl_loop>
75c564d8
MG
108</table>
109HTML
110
21cd9240
MG
1111;
112
113__END__
114
115=head1 NAME
116
117App::MusicExpo - script which generates a HTML table of music tags
118
119=head1 SYNOPSIS
120
121 use App::MusicExpo;
122 App::MusicExpo->run;
123
124=head1 DESCRIPTION
125
126App::MusicExpo creates a HTML table from a list of songs.
127
128The default template looks like:
129
130 | Title | Artist | Album | Genre | Track | Year | Type |
131 |---------+---------+-----------------+---------+-------+------+------|
132 | Cellule | Silence | L'autre endroit | Electro | 01/09 | 2005 | FLAC |
133
134where the title is a download link.
135
136=head1 OPTIONS
137
138=over
139
140=item B<--template> I<template>
141
75c564d8 142Path to the HTML::Template::Compiled template used for generating the music table. If '' (empty), uses the default format. Is empty by default.
21cd9240
MG
143
144=item B<--prefix> I<prefix>
145
146Prefix for download links. Defaults to '/music/'.
147
148=item B<--cache> I<filename>
149
150Path to the cache file. Created if it does not exist. Defaults to 'cache.db'
151
152=item B<--caching>, B<--no-caching>
153
154Enables or disables caching. Defaults to B<--caching>
155
156=back
157
158=head1 AUTHOR
159
160Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
161
162=head1 COPYRIGHT AND LICENSE
163
164Copyright (C) 2013 by Marius Gavrilescu
165
166This library is free software; you can redistribute it and/or modify
167it under the same terms as Perl itself, either Perl version 5.14.2 or,
168at your option, any later version of Perl 5 you may have available.
This page took 0.018615 seconds and 4 git commands to generate.