From dc7624ff537e183c9cf731f3a857faa80569cfee Mon Sep 17 00:00:00 2001 From: foo Date: Fri, 7 Nov 2014 19:22:07 +0000 Subject: [PATCH] stuff --- new.pl | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100755 new.pl diff --git a/new.pl b/new.pl new file mode 100755 index 0000000..6652bf2 --- /dev/null +++ b/new.pl @@ -0,0 +1,75 @@ +#!/usr/bin/perl -w +$|=1; +use strict; +use diagnostics; +use DBI; + +use LWP::UserAgent; +my $ua = LWP::UserAgent->new; +$ua->agent("Munin[river_levels_]"); + +my @stations = qw( +1129 1135 1133 1143 1132 1137 1134 1145 1139 1131 1130 1144 +1086 1085 1073 1091 1071 1076 1069 1066 1067 1072 1074 1083 1064 1090 1089 1088 1087 1078 1080 1063 +); + +my $base = "http://apps.environment-agency.gov.uk/river-and-sea-levels/136486.aspx?stationId="; + +my $db = DBI->connect("dbi:SQLite:dbname=rivers.db") or die $DBI::errstr; + +my $getlastrecord = $db->prepare("SELECT timestamp FROM lastrecord WHERE id=?") or die $db->errstr; +sub newer($$) { + my ($id, $time) = @_; + $getlastrecord->execute($id); + my ($lastrecord) = $getlastrecord->fetchrow(); + return $lastrecord ? $time gt $lastrecord : 1; +} + +my $setlastrecord = $db->prepare("REPLACE INTO lastrecord (id, timestamp) VALUES (?, ?)") or die $db->errstr; +sub setlastrecord($$) { + my ($id, $time) = @_; + $setlastrecord->execute($id, $time); +} + +# First thing on the line should be the current time +print time, "\t" unless $ENV{R_GPLOT}; + +my $c = 2; +foreach my $station (@stations) { + + my $req = HTTP::Request->new(GET => $base . $station); + my $res = $ua->request($req); + + my $level; + my $stationname; + my $lastupdate; + if ($res->is_success) { + $res->content =~ m~

([^<]+)

~; + $stationname = $1; + + $res->content =~ m~Current level: ([\d\.]+)m~; + $level = $1; + + $res->content =~ m~This measurement was recorded at (\d+:\d+) on (\d+)/(\d+)/(\d+)~; + $lastupdate = "$4-$3-$2T$1"; + + if($ENV{R_GPLOT}) { + print " \"data.tsv\" using 1:$c with linespoints title \"$stationname\", \\\n"; + } else { + if($level) { + if(newer($station, $lastupdate)) { + print $level; + setlastrecord($station, $lastupdate); + } else { + print "-"; + } + } else { + print "-"; + } + print "\t"; + } + } + $c++; +} + +print "\n";