#!/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"); ## important...no trailing zeroes else 301 error my $lat = 33.4; my $long = -112.1; my $west_long = -$long; $logger->info("$lat $long $west_long"); # 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"; # see examples in # https://www.weather.gov/documentation/services-web-api # set the host $rest->setHost('https://api.weather.gov'); # https://api.weather.gov/points/{latitude},{longitude} # https://api.weather.gov/points/45.4836356,-122.4170501 # and this is our query with lat,long specified above #my $query = "gridpoints/TOP/$lat,$long/forecast"; #https://api.weather.gov/stations/PQR/observations/latest KTTD my $query = "points/$lat,$long"; # 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 ) { warn "something wrong with this alleged json : '$jsonstr'"; } my $station = $pv->{'properties'}->{'cwa'}; my $gridX = $pv->{'properties'}->{'gridX'}; my $gridY = $pv->{'properties'}->{'gridY'}; $logger->info("$station"); $logger->info("$gridX $gridY"); # or print it all and examine it #$logger->info(perl2dump($pv)); ## use mojo now use Mojo::UserAgent; use open ':std', OUT => ':utf8'; use Mojo::Util qw(dumper); my $url = "https://api.weather.gov/gridpoints/$station/$gridX,$gridY/forecast/hourly"; $logger->info("=============="); $logger->info("$url"); # the API docs says you must identify yourself, please make this something legit my $name = '(example.com, contact@example.com)'; my $ua = Mojo::UserAgent->new; $ua->transactor->name($name); # get JSON response my $forecasts = $ua->get($url)->res->json->{properties}->{periods}; use DateTime::Format::Strptime; 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 @ordered; my $refordered = \@ordered; my $index = 0; for my $aforecast (@$forecasts) { my $number = $aforecast->{number}; # start and end times of the prediction, convert them to objects my $start_time_str = $aforecast->{'startTime'}; say "start is $start_time_str"; my $start_time_dateobj = $dateparser->parse_datetime($start_time_str) or die "parsing date '$start_time_str'."; my $jd = $start_time_dateobj->jd; $start_time_dateobj->set_time_zone('America/Los_Angeles'); my $st = $start_time_dateobj->strftime('%Y-%m-%d-%H:%M:%S %Z'); my $hour = $start_time_dateobj->hour_12(); my $which_m = $start_time_dateobj->am_or_pm(); my $abrv = $start_time_dateobj->day_abbr(); # sunny? my $forecast_str = $aforecast->{'shortForecast'}; # temperature as a number, see later for the units my $temp = $aforecast->{'temperature'}; # new scope for hash my %parsed; # store this record/prediction in our %parsed hash # keyed on this: my $key = $start_time_str; $parsed{$key} = { 'human-readable' => "$abrv $hour $which_m", 'date-from-local' => $st, 'number' => $number, 'forecast-string' => $forecast_str, 'julian day' => $jd, # we append temp unit to the key, e.g. 'F' 'forecast-temp-' . $aforecast->{'temperatureUnit'} => $temp, }; my $refparsed = \%parsed; $ordered[$index] = $refparsed; #$logger->info( dumper $refparsed); ++$index; } $logger->info( dumper $refordered); $logger->info("=============="); my $pturl1 = 'https://www.fourmilab.ch/yoursky/'; my $pturl2 = 'http://www.fourmilab.ch/cgi-bin/Yoursky?z=1&$lat&ns=North&lon=$west_long&ew=West'; #my $tx = $ua->post( $pturl2 => form => { }); __END__