#!/usr/local/bin/perl use strict; use warnings; use Data::Dumper::Simple; use FindBin; use lib "$FindBin::Bin/../../modules"; use SlapPhone; use CGI; use Time::localtime; my $cgi = CGI->new(); my $tm = localtime; my $raw_year = $tm->year; #my $raw_year = (localtime)[5]; my $year = ( $raw_year + 1900); my $epoch = time(); #read the conf file open (CONFIG,") { chomp; # no newline s/#.*//; # no comments s/^\s+//; # no leading white s/\s+$//; # no trailing white next unless length; # anything left? my ($var, $value) = split(/\s*=\s*/, $_, 2); $CONFIG{$var} = $value; } close CONFIG; #connect to the db my $dbh = DBI->connect("dbi:mysql:database=$CONFIG{'DB_NAME'};host=$CONFIG{'DB_HOST'}", $CONFIG{'DB_USER'}, $CONFIG{'DB_PASS'}) or die "Cannot connectto database: $!"; #get the category info my @cats; { my $query = $dbh->prepare("select * from book b, node n where b.nid=n.nid and b.parent = 4 and n.status=1 order by weight"); $query->execute(); while ( my $row = $query->fetchrow_hashref() ) { push (@cats, $row->{'nid'}); } } #get the faqs my @faqs; { my $sql_cats = commify (\@cats); my $query = $dbh->prepare("select n.nid, title, body, log from book b, node n where b.nid=n.nid and b.parent in ($sql_cats) and n.status=1 order by created desc"); $query->execute(); while ( my $row = $query->fetchrow_hashref() ) { push (@faqs, $row); } } #print $cgi->header; #print "
";
#print Dumper(@cats);
#print Dumper(@faqs);
# create an RSS 2.0 file
use XML::RSS;
my $rss = new XML::RSS (version => '2.0');
my $link = "http://slapcast.com/faq";
my $gmt = rfc822date($epoch);
$rss->channel(
title => "Slapcast.com FAQ",
link => $link,
language => 'en-us',
description => "Frequently Asked Questions",
copyright => "Copyright $year",
pubDate => $gmt,
lastBuildDate => $gmt,
docs => 'http://blogs.law.harvard.edu/tech/rss',
generator => 'http://slapcast.com/',
);
foreach my $faq (@faqs) {
my $guid = $faq->{'nid'};
my $faq_url = "http://slapcast.com/faq#" . $guid;
my $description;
$description .= "" . $faq->{'title'} . "\n\n";
if ($faq->{'log'}) {
$description .= $faq->{'log'};
} else {
$description .= $faq->{'body'};
}
$description =~ s/\n/
/g;
#add the message to the rss
$rss->add_item(
title => $faq->{'title'},
link => $faq_url,
permaLink => $faq_url,
#enclosure => { url=>$mp3_url, length=>$message->{'mp3_bytes'}, type=>"audio/mpeg" },
description => $description
);
}
# print the RSS as a string
my $rss_string = $rss->as_string;
#$rss_string =~ s/</g; # temp
print $cgi->header(-type => 'text/xml');
print $rss_string;
#print "";
sub commify {
my($rarrInput) = $_[0];
(@{$rarrInput} == 0) ? '' :
(@{$rarrInput} == 1) ? $$rarrInput[0] :
join(", ", @{$rarrInput}[0 .. ($#{$rarrInput}-1)], "$$rarrInput[-1]");
}
sub rfc822date {
my $time = shift;
my ($sec, $min, $hour, $mday, $mon, $year, $wday) = (gmtime($time))[0..6];
my $day = (qw(Sun Mon Tue Wed Thu Fri Sat))[$wday];
my $month = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$mon];
return sprintf "%s, %02d %s %d %02d:%02d:%02d GMT", $day, $mday, $month, $year+1900, $hour, $min, $sec;
}