http://www.perlmonks.org?node_id=11116731


in reply to polishing up a json fetching script for weather data

As requested, a fairly rough and ready proof of concept using Mojo:

#!/usr/bin/perl use strict; use warnings; use feature 'say'; use Mojo::UserAgent; use DateTime::Format::Strptime; use open ':std', OUT => ':utf8'; my $url = 'https://api.weather.gov/stations/KPDX/observations/latest'; # the API docs says you must identify yourself, please make this somet +hing legit my $name = '(example.com, contact@example.com)'; my $ua = Mojo::UserAgent->new; $ua->transactor->name( $name ); # get JSON response my $json = $ua->get( $url )->res->json->{properties}; # for each cloud.... foreach my $cloud ( @{$json->{cloudLayers}} ){ say "$cloud->{amount}, $cloud->{base}{value}"; } # JD from JSON timestamp my $format = DateTime::Format::Strptime->new( pattern => '%FT%T%z'); my $dt = $format->parse_datetime( $json->{timestamp} ); say 'Julian Day: ' . $dt->jd; my $pturl = 'http://www.fourmilab.ch/cgi-bin/Yoursky?z=1&lat=45.5183& +ns=North&lon=122.676&ew=West'; # you wanted Julian date so it looks like date should be '2' from the +source. my $tx = $ua->post( $pturl => form => { utc => $dt->jd, date => '2' } +); my $sunrow = $tx->res->dom->at('center:nth-of-type(3) table tr:nth-of- +type(3)'); # output say 'Name:' . $sunrow->children->[0]->all_text; say 'Altitude: ' . $sunrow->children->[4]->text; say 'Azimuth: ' . $sunrow->children->[5]->text; say 'Visible: ' . $sunrow->children->[6]->text;

Output:

FEW, 760 SCT, 3350 BKN, 7620 Julian Day: 2458982.28680556 Name: Sun Altitude: 61.520 Azimuth: 21.289 Visible: Up

There may be some typos in there, chalk that one up to the time of day here. Let me know if there are any problems.

Update: for the 'Name' switch to all_text from text.