#!/usr/bin/perl
#
# Author: Petter Reinholdtsen
# Date: 2006-11-01
# License: GNU General Public license
#
# Generate "videocast" RSS file based on the mpeg video files available from NUUG.

# Could use the adict data to extract title and description.

# http://www.apple.com/itunes/store/podcaststechspecs.html

use strict;
use warnings;

use Date::Parse;
use POSIX qw(strftime);

my $debug = 0;

my $format = $ARGV[0] || "mpeg";

# Where are the media files
my $videodir = "/home/ftp/pub/video/published";
my $videobaseurl = "http://www.nuug.no/pub/video/published";

# Where are the info pages
my $infodir = "/home/httpd/nuug/aktiviteter";
my $infobaseurl = "http://www.nuug.no/aktiviteter";

# Where is the ADiCT info
my $adict = "/home/httpd/nuug/adict/localevents";

my %mimetype =
 ("mpeg" => "video/mpeg",
  "mov"  => "video/mp4",
  "mp3"  => "audio/mpeg",
  "ogg"  => "application/ogg",
  "ogv"  => "video/ogg");

my @adict_entries = adict_load($adict) if (-f $adict);

adict_date_lookup("20061026", \@adict_entries);

chdir $videodir || die;
header();
for my $f (sort <*.$format>) {
    my ($base) = $f =~ m/^(.+).$format/;
    my $infourl = "";
    my $thumb = "";
    my $date;
    my $title = $base;
    my $desc = "";
    if ($base =~ m/^(\d{8})-(.*)$/) {
	my $adictentry = adict_date_lookup($1, $2, \@adict_entries);
	if ($adictentry) {
	    $title = $adictentry->{HEADLINE};
	    $title =~ s/^Medlemsmøte i NUUG Oslo: //;
	    $infourl = $adictentry->{EVENTURL};
	    $desc = $adictentry->{DESCRIPTION};
	    print STDERR "D: $desc\n" if $debug;
	} else {
	    $title =$2;
	}
        $date = format_date($1);
    }
    if ( !$infourl && -d "$infodir/$base" ) {
	$infourl = "$infobaseurl/$base/";
    }
    print STDERR "Check $videodir/$base-thumb.jpeg\n" if $debug;
    if ( -f "$videodir/$base-thumb.jpeg" ) {
        $thumb = "$videobaseurl/$base-thumb.jpeg";
    }
    my $size = (stat($f))[7];
    my $duration;
    if ( -f "$videodir/$base.mpeg" ) {
	$duration = `mpgtx -i $videodir/$base.mpeg |awk '/Estimated Duration:/ { print \$3}'`;
	chomp $duration;
	$duration =~ s/.\d+s//;
    }
    item($title, $date, $infourl, $desc,
         "$videobaseurl/$f", $mimetype{$format}, $size,
         $thumb, $duration);
}
footer();
exit 0;

sub header {
    print <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
  xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
  xmlns:atom="http://www.w3.org/2005/Atom"
>

  <channel>
    <title>NUUG foredrag ($format)</title>
    <link>http://www.nuug.no/dokumenter/nuug-tv.shtml</link>
    <atom:link href="http://www.nuug.no/pub/video/nuug-video-$format.rss" rel="self" type="application/rss+xml" />
    <description>Norwegian Unix User Groups opptak av
       presentasjoner og foredrag.</description>

    <copyright>(c) 2004-2009 NUUG</copyright>
    <language>nb</language>
    <itunes:author>NUUG</itunes:author>
    <itunes:subtitle>Collected recordings</itunes:subtitle>
    <itunes:summary>For Free.</itunes:summary>
    <itunes:owner>
        <itunes:name>NUUG</itunes:name>
        <itunes:email>sekretariat\@nuug.no</itunes:email>
    </itunes:owner>
    <category>Cartoons</category>
    <itunes:category text="Technology">
        <itunes:category text="Software How-To" />
    </itunes:category>

    <itunes:keywords>NUUG, Unix, linux, Free Software, Open Standard</itunes:keywords>
    <image>
        <title>NUUG foredrag ($format)</title>
        <url>http://www.nuug.no/images/nuuglogo_small.png</url>
        <link>http://www.nuug.no/dokumenter/nuug-tv.shtml</link>
    </image>
    <itunes:image href="http://www.nuug.no/images/nuuglogo_small.png"/>

EOF

}

sub footer {
    print <<EOF;
  </channel>
</rss>
EOF
}

sub item {
    my ($title, $date, $link, $desc,
        $enclurl, $encltype, $enclsize, $thumb, $duration) = @_;
    $enclurl = $link unless defined $enclurl;
    print <<EOF;
    <item>
      <title>$title</title>
EOF
    print "      <link>$link</link>\n" if $link;
    print "      <description>$desc</description>\n" if $desc;
    print "       <pubDate>$date</pubDate>\n" if $date;
    print <<EOF if $thumb;
      <image>
        <url>$thumb</url>
      </image>
      <itunes:image href="$thumb"/>
EOF
    print "      <itunes:duration>$duration</itunes:duration>\n" if ($duration);
    print <<EOF
      <enclosure url="$enclurl" type="$encltype" length="$enclsize" />
    </item>

EOF
}

sub format_date {
    my ($isodate) = @_;
    $isodate =~ s/^(\d{4})(\d{2})(\d{2})/$1-$2-$3/;
    my $time = str2time($isodate);

    return strftime("%a, %d %b %Y %H:%M:%S %z", gmtime($time));
}

sub adict_load {
    my $adictfile = shift;
    unless (open(ADICT, "<$adictfile")) {
	warn "Unable to read $adictfile";
	return;
    };
    my @entries;
    my $header = scalar <ADICT>; chomp $header;
    my @headers = split(/;/, $header);
    while (<ADICT>) {
	chomp;

	# Change the native (latin1) bytes to UTF-8 bytes.
	utf8::encode($_);

	my @f = split(/;/);
	my %info;
	my $index = 0;
	for my $header (@headers) {
	    $info{$header} = $f[$index++];
	}
	push @entries, \%info;
    }
    close ADICT;
    return @entries;
}

sub adict_date_lookup {
    my ($date, $name, $entriesref) = @_;

    my $count = 0;
    my @match;
    for my $entry (@{$entriesref}) {
	if ($entry->{DATE} eq $date) {
	    print STDERR $entry->{HEADLINE},"\n" if $debug;
	    $count++;
	    push @match, $entry;
	}
    }
    if ($count == 1) {
	return $match[0];
    } elsif ($count > 1) {
	# Not quite sure which entry to return, or how to select among
	# them.  Returning the last one seem to work fine for the
	# problematic ones for now, as the first is the annual
	# meeting.
	return pop @match;
    }
    
    return undef;
}
