in reply to Re: Regex to Array lookup question
in thread Regex to Array lookup question

I am having a hard time understanding the problem. You write, "no established list of all the possibilities".

We didn't hear back from OP, but we can investigate the problem it suggests in our minds.

I looked at the JSON output of https://api.weather.gov/icons. That looks like the possibilities to me. I decoded the JSON and simplified that into a more straightforward translation table. I do not show the LWP code, but I guess you know how to do that.

Marshall's post broke this open for me in a way that I wanted to pursue in first the LWP direction and then with some means to see these things that we're talking about. I'm taking WWW::Mechanize::Chrome through its paces. I ended up actually being able to see these things: 5 WMC screenshots. This gets a bit verbose, so I'll use readmore tags:

Output first:

$ ./1.icons.pl { "\@context" => [], "icons" => { bkn => { description => "Mostly cloudy" }, blizzard => { description => "Blizzard" }, cold => { description => "Cold" }, dust => { description => "Dust" }, few => { description => "A few clouds" }, fog => { description => "Fog/mist" }, fzra => { description => "Freezing rain" }, haze => { description => "Haze" }, hot => { description => "Hot" }, hurricane => { description => "Hurricane conditions" }, ovc => { description => "Overcast" }, rain => { description => "Rain" }, rain_fzra => { description => "Rain/freezing rain" }, rain_showers => { description => "Rain showers (high cloud cove +r)" }, rain_showers_hi => { description => "Rain showers (low cloud cover +)" }, rain_sleet => { description => "Rain/sleet" }, rain_snow => { description => "Rain/snow" }, sct => { description => "Partly cloudy" }, skc => { description => "Fair/clear" }, sleet => { description => "Sleet" }, smoke => { description => "Smoke" }, snow => { description => "Snow" }, snow_fzra => { description => "Freezing rain/snow" }, snow_sleet => { description => "Rain/sleet" }, tornado => { description => "Tornado" }, tropical_storm => { description => "Tropical storm conditions" }, tsra => { description => "Thunderstorm (high cloud cove +r)" }, tsra_hi => { description => "Thunderstorm (low cloud cover +)" }, tsra_sct => { description => "Thunderstorm (medium cloud co +ver)" }, wind_bkn => { description => "Mostly cloudy and windy" }, wind_few => { description => "A few clouds and windy" }, wind_ovc => { description => "Overcast and windy" }, wind_sct => { description => "Partly cloudy and windy" }, wind_skc => { description => "Fair/clear and windy" }, }, } URL = https://api.weather.gov/icons/land/day/tsra_sct,20/tsra_sct,40?s +ize=medium tsra_sct => 'Thunderstorm (medium cloud cover)' URL = https://api.weather.gov/icons/land/day/rain_showers,30/tsra_hi,3 +0?size=medium tsra_hi => 'Thunderstorm (low cloud cover)' URL = https://api.weather.gov/icons/land/night/rain_showers,30/rain_sh +owers?size=medium rain_showers => 'Rain showers (high cloud cover)' URL = https://api.weather.gov/icons/land/day/bkn?size=medium bkn => 'Mostly cloudy' $

The following is essentially a hybrid of Marshall's script with the sausages that come from curl2lwp converter. Source:

#!/usr/bin/perl -w use strict; use LWP::UserAgent; use Data::Dump; use JSON::Parse 'parse_json'; my $ua = LWP::UserAgent->new( 'send_te' => '0' ); my $r = HTTP::Request->new( 'GET' => 'https://api.weather.gov/icons', [ 'Cache-Control' => 'max-age=0', 'Connection' => 'keep-alive', 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*; +q=0.8', 'Accept-Encoding' => 'gzip, x-gzip, deflate, x-bzip2, bzip2', 'Accept-Language' => 'en-US,en;q=0.5', 'Host' => 'api.weather.gov:443', 'User-Agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:74.0) Gecko/20100101 Firef +ox/74.0', 'Upgrade-Insecure-Requests' => '1', ], ); my $out; my $res = $ua->request( $r, ); if ( $res->is_success ) { my $json = $res->decoded_content; $out = parse_json $json; } else { print "Error: " . $res->status_line . "\n"; } dd $out; my %xlated_abbrev; #simple abbreviation table => description foreach my $key ( keys %{ $out->{icons} } ) #gen simple xlate table { $xlated_abbrev{$key} = $out->{icons}{$key}{description}; } my @urls = ( 'https://api.weather.gov/icons/land/day/tsra_sct,20/tsra_sct,40?size +=medium', 'https://api.weather.gov/icons/land/day/rain_showers,30/tsra_hi,30?s +ize=medium', 'https://api.weather.gov/icons/land/night/rain_showers,30/rain_showe +rs?size=medium', 'https://api.weather.gov/icons/land/day/bkn?size=medium' ); foreach my $url (@urls) { my $last_path = ( split( '/', $url ) )[-1]; my ($abbrev_to_xlate) = $last_path =~ /^(\w+)/; print "URL = $url\n"; print " $abbrev_to_xlate => \'$xlated_abbrev{$abbrev_to_xlate}\'\n +\n"; } __END__ Created from curl command line curl 'https://api.weather.gov/icons' -H 'User-Agent: Mozilla/5.0 (X11; + Ubuntu; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0' -H 'Acce +pt: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp, +*/*;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Conn +ection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'Cache-Contr +ol: max-age=0'

The script I used to make png's of the icons follows:

#!/usr/bin/perl use strict; use warnings; use Log::Log4perl qw(:easy); use WWW::Mechanize::Chrome; my $a = 'b'; my @urls = ( 'https://api.weather.gov/icons/land/day/tsra_sct,20/tsra_sct,40?size +=medium', 'https://api.weather.gov/icons/land/day/rain_showers,30/tsra_hi,30?s +ize=medium', 'https://api.weather.gov/icons/land/night/rain_showers,30/rain_showe +rs?size=medium', 'https://api.weather.gov/icons/land/day/bkn?size=medium' ); foreach my $url (@urls) { show_screen( $a, $url ); $a++; } sub show_screen() { my ( $letter, $url ) = @_; my $mech = WWW::Mechanize::Chrome->new(); $mech->get($url); my $page_png = $mech->content_as_png(); my $base = '/home/hogan/5.scripts/1.corion./template_stuff/aimag +es'; my $fn = $base . "/$letter.png"; open my $fh, '>', $fn or die "Couldn't create '$fn': $!"; binmode $fh, ':raw'; print $fh $page_png; close $fh; print "exiting show_screen with letter $letter\n"; } sleep 1;

I didn't really understand any of this until I could see what the icons actually look like:

tsra_sct,20/tsra_sct,40

For example, on the left side of this icon, chance of rain is 20%, and on the right, 40%.

Anyways, I find using perl to access these APIs very interesting.

Update: Cropped screenshots and typo fixed here.