I am having a hard time understanding the problem. You write, "
no established list of all the possibilities". 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.
In general for things like this, I have found that using the textual description on the website is better than "rolling your own". If say, you need a different text for tsra_sct than the website provides, perhaps you want "tsra_sct" and "tsra_hi" to translate into the same thing? Then we are into a different discussion about how to maintain such a thing. That is a significant ongoing hassle that I don't recommend.
This gets the textual defs of each abbreviation from the website and translates the first "word" argument of the last path in the URL to that textual definition. I translated each of the URL's you provided. Please explain what else you need...
#!/usr/bin/perl
use strict;
use warnings;
use JSON::Parse 'parse_json';
my $json = do{local $/ = undef;<DATA>};
my $out = parse_json $json;
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=m
+edium',
'https://api.weather.gov/icons/land/day/rain_showers,30/tsra_hi,30?siz
+e=medium',
'https://api.weather.gov/icons/land/night/rain_showers,30/rain_showers
+?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";
}
=PRINTS:
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'
=cut
#Data returned from: https://api.weather.gov/icons
__DATA__
{
"@context": [],
"icons": {
"skc": {
"description": "Fair/clear"
},
"few": {
"description": "A few clouds"
},
"sct": {
"description": "Partly cloudy"
},
"bkn": {
"description": "Mostly cloudy"
},
"ovc": {
"description": "Overcast"
},
"wind_skc": {
"description": "Fair/clear and windy"
},
"wind_few": {
"description": "A few clouds and windy"
},
"wind_sct": {
"description": "Partly cloudy and windy"
},
"wind_bkn": {
"description": "Mostly cloudy and windy"
},
"wind_ovc": {
"description": "Overcast and windy"
},
"snow": {
"description": "Snow"
},
"rain_snow": {
"description": "Rain/snow"
},
"rain_sleet": {
"description": "Rain/sleet"
},
"snow_sleet": {
"description": "Rain/sleet"
},
"fzra": {
"description": "Freezing rain"
},
"rain_fzra": {
"description": "Rain/freezing rain"
},
"snow_fzra": {
"description": "Freezing rain/snow"
},
"sleet": {
"description": "Sleet"
},
"rain": {
"description": "Rain"
},
"rain_showers": {
"description": "Rain showers (high cloud cover)"
},
"rain_showers_hi": {
"description": "Rain showers (low cloud cover)"
},
"tsra": {
"description": "Thunderstorm (high cloud cover)"
},
"tsra_sct": {
"description": "Thunderstorm (medium cloud cover)"
},
"tsra_hi": {
"description": "Thunderstorm (low cloud cover)"
},
"tornado": {
"description": "Tornado"
},
"hurricane": {
"description": "Hurricane conditions"
},
"tropical_storm": {
"description": "Tropical storm conditions"
},
"dust": {
"description": "Dust"
},
"smoke": {
"description": "Smoke"
},
"haze": {
"description": "Haze"
},
"hot": {
"description": "Hot"
},
"cold": {
"description": "Cold"
},
"blizzard": {
"description": "Blizzard"
},
"fog": {
"description": "Fog/mist"
}
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.