DailyRiczWest – behind the scenes v0.6

Newspaper MegaphoneHuoston – we have full automation! :-)

At last, I’ve bitten the bullet and actually automated the whole thing (for this month at least – more on that later). Up until now, I’ve been manually tweaking page info etc… but now I’ve broken the header in to 4 html fragments:

header1.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>

<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>DailyRiczWest -

header2.html

</title></head><body>
<big><big><span style="font-family: Arial Black;"></span></big></big>
<table style="text-align: left; width: 100%;" border="0" cellpadding="0" cellspacing="5">
<tbody>
<tr>
<td style="vertical-align: top; height: 1px; width: 1px;"><img style="width: 70px; height: 70px;" src="../../images/newspaperMegaphone.jpg" alt=""></td>
<td style="vertical-align: top;"><big><big><span style="font-family: Arial Black;">Daily</span></big></big><big><big><span style="font-family: Arial Black;">RiczWest</span></big></big><span style="font-family: Helvetica,Arial,sans-serif;"> for

header3.html

</span><br>
<span style="font-family: Helvetica,Arial,sans-serif;">=&gt; not
your
ordinary daily feed - version

header4.html

<br>
<small><span style="font-weight: bold;">NOTE: </span>Will be
continually evolving, i.e. improving quite quickly</small></span></td>
</tr>
</tbody>
</table>
<span style="font-family: Helvetica,Arial,sans-serif;"></span>

Ah yes, takes me back to the “good old days”, pre frameworks and a whole bunch of other crap ;-)

Anyway, to the code, which is starting to get a bit messy now, with the actual maintenance and archiving of files, along with the fully automated generation. It will definitely be time for some GitHubbing and refactoring, but for the mean time, here it is in all it’s glory (not quite ;)

#!/usr/bin/perl -w
# use strict;

# IMPORTANT NOTE: This WILL NOT handle month & year rollovers yet!

use File::Copy;
use Time::Local;

$version = "0.6";

$ifttt = "/Users/rwest/Dropbox/ifttt";
$ifttt_favourites = "$ifttt/favourites.txt";
$templates = "../../templates";

@month_text = qw( jan feb mar apr may jun jul aug sep oct nov dec );
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$month = $month_text[$mon];
$year += 1900;
$twitter_month = "/Users/rwest/Dropbox/Public/Twitter/2012/$month";
$daily_day = $mday-1;
$twitter_date = "$daily_day $month, $year";
print "DailyRiczWestDate : ", $twitter_date;

chdir("$twitter_month"); # Process in month directory

# Assemble header
copy("$templates/header1.html", "header.html") or die "$templates/header1.html cannot be copied to $twitter_month";
open (HEADER, ">> header.html") or die ("Cannot Append to header.html - after header1.html");
print HEADER $twitter_date; close(HEADER);
system("cat $templates/header2.html >> header.html");
open (HEADER, ">> header.html") or die ("Cannot Append to header.html - after header2.html");
print HEADER $twitter_date; close(HEADER);
system("cat $templates/header3.html >> header.html");
open (HEADER, ">> header.html") or die ("Cannot Append to header.html - after header3.html");
print HEADER $version; close(HEADER);
system("cat $templates/header4.html >> header.html");

# Process Body
copy($ifttt_favourites, "favourites.html") or die "$ifttt_favourites cannot be copied to $twitter_month";

open(FAVOURITES,"< favourites.html") or die("Cannot Read favourites.html");
open(BODY,"> body.html") or die("Cannot Write body.html");

# Replace *BR* with <br> because ifttt will not pass through <br>
while (<FAVOURITES>) {
    s/\*BR\*/<br>/;
    print BODY;
}

close BODY; close FAVOURITES;

# Assemble Daily
system("cat header.html body.html $templates/footer.html > daily-$daily_day.html");
system("rm header.html favourites.html body.html");
system("mv $ifttt_favourites $ifttt/archive/favourites-$daily_day$month$year.txt");

Finally I have something that totally automatically:

  • Assembles the Page: Header (with date & version), Body and Footer
  • Archives the favourites.txt file should I need it in it’s raw form
  • Is designed to be invoked from cron on the day after the favorites are gathered

It’s far from ideal, as the code is no longer so clear and it won’t work properly when the month and year change, but I have 3 days to fix the month problem and 4 months to fix the year one ;-)

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s