#!/usr/bin/env perl use strict; use warnings; use DateTime::Format::Strptime; use REST::Client; use Data::Roundtrip qw/:all/; use 5.016; use Log::Log4perl; use Data::Dump; # get rid of old log my $file = '/home/hogan/Documents/hogan/logs/4.log4perl.txt'; unlink $file or warn "Could not unlink $file: $!"; my $log_conf4 = "/home/hogan/Documents/hogan/logs/conf_files/4.conf"; Log::Log4perl::init($log_conf4); #info my $logger = Log::Log4perl->get_logger(); $logger->info($0); # this is our fetcher, similar to LWP::UserAgent # but better suited for this kind of web service: REST my $rest = REST::Client->new() or die "failed to construct client"; # set the host $rest->setHost('https://api.weather.gov'); # note to self: KTTD is troutdale my $query = "stations/KPDX/observations/latest"; # make the request and check the response code, 200 is good my $response = $rest->GET($query) or die "failed to GET($query)"; if ( $rest->responseCode() != 200 ) { die "failed to GET(" . $rest->getHost() . "/$query) with " . $rest->responseCode(); } # we get back JSON my $jsonstr = $response->responseContent(); # convert JSON string to a perl variable my $pv = json2perl($jsonstr); if ( !defined $pv ) { die "something wrong with this alleged json : '$jsonstr'"; } # go to the interesting part my $var1 = $pv->{'properties'}->{'cloudLayers'}; $logger->info(perl2dump($var1)); ## failure #my %cl = %$var1; #my $cl1 = $cl{'amount'}; #say "cl 1 is $cl1"; # try heat index # try to pluck out the reference: my $var2 = $pv->{'properties'}->{'heatIndex'}; my %cl2 = %$var2; my $str2 = $cl2{'value'}; say "str 2 is $str2"; $logger->info(perl2dump($str2)); # this ain't right # let's get the timestamp my $var3 = $pv->{'properties'}->{'timestamp'}; say "var3 is $var3"; print perl2dump($var3); $logger->info(perl2dump($var3)); ## from bliako on same host (hope these are similar) # we have some dates in the data in ISO8601 format # this is a parser to convert that date to a DateTime # object which we can query about things (like seconds-unix-epoch) my $dateparser = DateTime::Format::Strptime->new( # parses 2020-04-26T06:00:00-05:00, # %F then literal T then %T then timezone offset pattern => '%FT%T%z' ) or die "failed to DateTime::Format::Strptime->new()"; my $time_dateobj = $dateparser->parse_datetime($var3) or die "parsing date failed"; my $td = $time_dateobj->strftime('%Y-%m-%d %H:%M:%S'); say "td is $td"; $logger->info("td is $td"); ## check to see what julian day it is use DateTime; my $julian=$time_dateobj->jd(); # this ain't right $logger->info("julian day is $td"); ### use retrieved time to figure out where sun is concurrently # use a perl automation tool that works for the $site* in question, # *which is written kinda funny use WWW::Mechanize; use HTML::TableExtract qw(tree); use open ':std', OUT => ':utf8'; my $site = 'http://www.fourmilab.ch/yoursky/cities.html'; my $mech = WWW::Mechanize->new; $mech->get($site); $mech->follow_link( text => 'Portland OR' ); $mech->set_fields(qw 'date 1'); $mech->set_fields( utc => $td ); $mech->click_button( value => "Update" ); my $te = 'HTML::TableExtract'->new; $te->parse( $mech->content ); my $table = ( $te->tables )[3]; my $table_tree = $table->tree; my $row = 2; # the sun my @sun = $table_tree->row($row)->as_text; $logger->info("row sun is @sun"); my $name = $table_tree->cell($row,0)->as_text; $logger->info("name: $name"); my $altitude = $table_tree->cell($row,4)->as_text; $logger->info("altitude: $altitude"); my $azimuth = $table_tree->cell($row,5)->as_text; $logger->info("azimuth: $azimuth"); my $visible = $table_tree->cell($row,6)->as_text; $logger->info("visible?: $visible"); $te->delete; __END__