Flagg
The Most Electrifying Man in Sports Entertainment
I always get nervous about things like this getting lost, even though the server has redundant disks and regular backups. The script which auto-updates the comic is close to 400 lines of code now, and it would be a huge PITA to re-do.
So, just in case I need to cut-and-paste it from here later:
So, just in case I need to cut-and-paste it from here later:
Code:
#!/usr/bin/perl
## Keychain of Creation Updater Script, by Flagg <flagg@patternspider.net>
##
## Created 20070815
## Updated 20070819 - added notify_email_list()
## Updated 20070820 - Fixed archive nav bug in generate_page()
## Updated 20070902 - Added image size calculation and added "news" to email notification
## Updated 20080104 - Added functionality for RSS feed - generate_rss_page()
## Updated 20080111 - Added code to 'ping' the feedburner RSS feed
use strict;
use Image::Size 'html_imgsize';
use HTML::Strip;
use LWP::Simple;
my $basepath = "/home/patternspider.net/www/keychain/"; Â Â Â Â # path to KOC
my $comicpath = "comics"; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â # path to comics
my $bufferpath = "buffer"; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â # path to buffer
my $archivepath = "archive"; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â # path to archives
my $templatepath = "script/template"; Â Â Â Â Â Â Â Â Â Â Â Â Â # path to templates
my $newspath = "news"; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â # path to news & synopses
my $imagepath = "images"; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â # path to images
my $prefix = "koc"; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â # file name prefix
my $imgext = "png"; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â # image file extension
my $curr = get_current(); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â # current issue number
my $currstring = pad_number($curr); Â Â Â Â Â Â Â Â Â Â Â Â Â Â # padded current issue string
my $prevstring = pad_number($curr - 1); Â Â Â Â Â Â Â Â Â Â Â Â # padded previous issue
my $prevarchivestring = pad_number($curr - 2); Â Â Â Â Â Â Â Â Â # padded previous archive string
### begin program
chdir $basepath;
# if these files exist, continue
if ( check_files() ) {
    # move new comic out of buffer
    system ("/bin/mv $bufferpath/$prefix$currstring.$imgext $comicpath/$prefix$currstring.$imgext");
    # generate new archive page
    generate_page("$archivepath/$prefix$prevstring.html", "$templatepath/archiveheader.html", "", "$templatepath/archivefooter.html", ($curr - 1), 1);
    # re-generate old archive page
    generate_page("$archivepath/$prefix$prevarchivestring.html", "$templatepath/archiveheader.html", "", "$templatepath/archivefooter.html", ($curr - 2), 1);
    # generate new index file
    generate_page("$basepath/index.html", "$templatepath/mainheader.html", "$newspath/$prefix$currstring.news.txt", "$templatepath/mainfooter.html", $curr, 0);
    # update archives page
    update_archives_page();
    # update current file
    set_current();
    # update RSS feed
    update_rss_page("$newspath/$prefix$currstring.news.txt", "$newspath/$prefix$currstring.title.txt");
    print "\nKeychain of Creation #$curr has been updated successfully.\n\n";
    notify_email_list("$newspath/$prefix$currstring.news.txt");
}
else { print "\nKeychain of Creation has not been updated.\n\n" }
### end program
### subroutines
# update current file
sub set_current {
    my $currentfile = "$basepath/script/current.issue";
    open(CURRENT, ">$currentfile");
    print CURRENT $curr;
    close(CURRENT);
}
# read current file
sub get_current {
    my $currentfile = "$basepath/script/current.issue";
    my $r;
    open(CURRENT, "<$currentfile");
    $r = <CURRENT>;
    close(CURRENT);
    chomp($r);
    $r++;
    return $r;
}
# update archive spage
sub update_archives_page {
    my $outfile = "$basepath/archives.html";
    my $headerfile = "$templatepath/mainheader.html";
    my @header;
    my $footerfile = "$templatepath/mainfooter.html";
    my @footer;
    my $titlefile = "$newspath/$prefix$currstring.title.txt";
    my $title;
    my $prevtitlefile = "$newspath/$prefix$prevstring.title.txt";
    my $prevtitle;
    my $archivefile = "$templatepath/archives.html";
    my @archivesin;
    my @archivesout;
    my $prevnumber = int($prevstring);
    open(HEADER, $headerfile);
    @header = <HEADER>;
    close(HEADER);
    open(FOOTER, $footerfile);
    @footer = <FOOTER>;
    close(FOOTER);
    open(TITLE, $titlefile);
    $title = <TITLE>;
    close(TITLE);
    chomp($title);
    open(PREVTITLE, $prevtitlefile);
    $prevtitle = <PREVTITLE>;
    close(PREVTITLE);
    chomp($prevtitle);
    open(ARCHIVES, "<$archivefile");
    @archivesin = <ARCHIVES>;
    close(ARCHIVES);
    for (my $x = 0; $x < (@archivesin - 1); $x++) {
        $archivesout[$x] = $archivesin[$x];
        next;
    }
    open(ARCHIVES, ">$archivefile");
    print ARCHIVES @archivesout;
    print ARCHIVES "\n       <a href=\"$archivepath/$prefix$prevstring.html\">Comic $prevnumber</a> - $prevtitle<br />";
    print ARCHIVES "\n       <a href=\"index.html\">Comic $curr</a> - $title<br />";
    close(ARCHIVES);
    open(ARCHIVES, "<$archivefile");
    @archivesout = <ARCHIVES>;
    close(ARCHIVES);
    open(OUTFILE, ">$outfile");
    print OUTFILE @header;
    print OUTFILE @archivesout;
    print OUTFILE "</p>\n";
    print OUTFILE @footer;
    close(OUTFILE);
}
# generate output page
sub generate_page {
    my $outfile = $_[0];                       # output file
    my $headerfile = $_[1];                     # page header file
    my @header;                           # page header code
    my $newsfile = "$_[2]";                     # news file
    my @news;                            # news file code
    my $footerfile = $_[3];                     # page footer file
    my @footer;                           # page footer code
    my $current = pad_number($_[4]);                 # issue number to create
    my $previous = pad_number($_[4] - 1);              # previous issue
    my $next = pad_number($_[4] + 1);                # next issue
    my $intcurrent = $_[4];                     # current issue in int format
    my $isarchive = $_[5];                      # boolean check to see page is archive
    my $pathtocomics;
    my $pathtonav;
    my $pathtomenu;
    my $pathtoimages;
    my $imgsize = html_imgsize("$comicpath/$prefix$current.$imgext"); # returns preformatting width/height tags
    if ($isarchive){
        $pathtocomics = "../$comicpath";
        $pathtonav = ".";
        $pathtomenu = "..";
        $pathtoimages = "../$imagepath";
    }
    else {
        $pathtocomics = "$comicpath";
        $pathtonav = "archive";
        $pathtomenu = ".";
        $pathtoimages = "$imagepath";
    }
    # open templates and read in data
    open(HEADER, $headerfile);
    @header = <HEADER>;
    close(HEADER);
    open(FOOTER, $footerfile);
    @footer = <FOOTER>;
    close(FOOTER);
    open(NEWS, $newsfile);
    @news = <NEWS>;
    close(NEWS);
    open(OUTFILE, ">$outfile");
    print OUTFILE "@header";
    # print CURRENT comic
    print OUTFILE "<img src=\"$pathtocomics/$prefix$current.$imgext\" alt=\"Comic $intcurrent\" $imgsize />";
    print OUTFILE "  <br />\n<br />";
    # print FIRST nav button
    print OUTFILE "  <td><a href=\"$pathtonav/koc0001.html\"><img src=\"$pathtoimages/first.gif\" alt=\"first\" width=\"144\" height=\"100\" border=\"0\" /></a>";
    # print BACK nav button
    print OUTFILE "<a href=\"$pathtonav/$prefix$previous.html\"><img src=\"$pathtoimages/back.gif\" alt=\"back\" width=\"134\" height=\"95\" border=\"0\" align=\"bottom\" /></a>\n";
    # if page is in archive
    if ( $isarchive ) {
        # if not newest archive page, print FORWARD and LATEST nav buttons
        if ( int($current) == ($curr - 2) ) {
            print OUTFILE "<a href=\"$pathtonav/$prefix$next.html\"><img src=\"$pathtoimages/forward.gif\" alt=\"forward\" width=\"123\" height=\"100\" border=\"0\" /></a>";
            print OUTFILE "<a href=\"$pathtomenu/index.html\"><img src=\"$pathtoimages/latest.gif\" alt=\"latest\" width=\"118\" height=\"96\" border=\"0\" /></a>";
        }
        # if newest archive page, print FORWARD nav button
        else {
            print OUTFILE "<a href=\"$pathtomenu/index.html\"><img src=\"$pathtoimages/forward.gif\" alt=\"forward\" width=\"123\" height=\"100\" border=\"0\" /></a>";
        }
    }
    # if not in archive, print NEWS
    else {
        print OUTFILE "  <hr size=\"1\" width=\"500\"/>\n<img src=\"$pathtoimages/newsheader.gif\" alt=\"news\" width=\"111\" height=\"36\" />\n<br /><br />\n";
        print OUTFILE "  <table width=\"500\" border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"2\">\n  <tr>\n    <td valign=\"top\"><div align=\"justify\" class=\"style3\">\n";
        print OUTFILE "@news";
    }
    print OUTFILE "@footer";
    close(OUTFILE);
}
# pad issue number with zeroes
sub pad_number {
    my $input = $_[0];    # input value
    my $r;          # return value
    if ($input < 10)     { $r = "000$input" }
    elsif ($input < 100)   { $r = "00$input" }
    elsif ($input < 1000)  { $r = "0$input" }
    return $r;
}
# check that all critical files exist before proceeding
sub check_files {
    my $filesexist = 1;               # boolean check for existence of necessary files
    # check to see if the new current.issue file exists
    if ( !( open(CHECK, "<script/current.issue") ) ) {
        print "!! ERROR: File $templatepath/current.issue does not exist!\n";
        $filesexist = 0;
    }
    else { close(CHECK) }
    # check to see if the main header file exists
    if ( !( open(CHECK, "<$templatepath/mainheader.html") ) ) {
        print "!! ERROR: File $templatepath/mainheader.html does not exist!\n";
        $filesexist = 0;
    }
    else { close(CHECK) }
    # check to see if the main footer file exists
    if ( !( open(CHECK, "<$templatepath/mainfooter.html") ) ) {
        print "!! ERROR: File $templatepath/mainfooter.html does not exist!\n";
        $filesexist = 0;
    }
    else { close(CHECK) }
    # check to see if the archive header file exists
    if ( !( open(CHECK, "<$templatepath/archiveheader.html") ) ) {
        print "!! ERROR: File $templatepath/archiveheader.html does not exist!\n";
        $filesexist = 0;
    }
    else { close(CHECK) }
    # check to see if the archive footer file exists
    if ( !( open(CHECK, "<$templatepath/archivefooter.html") ) ) {
        print "!! ERROR: File $templatepath/archivefooter.html does not exist!\n";
        $filesexist = 0;
    }
    else { close(CHECK) }
    # check to see if the new comic exists in the buffer
    if ( !( open(CHECK, "<$bufferpath/$prefix$currstring.$imgext") ) ) {
        print "!! ERROR: File $bufferpath/$prefix$currstring.$imgext does not exist!\n";
        $filesexist = 0;
    }
    else { close(CHECK) }
    # check to see if the previous comic exists in the archive
    if ( !( open(CHECK, "<$archivepath/$prefix$prevarchivestring.html") ) ) {
        print "!! ERROR: File $archivepath/$prefix$prevarchivestring.html does not exist!\n";
        $filesexist = 0;
    }
    else { close(CHECK) }
    return $filesexist;
}
# Send email to distribution list
sub notify_email_list {
    my $newsfile = $_[0];
    my @news;
    my $dest = "To: keychain-of-creation\@googlegroups.com\n";
    my $subject = "Subject: Issue #$curr has been released!\n";
    my $reply_to = "Reply-to: jukashi\@patternspider.net\n";
    my $sendmail = "/usr/sbin/sendmail -t";
    my $message;
    open(NEWS, $newsfile);
    @news = <NEWS>;
    close(NEWS);
    $message = "<a href=\"http://keychain.patternspider.net\">Keychain of Creation</a><br><br>NEWS:<br>@news<br>";
    open (SENDMAIL, "|$sendmail");
    print SENDMAIL $dest;
    print SENDMAIL $reply_to;
    print SENDMAIL $subject;
    print SENDMAIL "Content-type: text/html; charset=iso-8859-1\n\n";
    print SENDMAIL $message;
    close (SENDMAIL);
}
sub update_rss_page {
    my $htmlstrip = HTML::Strip->new();
    my $outfile = "$basepath/rss.xml";
    my $headerfile = "$templatepath/rssheader.xml";
    my @header;
    my $footerfile = "$templatepath/rssfooter.xml";
    my @footer;
    my $titlefile = $_[1];
    my $title;
    my $rssfile = "$templatepath/rssbody.xml";
    my @rssin;
    my @rssout;
    my $newsfile = $_[0];
    my @news;
    my @htmlnews;
    my $pingurl = "http://www.feedburner.com/fb/a/pingSubmit?bloglink=http://keychain.patternspider.net";
    open(NEWS, $newsfile);
    @htmlnews = <NEWS>;
    close(NEWS);
    my @news = $htmlstrip->parse( @htmlnews );
    $htmlstrip->eof;
    open(HEADER, $headerfile);
    @header = <HEADER>;
    close(HEADER);
    open(FOOTER, $footerfile);
    @footer = <FOOTER>;
    close(FOOTER);
    open(TITLE, $titlefile);
    $title = <TITLE>;
    close(TITLE);
    chomp($title);
    open(RSS, "<$rssfile");
    @rssin = <RSS>;
    close(RSS);
    for (my $x = 0; $x < 14; $x++) {
        $rssout[$x] = $rssin[$x];
        next;
    }
    open(RSS, ">$rssfile");
    print RSS "<item><title>Comic $curr - $title</title><description><img src=http://keychain.patternspider.net/comics/$prefix$currstring.$imgext /> <br/> <br/> @news</description><link>http://keychain.patternspider.net</link></item>\n";
    print RSS @rssout;
    close(RSS);
    open(RSS, "<$rssfile");
    @rssout = <RSS>;
    close(RSS);
    open(OUTFILE, ">$outfile");
    print OUTFILE @header;
    print OUTFILE @rssout;
    print OUTFILE @footer;
    close(OUTFILE);
    # ping feedburner feed
    get $pingurl;
}