From 9160e701b801d79c2f750a2dd23710c46ab28649 Mon Sep 17 00:00:00 2001 From: bluesaxman Date: Tue, 21 Jan 2020 14:01:04 -0700 Subject: [PATCH] initial commit for weatherget --- weather.sh | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ weatherbug.sh | 20 +++++++++++ weathergov.sh | 18 ++++++++++ wttrin.sh | 15 +++++++++ 4 files changed, 144 insertions(+) create mode 100755 weather.sh create mode 100755 weatherbug.sh create mode 100755 weathergov.sh create mode 100755 wttrin.sh diff --git a/weather.sh b/weather.sh new file mode 100755 index 0000000..8350d8c --- /dev/null +++ b/weather.sh @@ -0,0 +1,91 @@ +#!/usr/bin/perl -w +use strict; +use warnings; +use Math::Trig; + +sub checksources { + return `./wttrin.sh` or `./weatherbug.sh` or `./weathergov.sh` or print "Not Available\n\n#FF0000" and exit 33; +} + +sub wavegen { + my $x = shift; + my $period = shift; + my $phaseshift = shift; + my $magnitude = shift; + my $y_adjust = shift; + return sin($x/($period/pi)+$phaseshift)*$magnitude+$y_adjust; +} + +sub hexcolor { + my ($temp,$cold,$hot,$rwave,$gwave,$bwave) = @_; + my $temp_gauge = ($temp - $cold)/($hot - $cold)*100; + + my $r = (100 * sin($temp_gauge / 10)) + ($temp_gauge - 30) * 2; + my $g = ($temp_gauge - 50) * ($temp_gauge - 75) * (0 - 1) / 10 + 100; + my $b = ($temp_gauge - 25) * ($temp_gauge - 25) * (0 - 1) / 5 + 100; + + $r *= 2; + $g *= 2; + $b *= 2; + + #brightness + my $brightness = 270; #out of a max of 768 (white) + + #sanitize + $r = $r > 0 ? $r : 0; + $g = $g > 0 ? $g : 0; + $b = $b > 0 ? $b : 0; + $r = $r < 255 ? $r : 255; + $g = $g < 255 ? $g : 255; + $b = $b < 255 ? $b : 255; + + if ($r+$g+$b < $brightness) { + $brightness -= ($r+$g+$b); + $r += ($brightness/3); + $g += ($brightness/3); + $b += ($brightness/3); + } + + if ($rwave) { $r = $rwave } + if ($gwave) { $g = $gwave } + if ($bwave) { $b = $bwave } + + my $CR = unpack("H2",pack("I",$r)); + my $CG = unpack("H2",pack("I",$g)); + my $CB = unpack("H2",pack("I",$b)); + + return "#".$CR.$CG.$CB; +} + +sub getdir { + my $degree = shift; + if ($degree <= 22 ) { return "↑";} + if ($degree <= 67 ) { return "↗";} + if ($degree <= 112 ) { return "→";} + if ($degree <= 157 ) { return "↘";} + if ($degree <= 202 ) { return "↓";} + if ($degree <= 247 ) { return "↙";} + if ($degree <= 292 ) { return "←";} + if ($degree <= 337 ) { return "↖";} + return "↑"; +} + +my @weather = split(":",checksources) or print "Not Available\n\n#FF0000" and exit 33; + +my $type = $weather[0]; +my $temp = sprintf("%.0f", ($weather[1] - 32) * (5/9) ); #converts from F to C +my $wind = $weather[2]; +my $wdir = getdir($weather[3]); + +my $color = hexcolor($temp,-17,42); + +if($type =~ /Rain/) {$type = "";} +elsif ($type =~ /Overcast/) {$type = "";} +elsif ($type =~ /cloud/) {$type = "";} +elsif ($type =~ /Clear/) {$type = "";} +elsif ($type =~ /Sun/) {$type = "";} +elsif ($type =~ /Snow/) {$type = "";} +else {$type=$type."...";} + +print $type."  ".$temp."  ".$wdir." ".$wind."mph\n\n"; +print $color."\n"; diff --git a/weatherbug.sh b/weatherbug.sh new file mode 100755 index 0000000..846e596 --- /dev/null +++ b/weatherbug.sh @@ -0,0 +1,20 @@ +#! /usr/bin/perl -w +use strict; +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 $info = `curl https://www.weatherbug.com/api/observation?lat=$URL_lat&lng=$URL_lng&providerId=$URL_ProID&stationId=$URL_StatID` or die "Error"; + +my $json = decode_json $info or die "Error"; + +my $temp = %{$json}{"Observation"}->{"Temperature"}; +my $windS = %{$json}{"Observation"}->{"WindSpeedAvg"}; +my $windD = %{$json}{"Observation"}->{"WindDirectionAvg"}; + +print "Unknown:".$temp.":".$windS.":".$windD; diff --git a/weathergov.sh b/weathergov.sh new file mode 100755 index 0000000..e046969 --- /dev/null +++ b/weathergov.sh @@ -0,0 +1,18 @@ +#!/usr/bin/perl -w +use strict; +use warnings; +use JSON; + +my $URL_lat = 40.4936; +my $URL_lon = -111.9388; + +my $info = `curl "https://forecast.weather.gov/MapClick.php?lat=$URL_lat&lon=$URL_lon&unit=0&lg=english&FcstType=json"` or die "Error"; + +my $json = decode_json $info or die "Error"; + +my $temp = %{$json}{"currentobservation"}->{"Temp"}; +my $windS = %{$json}{"currentobservation"}->{"Winds"}; +my $windD = %{$json}{"currentobservation"}->{"Windd"}; +my $desc = %{$json}{"currentobservation"}->{"Weather"}; + +print $desc.":".$temp.":".$windS.":".$windD; diff --git a/wttrin.sh b/wttrin.sh new file mode 100755 index 0000000..fe8a1ab --- /dev/null +++ b/wttrin.sh @@ -0,0 +1,15 @@ +#! /usr/bin/perl -w +use strict; +use warnings; +use JSON; + +my $info = `curl wttr.in/bluffdale?format=j1` or die "Error"; + +my $json = decode_json $info or die "Error"; + +my $temp = %{$json}{"current_condition"}->[0]->{"temp_F"}; +my $windS = %{$json}{"current_condition"}->[0]->{"windspeedMiles"}; +my $windD = %{$json}{"current_condition"}->[0]->{"winddirDegree"}; +my $desc = %{$json}{"current_condition"}->[0]->{"weatherDesc"}->[0]->{"value"}; + +print $desc.":".$temp.":".$windS.":".$windD;