| 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 | use Ogg::Vorbis::Header::PurePerl; |
| 11 | |
| 12 | use DB_File qw//; |
| 13 | use File::Basename qw/fileparse/; |
| 14 | use Fcntl qw/O_RDWR O_CREAT/; |
| 15 | use Getopt::Long; |
| 16 | use Storable qw/thaw freeze/; |
| 17 | |
| 18 | ################################################## |
| 19 | |
| 20 | my $default_template; |
| 21 | |
| 22 | our $prefix='/music/'; |
| 23 | our $cache=''; |
| 24 | our $template=''; |
| 25 | |
| 26 | GetOptions ( |
| 27 | "template=s" => \$template, |
| 28 | "prefix=s" => \$prefix, |
| 29 | "cache=s" => \$cache, |
| 30 | ); |
| 31 | |
| 32 | |
| 33 | sub fix{ |
| 34 | utf8::decode($_[0]); |
| 35 | $_[0] |
| 36 | } |
| 37 | |
| 38 | sub flacinfo{ |
| 39 | my $file=$_[0]; |
| 40 | my $flac=Audio::FLAC::Header->new($file); |
| 41 | $file = $prefix . 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 | |
| 56 | sub mp3info{ |
| 57 | my $file=$_[0]; |
| 58 | my $mp3=MP3::Tag->new($file); |
| 59 | $file = $prefix . 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 | |
| 74 | sub vorbisinfo{ |
| 75 | my $file=$_[0]; |
| 76 | my $ogg=Ogg::Vorbis::Header::PurePerl->new($file); |
| 77 | $file = $prefix . scalar fileparse $file; |
| 78 | |
| 79 | freeze +{ |
| 80 | format => 'Vorbis', |
| 81 | title => fix($ogg->comment('TITLE') || '?'), |
| 82 | artist => fix ($ogg->comment('artist') || '?'), |
| 83 | year => fix ($ogg->comment('DATE') || '?'), |
| 84 | album => fix ($ogg->comment('ALBUM') || '?'), |
| 85 | tracknumber => fix ($ogg->comment('TRACKNUMBER') || '?'), |
| 86 | tracktotal => fix ($ogg->comment('TRACKTOTAL') || '?'), |
| 87 | genre => fix ($ogg->comment('GENRE')) || '?', |
| 88 | path => $file, |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | sub normalizer{ |
| 93 | "$_[0]|".(stat $_[0])[9] |
| 94 | } |
| 95 | |
| 96 | sub run { |
| 97 | tie my %cache, 'DB_File', $cache, O_RDWR|O_CREAT, 0644 unless $cache eq ''; |
| 98 | memoize 'flacinfo', NORMALIZER => \&normalizer, LIST_CACHE => 'MERGE', SCALAR_CACHE => [HASH => \%cache] unless $cache eq ''; |
| 99 | memoize 'mp3info' , NORMALIZER => \&normalizer, LIST_CACHE => 'MERGE', SCALAR_CACHE => [HASH => \%cache] unless $cache eq ''; |
| 100 | memoize 'vorbisinfo' , NORMALIZER => \&normalizer, LIST_CACHE => 'MERGE', SCALAR_CACHE => [HASH => \%cache] unless $cache eq ''; |
| 101 | |
| 102 | my %files; |
| 103 | for my $file (@ARGV) { |
| 104 | my $info; |
| 105 | $info = thaw flacinfo $file if $file =~ /.flac$/i; |
| 106 | $info = thaw mp3info $file if $file =~ /.mp3$/i; |
| 107 | $info = thaw vorbisinfo $file if $file =~ /.og(?:g|a)$/i; |
| 108 | next unless defined $info; |
| 109 | my $basename = fileparse $file, '.flac', '.mp3', '.ogg', '.oga'; |
| 110 | $files{$basename} //= []; |
| 111 | push $files{$basename}, $info; |
| 112 | } |
| 113 | |
| 114 | my $ht=HTML::Template::Compiled->new( |
| 115 | default_escape => 'HTML', |
| 116 | $template eq '' ? (scalarref => \$default_template) : (filename => $template), |
| 117 | ); |
| 118 | |
| 119 | my @files; |
| 120 | for (values %files) { |
| 121 | my @versions = @$_; |
| 122 | my %entry = (%{$versions[0]}, formats => []); |
| 123 | push $entry{formats}, {format => $_->{format}, path => $_->{path}} for @versions; |
| 124 | push @files, \%entry |
| 125 | } |
| 126 | |
| 127 | $ht->param(files=>[sort { $a->{title} cmp $b->{title} } @files]); |
| 128 | print $ht->output; |
| 129 | } |
| 130 | |
| 131 | $default_template = <<'HTML'; |
| 132 | <!DOCTYPE html> |
| 133 | <title>Music</title> |
| 134 | <meta charset="utf-8"> |
| 135 | <link rel="stylesheet" href="/music.css"> |
| 136 | |
| 137 | <table border> |
| 138 | <thead> |
| 139 | <tr><th>Title<th>Artist<th>Album<th>Genre<th>Track<th>Year<th>Type |
| 140 | <tbody><tmpl_loop files> |
| 141 | <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> |
| 142 | </table> |
| 143 | HTML |
| 144 | |
| 145 | 1; |
| 146 | |
| 147 | __END__ |
| 148 | |
| 149 | =head1 NAME |
| 150 | |
| 151 | App::MusicExpo - script which generates a HTML table of music tags |
| 152 | |
| 153 | =head1 SYNOPSIS |
| 154 | |
| 155 | use App::MusicExpo; |
| 156 | App::MusicExpo->run; |
| 157 | |
| 158 | =head1 DESCRIPTION |
| 159 | |
| 160 | App::MusicExpo creates a HTML table from a list of songs. |
| 161 | |
| 162 | The default template looks like: |
| 163 | |
| 164 | | Title | Artist | Album | Genre | Track | Year | Type | |
| 165 | |---------+---------+-----------------+---------+-------+------+------| |
| 166 | | Cellule | Silence | L'autre endroit | Electro | 01/09 | 2005 | FLAC | |
| 167 | |
| 168 | where the title is a download link. |
| 169 | |
| 170 | =head1 OPTIONS |
| 171 | |
| 172 | =over |
| 173 | |
| 174 | =item B<--template> I<template> |
| 175 | |
| 176 | Path to the HTML::Template::Compiled template used for generating the music table. If '' (empty), uses the default format. Is empty by default. |
| 177 | |
| 178 | =item B<--prefix> I<prefix> |
| 179 | |
| 180 | Prefix for download links. Defaults to '/music/'. |
| 181 | |
| 182 | =item B<--cache> I<filename> |
| 183 | |
| 184 | Path to the cache file. Created if it does not exist. If '' (empty), disables caching. Is empty by default. |
| 185 | |
| 186 | =back |
| 187 | |
| 188 | =head1 AUTHOR |
| 189 | |
| 190 | Marius Gavrilescu, E<lt>marius@ieval.roE<gt> |
| 191 | |
| 192 | =head1 COPYRIGHT AND LICENSE |
| 193 | |
| 194 | Copyright (C) 2013 by Marius Gavrilescu |
| 195 | |
| 196 | This library is free software; you can redistribute it and/or modify |
| 197 | it under the same terms as Perl itself, either Perl version 5.14.2 or, |
| 198 | at your option, any later version of Perl 5 you may have available. |