From a9e23843cedb0bb26be6bb15c6ad8ce2d1078f21 Mon Sep 17 00:00:00 2001 From: bluesaxman Date: Fri, 10 Feb 2023 10:54:10 -0700 Subject: [PATCH] Added localized config file with default fallbacks, fixed a few issue, replaced wb api key --- location_example.json | 8 ++++++++ weather.sh | 19 ++++++++++++++++++- weatherbug.sh | 8 ++++---- weathergov.sh | 4 ++-- weatherunder.sh | 6 +++--- wttrin.sh | 6 ++++-- 6 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 location_example.json diff --git a/location_example.json b/location_example.json new file mode 100644 index 0000000..c5a872a --- /dev/null +++ b/location_example.json @@ -0,0 +1,8 @@ +{ +"lat":"40.49967", +"lon":"-111.9388", +"WBproviderID":"23", +"WBstationID":"C3430", +"WUstationID":"KUTRIVER63", +"city":"bluffdale" +} diff --git a/weather.sh b/weather.sh index 90313de..6ee8c27 100755 --- a/weather.sh +++ b/weather.sh @@ -2,13 +2,30 @@ use strict; use warnings; use Math::Trig; +use JSON; my @path = split("/", $0); pop @path; my $p = join("/", @path); +my $config_file = -e '~/.location.json' ? '~/.location.json' : 'location_example.json'; + +# Load up config data +open (CONFIG, $config_file) or goto NOCONFIG; +my $raw_config = ""; +while () { $raw_config .= $_; } +my %config = %{decode_json($raw_config)}; +close(CONFIG); + +NOCONFIG: +my $lat = $config{lat} or scalar(40.48967); +my $lon = $config{lon} or scalar(-111.93882); +my $WB_providerID = $config{WBproviderID} or scalar(23); +my $WB_stationID = $config{WBstationID} or scalar("C3430"); +my $WU_stationID = $config{WUstationID} or scalar("KUTRIVER63"); +my $city = $config{city} or scalar("bluffdale"); sub checksources { - my $weather = (`$p/wttrin.sh` || `$p/weatherunder.sh` || `$p/weatherbug.sh` || `$p/weathergov.sh`) or print "Not Available\n\n#FF0000" and exit 33; + my $weather = (`$p/wttrin.sh $city` || `$p/weatherunder.sh $WU_stationID` || `$p/weatherbug.sh $lat $lon $WB_providerID $WB_stationID` || `$p/weathergov.sh $lat $lon`) or print "Not Available\n\n#FF0000" and exit 33; return $weather; } diff --git a/weatherbug.sh b/weatherbug.sh index 1876800..537f08f 100755 --- a/weatherbug.sh +++ b/weatherbug.sh @@ -4,10 +4,10 @@ use warnings; use JSON; #Have to open the web page and look in developer mode to get this info -my $URL_lat = 40.48967; -my $URL_lng = -111.93882; -my $URL_ProID = 23; -my $URL_StatID = "C3430"; +my $URL_lat = $ARGV[0] or scalar(40.48967); +my $URL_lng = $ARGV[1] or scalar(-111.93882); +my $URL_ProID = $ARGV[2] or scalar( 23); +my $URL_StatID = $ARGV[3] or scalar("C3430"); my $info = `curl -sm 5 "https://www.weatherbug.com/api/observation?lat=$URL_lat&lng=$URL_lng&providerId=$URL_ProID&stationId=$URL_StatID"` or die "Error"; diff --git a/weathergov.sh b/weathergov.sh index a4411bb..0dd8368 100755 --- a/weathergov.sh +++ b/weathergov.sh @@ -3,8 +3,8 @@ use strict; use warnings; use JSON; -my $URL_lat = 40.4936; -my $URL_lon = -111.9388; +my $URL_lat = $ARGV[0] or scalar(40.4936); +my $URL_lon = $ARGV[1] or scalar(-111.9388); my $info = `curl -sm 5 "https://forecast.weather.gov/MapClick.php?lat=$URL_lat&lon=$URL_lon&unit=0&lg=english&FcstType=json"` or die "Error"; diff --git a/weatherunder.sh b/weatherunder.sh index 0d01d6e..6530bfe 100755 --- a/weatherunder.sh +++ b/weatherunder.sh @@ -4,11 +4,11 @@ use strict; use warnings; use JSON; -my $APIKEY = "6532d6454b8aa370768e63d6ba5a832e"; -my $StationID = "KUTRIVER63"; +my $APIKEY = "e1f10a1e78da46f5b10a1e78da96f525"; +my $StationID = $ARGV[0] or scalar("KUTRIVER63"); my $Units = "m"; # e = imperial, m = metric -my $info = `curl -sm "https://api.weather.com/v2/pws/observations/current?apiKey=$APIKEY&stationId=$StationID&numericPrecision=decimal&format=json&units=$Units"` or die "Error"; +my $info = `curl -sm 1 "https://api.weather.com/v2/pws/observations/current?apiKey=$APIKEY&stationId=$StationID&numericPrecision=decimal&format=json&units=$Units"` or die "Error"; my $json = decode_json $info or die "Error"; diff --git a/wttrin.sh b/wttrin.sh index eab6532..7a53ba4 100755 --- a/wttrin.sh +++ b/wttrin.sh @@ -3,11 +3,13 @@ use strict; use warnings; use JSON; -my $info = `curl -sm 5 wttr.in/bluffdale?format=j1` or die "Error"; +my $city = $ARGV[0] or scalar("bluffdale"); + +my $info = `curl -sm 5 wttr.in/$city?format=j1` or die "Error"; my $json = decode_json $info or die "Error"; -my $loc = %{$json}{"nearest_area"}->[0]->{"areaName"}->{"value"}; +my $loc = %{$json}{"nearest_area"}->[0]->{"areaName"}->[0]->{"value"}; my $temp = %{$json}{"current_condition"}->[0]->{"temp_F"}; my $windS = %{$json}{"current_condition"}->[0]->{"windspeedMiles"}; my $windD = %{$json}{"current_condition"}->[0]->{"winddirDegree"};