databasetastic

This commit is contained in:
foo 2014-11-07 19:59:40 +00:00
parent f786b04efb
commit 68e1e6539c

29
new.pl
View File

@ -26,11 +26,16 @@ sub newer($$) {
} }
my $setlastrecord = $db->prepare("REPLACE INTO lastrecord (id, timestamp) VALUES (?, ?)") or die $db->errstr; my $setlastrecord = $db->prepare("REPLACE INTO lastrecord (id, timestamp) VALUES (?, ?)") or die $db->errstr;
sub setlastrecord($$) { my $recordlevel = $db->prepare("INSERT INTO levels (stationid, timestamp, level) VALUES (?,?,?)") or die $db->errstr;
my ($id, $time) = @_; sub recordlevel($$$) {
my ($id, $time, $level) = @_;
$recordlevel->execute($id, $time, $level);
$setlastrecord->execute($id, $time); $setlastrecord->execute($id, $time);
} }
my $rememberstation = $db->prepare("INSERT INTO stations (id, name, watercourse, datum) VALUES (?,?,?,?)") or die $db->errstr;
# First thing on the line should be the current time # First thing on the line should be the current time
print time, "\t" unless $ENV{R_GPLOT}; print time, "\t" unless $ENV{R_GPLOT};
@ -43,7 +48,9 @@ foreach my $station (@stations) {
my $level; my $level;
my $stationname; my $stationname;
my $lastupdate; my $lastupdate;
if ($res->is_success) { my $watercourse;
my $datum;
if ($res->is_success and $res->content =~ m~Current level:~) {
$res->content =~ m~<h1>([^<]+)</h1>~; $res->content =~ m~<h1>([^<]+)</h1>~;
$stationname = $1; $stationname = $1;
@ -54,12 +61,24 @@ foreach my $station (@stations) {
$lastupdate = "$4-$3-$2T$1"; $lastupdate = "$4-$3-$2T$1";
if($ENV{R_GPLOT}) { if($ENV{R_GPLOT}) {
$res->content =~ m~Watercourse:(&nbsp;|\s)+([^<]+)~;
$watercourse = $2;
$watercourse =~ s~\s*$~~;
$watercourse =~ s~^\s*~~;
$res->content =~ m~Site datum:(&nbsp;|\s)+([^<]+) m AOD~;
$datum = $2;
$datum =~ s~\s*$~~;
$datum =~ s~^\s*~~;
print " \"data.tsv\" using 1:$c with linespoints title \"$stationname\", \\\n"; print " \"data.tsv\" using 1:$c with linespoints title \"$stationname\", \\\n";
$rememberstation->execute($station, $stationname, $watercourse, $datum);
} else { } else {
if($level) { if($level) {
if(newer($station, $lastupdate)) { if(newer($station, $lastupdate)) {
print $level; print $level;
setlastrecord($station, $lastupdate); recordlevel($station, $lastupdate, $level);
} else { } else {
print "-"; print "-";
} }
@ -68,7 +87,7 @@ foreach my $station (@stations) {
} }
print "\t"; print "\t";
} }
} } else { print "-\t";}
$c++; $c++;
} }