stuff
This commit is contained in:
parent
1c66e98d77
commit
dc7624ff53
75
new.pl
Executable file
75
new.pl
Executable file
@ -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~<h1>([^<]+)</h1>~;
|
||||
$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";
|
Loading…
x
Reference in New Issue
Block a user