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