I programmed a perl script to mail the current river flow for a certain
river every hour if the flow is over 10,000 (feet per second).  Here is
my script:

#!/usr/bin/env perl

if (system("ls -l /etc/mailsnoq.txt") eq 256) {
        print "Getting web page (file nonexistent)...\n";
        system "wget http://waterdata.usgs.gov/wa/nwis/uv?site_no=12144500 -O 
/etc/snoqualmie.html";
}

# is there an easier way to see if a file exists 
# besides seeing if the list command works?  
# (if it doesn't exist returning 256)

# also, is there an easier way to get the modification time of a file,
# in seconds, rather than the following?

if (open FSTAT, "/etc/mailsnoq.txt")
{
        $filestat = stat(FSTAT);
        $mt = $filestat[9];
        $currenttime = time; print "mtime - $mt curtime - $currenttime\n";
        $elapsed = $currenttime - $mt;
        print "Elapsed time: $elapsed\n";
# update the web page only if it's been more than 15 minutes
        if (($elapsed)  > (15 * 60)) {
                system "rm mailsnoq.txt";
                print "Getting web page (out of date)...\n";
                system "wget http://waterdata.usgs.gov/wa/nwis/uv?site_no=12144500 -O 
/etc/snoqualmie.html";
        }
        close FSTAT;
}

open INFILE,   "/etc/snoqualmie.html" or die "Can't open snoqualmie.html: $!";
while (<INFILE>)
{
  if ($_ =~ /(Most recent value:)\s(..,...)\s&nbsp;\s(..-..-....)&nbsp;\s(..:..)/) {
        #a better way to do it?  
          print "$1 $2 $3 $4\n"; 
          open OUTFILE, ">/etc/mailsnoq.txt" or die "error opening file for write: $!";
          print OUTFILE "From: Bryce\n";
          print OUTFILE "To: Peter\n";
          print OUTFILE "Return-Path: [EMAIL PROTECTED]\n";
          print OUTFILE "\n";
          print OUTFILE "Snoqualmie most recent flow: $2 fps $3 $4\n";
          close INFILE;
          close OUTFILE;
          exec "/usr/local/bin/mailsnoqualmie";  
                #executes a script that sends the file via sendmail
  }
} 

I'm pretty new with Perl, and used the man pages to supplement my very
basic knowledge.  It took about 6 hours to write and debug it.

Any critiques, assistance, etc. would be greatly appreciated!

-Bryce

(in the future I might even make a mailing list for those who wanted
these river flow updates -- Snoqualmie Falls is very popular -- but I
have school so I can't spend much time on it right now)




Want to buy your Pack or Services from MandrakeSoft? 
Go to http://www.mandrakestore.com

Reply via email to