nur schreiben der JSON wenn Werte sich geändert haben
This commit is contained in:
112
getwetter.pl
112
getwetter.pl
@@ -1,69 +1,75 @@
|
||||
use Mojo::UserAgent;
|
||||
use Data::Printer;
|
||||
#use Data::Printer;
|
||||
use File::Slurp;
|
||||
use JSON;
|
||||
|
||||
my $ua = Mojo::UserAgent->new;
|
||||
# # my $tx = $ua->get('https://wetter.lauters.heim-server.de/current.html')->result->body;
|
||||
my $tx = $ua->get('https://wetter.lauters.heim-server.de/current.html')->result->body;
|
||||
|
||||
# # my $netext = $tx =~ s/\R//rg;
|
||||
my $netext = $tx =~ s/\R//rg;
|
||||
|
||||
my $tx = $ua->get('https://wetter.lauters.heim-server.de/clientraw.txt')->result->body;
|
||||
append_file('clientraw.txt', "$tx");
|
||||
my $wohin = '';
|
||||
my $referenz = {
|
||||
"uvindex" => "0",
|
||||
"solarstrahlung" => "0",
|
||||
"taupunkt" => "0",
|
||||
"luftdruck" => "0",
|
||||
"luftfeuchte" => "0",
|
||||
"temp" => "0"
|
||||
};
|
||||
|
||||
# my $red = read_file('clientraw.txt');
|
||||
my @read = split(' ', $tx);
|
||||
|
||||
my $var = read_file('variablen.txt');
|
||||
my @vari = split(/\n/, $var);
|
||||
# p @vari;
|
||||
# p @read;
|
||||
|
||||
my $c = 0;
|
||||
my $json = {};
|
||||
for my $v ( @read ) {
|
||||
if ( $vari[$c] =~ /,/ ) {
|
||||
my ($wert, $einheit, $jsonkey ) = split(',', $vari[$c]);
|
||||
if ( $jsonkey ) {
|
||||
$json->{$jsonkey} = $read[$c];
|
||||
}
|
||||
print "$wert: $read[$c] $einheit\n";
|
||||
}
|
||||
|
||||
$c ++;
|
||||
if ( -e $wohin.'wetter.json') {
|
||||
my $red = read_file( $wohin. 'wetter.json');
|
||||
$referenz = decode_json($red);
|
||||
}
|
||||
|
||||
my $json = {
|
||||
"uvindex" => "0",
|
||||
"solarstrahlung" => "0",
|
||||
"taupunkt" => "0",
|
||||
"luftdruck" => "0",
|
||||
"luftfeuchte" => "0",
|
||||
"temp" => "0"
|
||||
};
|
||||
|
||||
# my @tr = $red =~ /<tr>(.*?)<\/tr>/g;
|
||||
my @tr = $netext =~ /<tr>(.*?)<\/tr>/g;
|
||||
|
||||
# for my $t ( @tr ) {
|
||||
# if ( $t =~ /<nobr>Temperatur Außen<\/nobr>/ ) {
|
||||
# my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)°C<\/font><\/b/g;
|
||||
# $json->{temp} = $aktuell =~ s/,/\./r;
|
||||
# }
|
||||
for my $t ( @tr ) {
|
||||
if ( $t =~ /<nobr>Temperatur Außen<\/nobr>/ ) {
|
||||
my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)°C<\/font><\/b/g;
|
||||
$json->{temp} = $aktuell =~ s/,/\./r;
|
||||
}
|
||||
|
||||
# if ( $t =~ /<nobr>Luftfeuchte Außen<\/nobr>/ ) {
|
||||
# my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)%<\/font><\/b/g;
|
||||
# $json->{luftfeuchte} = $aktuell;
|
||||
# }
|
||||
if ( $t =~ /<nobr>Luftfeuchte Außen<\/nobr>/ ) {
|
||||
my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)%<\/font><\/b/g;
|
||||
$json->{luftfeuchte} = $aktuell;
|
||||
}
|
||||
|
||||
# if ( $t =~ /<nobr>Luftdruck<\/nobr>/ ) {
|
||||
# my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)hPa<\/font><\/b/g;
|
||||
# $json->{luftdruck} = $aktuell =~ s/,/\./r;
|
||||
# }
|
||||
if ( $t =~ /<nobr>Luftdruck<\/nobr>/ ) {
|
||||
my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)hPa<\/font><\/b/g;
|
||||
$json->{luftdruck} = $aktuell =~ s/,/\./r;
|
||||
}
|
||||
|
||||
# if ( $t =~ /<nobr>Taupunkt<\/nobr>/ ) {
|
||||
# my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)°C<\/font><\/b/g;
|
||||
# $json->{taupunkt} = $aktuell =~ s/,/\./r;
|
||||
# }
|
||||
# if ( $t =~ /<nobr>UV-Index<\/nobr>/ ) {
|
||||
# my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)UV-I<\/font><\/b/g;
|
||||
# $json->{uvindex} = $aktuell =~ s/,/\./r;
|
||||
# }
|
||||
# if ( $t =~ /<nobr>Solarstrahlung<\/nobr>/ ) {
|
||||
# my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)W\/m²<\/font><\/b/g;
|
||||
# $json->{solarstrahlung} = $aktuell =~ s/,/\./r;
|
||||
# }
|
||||
# }
|
||||
if ( $t =~ /<nobr>Taupunkt<\/nobr>/ ) {
|
||||
my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)°C<\/font><\/b/g;
|
||||
$json->{taupunkt} = $aktuell =~ s/,/\./r;
|
||||
}
|
||||
if ( $t =~ /<nobr>UV-Index<\/nobr>/ ) {
|
||||
my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)UV-I<\/font><\/b/g;
|
||||
$json->{uvindex} = $aktuell =~ s/,/\./r;
|
||||
}
|
||||
if ( $t =~ /<nobr>Solarstrahlung<\/nobr>/ ) {
|
||||
my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)W\/m²<\/font><\/b/g;
|
||||
$json->{solarstrahlung} = $aktuell =~ s/,/\./r;
|
||||
}
|
||||
}
|
||||
|
||||
write_file( 'wetter.json', encode_json($json) );
|
||||
my $aenderungen;
|
||||
for my $ref ( keys %{$referenz} ) {
|
||||
if ( $referenz->{$ref} ne $json->{$ref} ) {
|
||||
$aenderungen = 1;
|
||||
}
|
||||
}
|
||||
|
||||
write_file( $wohin.'wetter.json', encode_json($json) ) if $aenderungen;
|
||||
# write_file( "/var/www/html/wetter/wetter.json", encode_json($json) );
|
||||
|
||||
69
getwetter_test.pl
Normal file
69
getwetter_test.pl
Normal file
@@ -0,0 +1,69 @@
|
||||
use Mojo::UserAgent;
|
||||
use Data::Printer;
|
||||
use File::Slurp;
|
||||
use JSON;
|
||||
|
||||
my $ua = Mojo::UserAgent->new;
|
||||
# # my $tx = $ua->get('https://wetter.lauters.heim-server.de/current.html')->result->body;
|
||||
|
||||
# # my $netext = $tx =~ s/\R//rg;
|
||||
|
||||
my $tx = $ua->get('https://wetter.lauters.heim-server.de/clientraw.txt')->result->body;
|
||||
append_file('clientraw.txt', "$tx");
|
||||
|
||||
# my $red = read_file('clientraw.txt');
|
||||
my @read = split(' ', $tx);
|
||||
|
||||
my $var = read_file('variablen.txt');
|
||||
my @vari = split(/\n/, $var);
|
||||
# p @vari;
|
||||
# p @read;
|
||||
|
||||
my $c = 0;
|
||||
my $json = {};
|
||||
for my $v ( @read ) {
|
||||
if ( $vari[$c] =~ /,/ ) {
|
||||
my ($wert, $einheit, $jsonkey ) = split(',', $vari[$c]);
|
||||
if ( $jsonkey ) {
|
||||
$json->{$jsonkey} = $read[$c];
|
||||
}
|
||||
print "$wert: $read[$c] $einheit\n";
|
||||
}
|
||||
|
||||
$c ++;
|
||||
}
|
||||
|
||||
|
||||
# my @tr = $red =~ /<tr>(.*?)<\/tr>/g;
|
||||
|
||||
# for my $t ( @tr ) {
|
||||
# if ( $t =~ /<nobr>Temperatur Außen<\/nobr>/ ) {
|
||||
# my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)°C<\/font><\/b/g;
|
||||
# $json->{temp} = $aktuell =~ s/,/\./r;
|
||||
# }
|
||||
|
||||
# if ( $t =~ /<nobr>Luftfeuchte Außen<\/nobr>/ ) {
|
||||
# my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)%<\/font><\/b/g;
|
||||
# $json->{luftfeuchte} = $aktuell;
|
||||
# }
|
||||
|
||||
# if ( $t =~ /<nobr>Luftdruck<\/nobr>/ ) {
|
||||
# my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)hPa<\/font><\/b/g;
|
||||
# $json->{luftdruck} = $aktuell =~ s/,/\./r;
|
||||
# }
|
||||
|
||||
# if ( $t =~ /<nobr>Taupunkt<\/nobr>/ ) {
|
||||
# my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)°C<\/font><\/b/g;
|
||||
# $json->{taupunkt} = $aktuell =~ s/,/\./r;
|
||||
# }
|
||||
# if ( $t =~ /<nobr>UV-Index<\/nobr>/ ) {
|
||||
# my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)UV-I<\/font><\/b/g;
|
||||
# $json->{uvindex} = $aktuell =~ s/,/\./r;
|
||||
# }
|
||||
# if ( $t =~ /<nobr>Solarstrahlung<\/nobr>/ ) {
|
||||
# my ($aktuell) = $t =~ /<b><font size="1">aktuell<\/font><\/b><br><b><font size="5">(.*?)W\/m²<\/font><\/b/g;
|
||||
# $json->{solarstrahlung} = $aktuell =~ s/,/\./r;
|
||||
# }
|
||||
# }
|
||||
|
||||
write_file( 'wetter.json', encode_json($json) );
|
||||
Reference in New Issue
Block a user