[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

PERL :: Hash of Hashes




Hello folks,

Recently (for the past few months), I have been having the
necessity to remind some of my friends to write blogs.
Finally managed to write one such (thanks to books borrowed
from my sys-admins) and generic version of it is attached
with this mail.

It uses something called 'hash of hashes' and the concept
wasn't trivial to me. Though I use it for reminding my
friends, it could very well be used for other
'multi-dimensional' purposes. Comments/Suggestions to
make it more efficient are very welcome.

This and other scripts can also be downloaded from

 <http://sgowtham.net/scripts.html>

Cheers,
g

--
S Gowtham
Grad Student
Dept of Physics
Michigan Tech Univ, Houghton
http://phy.mtu.edu/~sgowtham
#! /usr/bin/perl -w

# PERL script to remind friends to write blogs, when they haven't
# done so in N days (N=15 in this example).
#
# This program is free software; you can redistribute it and/or modify it 
# under the terms of the GNU General Public License as published by the 
# Free Software Foundation; either version 2 of the License, or (at your 
# option) any later version.
# 
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 
# more details.
# 
# You should have received a copy of the GNU General Public License along 
# with this program; if not, write to the Free Software Foundation, Inc., 
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#

#
# Questions and/or comments should be sent to mail@xxxxxxxxxxxx
#
# First written : Gowtham, Fri, 02 Jun 2006 09:20:48 -0400 
# Last modified : Gowtham, Fri, 13 Oct 2006 15:56:16 -0400
#


# Pre-Requisites:
# 0. Friends who write blogs
# 1. Planet (or something like it) to aggregate your friends' blogs
#    into one place.
# 2. Ability to use 'mail' or 'pine' from the commandline to send
#    emails, without having to enter password. This needs a working
#    mail server and contact your sys-admin if you need more details.

#
# Part 1 : Fetch the contents of aggregated blog page.
# I use Planet for this purpose and it aggregates the blogs from last 15 days. 
# Fetched contents are stored in a file called 'index.txt' under the 
# current working directory. Change the URL to meet your requirements.
use LWP::UserAgent;
use URI::URL;

$hdr = new HTTP::Headers(Accept => 'text/plain', UserAgent => 'GBrowser/1.0');
$url = new URI::URL('http://sgowtham.net/news/people/index.html');
$req = new HTTP::Request(GET, $url, $hdr);
$uag = new LWP::UserAgent;
$rpl = $ua->request($req);
if ($rpl->is_success) {
  open (INDEX, "> ./index.txt");
  print INDEX $rpl->content;
  close (INDEX);
}
else {
  print $rpl->message;}

# 
# Part 2 : Create Hash-of-Hashes 
# This will contain list of bloggers, blog titles and email addresses. 
# Extend this to include as many friends you have.
my %BLOGGERS = (
 'Friend0' => {'title' => 'Blog Title 0', 'email' => 'friend0\@someplace.com'},
 'Friend1' => {'title' => 'Blog Title 1', 'email' => 'friend1\@someplace.com'},
 'Friend2' => {'title' => 'Blog Title 2', 'email' => 'friend2\@someplace.com'},
 'Friend3' => {'title' => 'Blog Title 3', 'email' => 'friend3\@someplace.com'},
  );

# 
# Part 3 : Send Reminders
# Search the file 'index.txt' for bloggers' entry in the last 15 days
# and remind them if there is none. This example uses 'mail' command. If
# it's not available for you for any reason, consider using PINE (it can
# be set up to send emails from commandline without entering password).
$date = localtime;
$file = "./index.txt";

foreach $key (keys %BLOGGERS) {
  $entry = qx(grep "$key</a></h4>" $file | wc -l);
  if ( $entry < 1 ) {
    print "Reminding $key \n";
    open MAILBODY, "| mail -s 'Eager To Read Your Blog' $BLOGGERS{$key}->{'email'}" or die "Cannot send email,\n";
    print MAILBODY "\n $date EST\n\n";
    print MAILBODY " Dear $key\n\n";
    print MAILBODY " It has been a while since you updated your blog,\n";
    print MAILBODY " titled \"$BLOGGERS{$key}->{'title'}\",\n";
    print MAILBODY " (none in the last 15 days or so). As you might\n";
    print MAILBODY " (or might not) know, there are people eager to\n";
    print MAILBODY " know about what's happening in your life and/or\n";
    print MAILBODY " your thoughts about what's happening around you.\n";
    print MAILBODY " If work's keeping you busy, it's OK - sacrifice a\n";
    print MAILBODY " meal (or two) and update your blog... just kidding.\n";
    print MAILBODY " Hopeful that it wouldn't be long to read one...";
    print MAILBODY " \n\n";
    print MAILBODY " Yours,\n";
    print MAILBODY " YOUR NAME\n\n";
    close (MAILBODY);
  }
}