Bump MP3::Tag dependency
[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 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]);
21cd9240
MG
34}
35
36sub flacinfo{
37 my $file=$_[0];
38 my $flac=Audio::FLAC::Header->new($file);
e0fa3070 39 $file = $prefix . scalar fileparse $file;
21cd9240
MG
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
54sub mp3info{
55 my $file=$_[0];
56 my $mp3=MP3::Tag->new($file);
e0fa3070 57 $file = $prefix . scalar fileparse $file;
21cd9240
MG
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
72sub normalizer{
73 "$_[0]|".(stat $_[0])[9]
74}
75
76sub run {
fb015c82
MG
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 '';
21cd9240
MG
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
e0fa3070
MG
87 my $ht=HTML::Template::Compiled->new(
88 default_escape => 'HTML',
89 $template eq '' ? (scalarref => \$default_template) : (filename => $template),
90 );
21cd9240
MG
91 $ht->param(files=>[sort { $a->{title} cmp $b->{title} } @files]);
92 print $ht->output;
93}
94
75c564d8
MG
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>
e0fa3070 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>
75c564d8
MG
106</table>
107HTML
108
21cd9240
MG
1091;
110
111__END__
112
113=head1 NAME
114
115App::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
124App::MusicExpo creates a HTML table from a list of songs.
125
126The 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
132where the title is a download link.
133
134=head1 OPTIONS
135
136=over
137
138=item B<--template> I<template>
139
75c564d8 140Path to the HTML::Template::Compiled template used for generating the music table. If '' (empty), uses the default format. Is empty by default.
21cd9240
MG
141
142=item B<--prefix> I<prefix>
143
144Prefix for download links. Defaults to '/music/'.
145
146=item B<--cache> I<filename>
147
fb015c82 148Path to the cache file. Created if it does not exist. If '' (empty), disables caching. Is empty by default.
21cd9240
MG
149
150=back
151
152=head1 AUTHOR
153
154Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
155
156=head1 COPYRIGHT AND LICENSE
157
158Copyright (C) 2013 by Marius Gavrilescu
159
160This library is free software; you can redistribute it and/or modify
161it under the same terms as Perl itself, either Perl version 5.14.2 or,
162at your option, any later version of Perl 5 you may have available.
This page took 0.017746 seconds and 4 git commands to generate.