/omake/ut-web
#!/usr/bin/perl
use Getopt::Long;
use File::Basename;
use File::Copy;
use IPC::Open2;
use strict;
my $resources = '/home/yomiko/chip/programs/utunes/omake/webpack';
my $name = 'Web Playlist';
my $convopts = '';
GetOptions(
"name=s" => \$name,
"convopts=s" => \$convopts,
);
sub url_escape {
my $s = shift;
$s =~ s/([^a-zA-Z0-9_\-._])/sprintf("%%%02x", ord($1))/eg;
return $s;
}
sub udb_snarf {
my %info;
my $pid = open2 \*UDB_OUT, \*UDB_IN, 'udb info'
or die "Could not execute udb";
print UDB_IN $_[0], "\n";
close UDB_IN;
while (<UDB_OUT>) {
$info{$1} = $2 if /^\t(\w+)\s+(.*)$/;
}
close UDB_OUT;
waitpid $pid, 0;
return \%info;
}
my $dir = shift;
# Initialize a dir
mkdir "$dir";
mkdir "$dir/songs";
for (<$resources/*>) {
copy $_, $dir;
}
chdir $dir;
open PLAYLIST, ">playlist.xml";
print PLAYLIST <<XML;
<?xml version="1.0" encoding="UTF-8"?>
<playlist version="1">
<title>$name</title>
<tracklist>
XML
open CONV, "| conv $convopts -o songs -s -i mp3";
while (<STDIN>) {
chomp;
unless (-f) {
warn "$_ does not exist";
next;
}
print CONV "$_\n";
my $name = basename $_;
(my $outname = $name) =~ s/\.[^.]+$/\.mp3/;
$outname = 'songs/' . url_escape($outname);
# snarf some info
my $info = udb_snarf($_);
$name = $info->{title} if exists $info->{title} and $info->{title};
print PLAYLIST <<XML;
<track>
<title>$name</title>
<location>$outname</location>
<annotation>$info->{artist}\n$info->{album}</annotation>
</track>
XML
}
close CONV;
print PLAYLIST <<'XML';
</tracklist>
</playlist>
XML
close PLAYLIST;
open INDEX, ">index.html";
print INDEX <<HTML;
<html>
<head>
<title>$name</title>
<style type="text/css">
body { margin: 0 }
</style>
</head>
<body>
<div id="container">YOU DON'T HAVE FLASH PLAYER, NIGGA</div>
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">
var w = 640;
var h = 480;
var plh = 460;
if (window.innerWidth) {
w = window.innerWidth;
h = window.innerHeight;
} else {
w = document.documentElement.offsetWidth;
h = document.documentElement.offsetHeight;
}
plh = h - 20;
var so = new SWFObject("player.swf", "ply", w.toString(), h.toString(), "9", "#FFFFFF");
so.addParam("Flashvars", "file=playlist.xml&height=0&playlist=bottom&repeat=list&playlistsize=" + plh);
so.write("container");
</script>
</body>
</html>
HTML
close INDEX;