]>
Commit | Line | Data |
---|---|---|
c2a060a4 | 1 | package App::MusicExpo 0.002; |
21cd9240 | 2 | use v5.14; |
75c564d8 | 3 | use strict; |
21cd9240 MG |
4 | use warnings; |
5 | ||
6 | use Audio::FLAC::Header qw//; | |
21cd9240 MG |
7 | use HTML::Template::Compiled qw//; |
8 | use Memoize qw/memoize/; | |
9 | use MP3::Tag qw//; | |
21cd9240 MG |
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 | ||
75c564d8 MG |
19 | my $default_template; |
20 | ||
21cd9240 | 21 | our $prefix='/music/'; |
fb015c82 | 22 | our $cache=''; |
75c564d8 | 23 | our $template=''; |
21cd9240 MG |
24 | |
25 | GetOptions ( | |
26 | "template=s" => \$template, | |
27 | "prefix=s" => \$prefix, | |
28 | "cache=s" => \$cache, | |
21cd9240 MG |
29 | ); |
30 | ||
31 | ||
32 | sub fix{ | |
33 | utf8::decode($_[0]); | |
d718c66c | 34 | $_[0] |
21cd9240 MG |
35 | } |
36 | ||
37 | sub 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 | ||
55 | sub 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 | ||
73 | sub normalizer{ | |
74 | "$_[0]|".(stat $_[0])[9] | |
75 | } | |
76 | ||
77 | sub 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> |
108 | HTML | |
109 | ||
21cd9240 MG |
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 | ||
75c564d8 | 141 | Path 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 | ||
145 | Prefix for download links. Defaults to '/music/'. | |
146 | ||
147 | =item B<--cache> I<filename> | |
148 | ||
fb015c82 | 149 | Path 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 | ||
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. |