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