Initial commit
[app-musicexpo.git] / lib / App / MusicExpo.pm
CommitLineData
21cd9240
MG
1package App::MusicExpo 0.001;
2use v5.14;
3use warnings;
4
5use Audio::FLAC::Header qw//;
6use HTML::Entities qw/encode_entities/;
7use HTML::Template::Compiled qw//;
8use Memoize qw/memoize/;
9use MP3::Tag qw//;
10use URI::Escape qw/uri_escape/;
11
12use DB_File qw//;
13use File::Basename qw/fileparse/;
14use Fcntl qw/O_RDWR O_CREAT/;
15use Getopt::Long;
16use Storable qw/thaw freeze/;
17
18##################################################
19
20our $prefix='/music/';
21our $cache='cache.db';
22our $caching=1;
23our $template='index.tmpl';
24
25GetOptions (
26 "template=s" => \$template,
27 "prefix=s" => \$prefix,
28 "cache=s" => \$cache,
29 "caching!" => \$caching,
30);
31
32
33sub fix{
34 utf8::decode($_[0]);
35 encode_entities($_[0])
36}
37
38sub flacinfo{
39 my $file=$_[0];
40 my $flac=Audio::FLAC::Header->new($file);
41 $file = $prefix . uri_escape scalar fileparse $file;
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);
59 $file = $prefix . uri_escape scalar fileparse $file;
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
89 my $ht=HTML::Template::Compiled->new(filename => $template);
90 $ht->param(files=>[sort { $a->{title} cmp $b->{title} } @files]);
91 print $ht->output;
92}
93
941;
95
96__END__
97
98=head1 NAME
99
100App::MusicExpo - script which generates a HTML table of music tags
101
102=head1 SYNOPSIS
103
104 use App::MusicExpo;
105 App::MusicExpo->run;
106
107=head1 DESCRIPTION
108
109App::MusicExpo creates a HTML table from a list of songs.
110
111The default template looks like:
112
113 | Title | Artist | Album | Genre | Track | Year | Type |
114 |---------+---------+-----------------+---------+-------+------+------|
115 | Cellule | Silence | L'autre endroit | Electro | 01/09 | 2005 | FLAC |
116
117where the title is a download link.
118
119=head1 OPTIONS
120
121=over
122
123=item B<--template> I<template>
124
125Path to the HTML::Template::Compiled template used for generating the music table. Defaults to 'index.tmpl'.
126
127=item B<--prefix> I<prefix>
128
129Prefix for download links. Defaults to '/music/'.
130
131=item B<--cache> I<filename>
132
133Path to the cache file. Created if it does not exist. Defaults to 'cache.db'
134
135=item B<--caching>, B<--no-caching>
136
137Enables or disables caching. Defaults to B<--caching>
138
139=back
140
141=head1 AUTHOR
142
143Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
144
145=head1 COPYRIGHT AND LICENSE
146
147Copyright (C) 2013 by Marius Gavrilescu
148
149This library is free software; you can redistribute it and/or modify
150it under the same terms as Perl itself, either Perl version 5.14.2 or,
151at your option, any later version of Perl 5 you may have available.
This page took 0.016242 seconds and 4 git commands to generate.