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