Update list-weather.ps1

This commit is contained in:
Markus Fleschutz 2024-05-14 15:31:57 +02:00
parent fa466955a4
commit 6cd2ea934d

View File

@ -2,13 +2,13 @@
.SYNOPSIS .SYNOPSIS
Lists the weather report Lists the weather report
.DESCRIPTION .DESCRIPTION
This PowerShell script lists the hourly weather report in a nice table. This PowerShell script queries the 48h weather report from wttr.in and lists it in a nice table.
.PARAMETER Location .PARAMETER location
Specifies the location to use (determined automatically per default) Specifies the location to use (determined automatically by default)
.EXAMPLE .EXAMPLE
PS> ./list-weather.ps1 PS> ./list-weather.ps1
TODAY 🌡°C mm 💧 💨km/h UV 👁km at Munich (Bayern, Germany) TODAY 🌡°C mm 💧 💨km/h UV 👁km at Munich (Bayern, Germany)
0°° -2° 0.0 93% 6 1 21% 10 🌙 clear 0h 11° 0.0 88% 7 1 8% 10 🌙 clear
... ...
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
@ -16,10 +16,10 @@
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
param([string]$Location = "") # empty means determine automatically param([string]$location = "") # empty means determine automatically
function GetDescription { param([string]$text) function GetDescription([string]$text) {
switch ($text.trim()) { switch ($text) {
"Blizzard" { return "❄️ blizzard ⚠️" } "Blizzard" { return "❄️ blizzard ⚠️" }
"Blowing snow" { return "❄️ blowing snow ⚠️" } "Blowing snow" { return "❄️ blowing snow ⚠️" }
"Clear" { return "🌙 clear" } "Clear" { return "🌙 clear" }
@ -38,6 +38,7 @@ function GetDescription { param([string]$text)
"Moderate or heavy freezing rain"{return "💧 moderate or heavy freezing rain ⚠️" } "Moderate or heavy freezing rain"{return "💧 moderate or heavy freezing rain ⚠️" }
"Moderate or heavy sleet" { return "❄️ moderate or heavy sleet ⚠️" } "Moderate or heavy sleet" { return "❄️ moderate or heavy sleet ⚠️" }
"Moderate or heavy rain shower" { return "💧 moderate or heavy rain shower ⚠️" } "Moderate or heavy rain shower" { return "💧 moderate or heavy rain shower ⚠️" }
"Moderate or heavy rain in area with thunder" { return "💧 moderate or heavy rain in area with thunder ⚠️" }
"Moderate or heavy snow showers"{ return "❄️ moderate or heavy snow showers ⚠️" } "Moderate or heavy snow showers"{ return "❄️ moderate or heavy snow showers ⚠️" }
"Moderate or heavy snow in area with thunder" { return "❄️ moderate or heavy snow in area with thunder ⚠️" } "Moderate or heavy snow in area with thunder" { return "❄️ moderate or heavy snow in area with thunder ⚠️" }
"Moderate rain" { return "💧 moderate rain" } "Moderate rain" { return "💧 moderate rain" }
@ -64,8 +65,8 @@ function GetDescription { param([string]$text)
} }
} }
function GetWindDir { param([string]$Text) function GetWindDir([string]$text) {
switch($Text) { switch($text) {
"NW" { return "" } "NW" { return "" }
"NNW" { return "" } "NNW" { return "" }
"N" { return "" } "N" { return "" }
@ -82,45 +83,46 @@ function GetWindDir { param([string]$Text)
"WSW" { return "" } "WSW" { return "" }
"W" { return "" } "W" { return "" }
"WNW" { return "" } "WNW" { return "" }
default { return "$Text" } default { return "$text" }
} }
} }
try { try {
Write-Progress "Loading weather data from http://wttr.in ..." Write-Progress "Loading weather data from http://wttr.in ..."
$Weather = (Invoke-WebRequest -URI http://wttr.in/${Location}?format=j1 -userAgent "curl" -useBasicParsing).Content | ConvertFrom-Json $weather = (Invoke-WebRequest -URI http://wttr.in/${location}?format=j1 -userAgent "curl" -useBasicParsing).Content | ConvertFrom-Json
Write-Progress -completed "." $area = $weather.nearest_area.areaName.value
$Area = $Weather.nearest_area.areaName.value $region = $weather.nearest_area.region.value
$Region = $Weather.nearest_area.region.value $country = $weather.nearest_area.country.value
$Country = $Weather.nearest_area.country.value Write-Progress -completed "Done."
[int]$Day = 0
foreach($Hourly in $Weather.weather.hourly) { [int]$day = 0
$Hour = $Hourly.time / 100 foreach($hourly in $weather.weather.hourly) {
$Temp = $(($Hourly.tempC.toString()).PadLeft(3)) $hour = $hourly.time / 100
$Precip = $Hourly.precipMM $tempC = $(($hourly.tempC.toString()).PadLeft(3))
$Humidity = $(($Hourly.humidity.toString()).PadLeft(3)) $precip = $hourly.precipMM
$Pressure = $Hourly.pressure $humidity = $(($hourly.humidity.toString()).PadLeft(3))
$WindSpeed = $(($Hourly.windspeedKmph.toString()).PadLeft(2)) $pressure = $hourly.pressure
$WindDir = GetWindDir $Hourly.winddir16Point $windSpeed = $(($hourly.windspeedKmph.toString()).PadLeft(2))
$UV = $Hourly.uvIndex $windDir = GetWindDir $hourly.winddir16Point
$Clouds = $(($Hourly.cloudcover.toString()).PadLeft(3)) $UV = $hourly.uvIndex
$Visib = $(($Hourly.visibility.toString()).PadLeft(2)) $clouds = $(($hourly.cloudcover.toString()).PadLeft(3))
$Desc = GetDescription $Hourly.weatherDesc.value $visib = $(($hourly.visibility.toString()).PadLeft(2))
if ($Hour -eq 0) { $desc = GetDescription $hourly.weatherDesc.value.trim()
if ($Day -eq 0) { if ($hour -eq 0) {
Write-Host -foregroundColor green "TODAY 🌡°C ☂mm 💧 💨km/h ☀UV ☁️ 👁km at $Area ($Region, $Country)" if ($day -eq 0) {
} elseif ($Day -eq 1) { Write-Host "TODAY 🌡°C ☂mm 💧 💨km/h ☀UV ☁️ 👁km at $area ($region, $country)" -foregroundColor green
$Date = (Get-Date).AddDays(1) } elseif ($day -eq 1) {
[string]$Weekday = $Date.DayOfWeek $date = (Get-Date).AddDays(1)
Write-Host -foregroundColor green "$($Weekday.toUpper())" [string]$dayOfWeek = $date.DayOfWeek
Write-Host "$($dayOfWeek.toUpper())" -foregroundColor green
} else { } else {
$Date = (Get-Date).AddDays(2) $date = (Get-Date).AddDays(2)
[string]$Weekday = $Date.DayOfWeek [string]$dayOfWeek = $date.DayOfWeek
Write-Host -foregroundColor green "$($Weekday.toUpper())" Write-Host "$($dayOfWeek.toUpper())" -foregroundColor green
} }
$Day++ $day++
} }
"$(($Hour.toString()).PadLeft(2))h $Temp° $Precip $Humidity% $($WindDir)$WindSpeed $UV $Clouds% $Visib $Desc" "$(($hour.toString()).PadLeft(2))h $tempC° $precip $humidity% $($windDir)$windSpeed $UV $clouds% $visib $desc"
} }
exit 0 # success exit 0 # success
} catch { } catch {