#!/usr/bin/perl -w use strict; use File::Find; use File::Basename; use HTML::Entities qw(encode_entities); use URI::URL; my $index_file = $ARGV[0] || "index.html"; print STDERR "index file = \"$index_file\"\n"; use constant BYTES_PER_MB => 2**20; my $archive_dir = ".archive"; my $mp3info_exec = "/usr/local/bin/mp3info"; my @files; sub wanted { if (-f $_ && /\.mp3$/i) { push @files, $File::Find::name; } } find(\&wanted, '.'); @files = sort @files; my $key = 0; # since we're splitting on /, "/" can never appear in a real name. so # we can use these for private keys. my $root = { "/key" => $key, "/html" => "/", "/dir" => 1}; sub get_info { my $filename = shift; my $pid = open KID, "-|"; die "couldn't fork: $!" unless defined $pid; if ($pid == 0) { # child exec($mp3info_exec, $filename) or die "couldn't exec $mp3info_exec: $!"; } # parent my $retval = ""; while () { $retval .= $_; } close KID; waitpid $pid, 0; return $retval; } foreach my $name (@files) { $name =~ s!^\./!!; my $url = url($name); my $base = basename $name; my $html_name = encode_entities($base); my $megabytes = (-s $name) / BYTES_PER_MB; my $info = get_info($name); chomp $info; my ($kbps, $mode, $min, $sec) = ($info =~ m!(\d+) kbit/s (.*) \((\d+)m (\d+)s\)$!); # print STDERR "info = \"$info\", kbps=\"$kbps\", mode=\"$mode\", min=\"$min\", sec=\"$sec\"\n"; my $size = sprintf ("(%.2f MB = %d:%02d @ %d kbps [%s])", $megabytes, $min, $sec, $kbps, $mode); my $r = $root; for my $dir (split /\//, dirname $name) { unless (exists $r->{$dir}) { $r->{$dir} = { "/key" => ++$key, "/dir" => 1, "/html" => encode_entities($dir) }; } $r = $r->{$dir}; } $r->{$base} = { "/key" => ++$key, "/file" => 1, url => $url, html => $html_name, size => $size }; } # while (my ($k, $v) = each %$root) { # print "$k => $v\n"; # } open F, "> $index_file" or die "opening $index_file: $!"; print F < Tkil MP3s

Tkil MP3s

EOT if (-d $archive_dir) { print F "

Archives

\n", "\n"; } print F "

MP3s currently on disk

\n"; sub print_hash { my $dir = shift; my @sorted_hrefs = map { $dir->{$_} } sort { $dir->{$a}->{"/key"} <=> $dir->{$b}->{"/key"} } grep { ! m@^/.@ } keys %$dir; foreach my $href (@sorted_hrefs) { if (exists $href->{"/file"}) { my ($url, $html, $size) = @{$href}{qw(url html size)}; print F "
  • $html\n", " $size\n"; } elsif (exists $href->{"/dir"}) { my $html = $href->{"/html"}; print F "
  • $html\n
      \n"; print_hash($href); print F "
    \n" } } } print F "
      \n"; print_hash($root); print F "
    \n"; print F < EOT close F or die "closing 00INDEX.txt: $!"; exit 0;