PowerShell/scripts/list-weather.ps1

124 lines
4.7 KiB
PowerShell
Raw Normal View History

2023-10-31 11:25:11 +01:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2022-11-10 13:07:47 +01:00
Lists the weather report
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2022-11-25 15:04:10 +01:00
This PowerShell script lists the hourly weather report in a nice table.
2021-10-16 13:40:20 +02:00
.PARAMETER Location
Specifies the location to use (determined automatically per default)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./list-weather.ps1
2022-12-05 08:19:26 +01:00
TODAY 🌡°C mm 💧 💨km/h UV 👁km at Munich (Bayern, Germany)
0°° -2° 0.0 93% 6 1 21% 10 🌙 clear
...
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-04-19 11:12:21 +02:00
#>
2021-07-15 15:51:22 +02:00
param([string]$Location = "") # empty means determine automatically
2021-04-19 11:12:21 +02:00
2022-11-25 15:04:10 +01:00
function GetDescription { param([string]$Text)
switch($Text) {
2022-11-26 12:04:48 +01:00
"Blizzard" { return "❄️ blizzard ⚠️" }
2023-01-31 13:31:05 +01:00
"Blowing snow" { return "❄️ blowing snow ⚠️" }
2022-11-26 12:04:48 +01:00
"Clear" { return "🌙 clear" }
"Cloudy" { return "☁️ cloudy" }
"Fog" { return "🌫 fog" }
2022-12-01 21:02:40 +01:00
"Freezing fog" { return "🌫 freezing fog" }
2022-11-26 12:04:48 +01:00
"Heavy snow" { return "❄️ heavy snow ⚠️" }
2022-11-17 10:45:37 +01:00
"Light drizzle" { return "💧 light drizzle" }
2022-12-14 12:16:36 +01:00
"Light freezing rain" { return "💧 light freezing rain ⚠️" }
2022-11-26 12:04:48 +01:00
"Light rain" { return "💧 light rain" }
2022-11-18 09:56:33 +01:00
"Light rain shower" { return "💧 light rain shower" }
2022-11-21 09:45:09 +01:00
"Light sleet" { return "❄️ light sleet" }
2022-12-26 22:48:51 +01:00
"Light sleet showers" { return "❄️ light sleet showers" }
2022-11-18 17:34:55 +01:00
"Light snow" { return "❄️ light snow" }
2022-11-28 08:31:53 +01:00
"Light snow showers" { return "❄️ light snow showers" }
2023-08-14 16:48:45 +02:00
"Moderate or heavy freezing rain"{return "💧 moderate or heavy freezing rain ⚠️" }
"Moderate or heavy rain shower" { return "💧 moderate or heavy rain shower ⚠️" }
2022-12-05 08:19:26 +01:00
"Moderate or heavy snow showers"{ return "❄️ moderate or heavy snow showers ⚠️" }
2022-12-14 12:16:36 +01:00
"Moderate rain" { return "💧 moderate rain" }
2023-07-26 16:00:03 +02:00
"Moderate rain at times" { return "💧 moderate rain at times" }
2022-11-18 17:34:55 +01:00
"Moderate snow" { return "❄️ moderate snow" }
2022-11-26 12:04:48 +01:00
"Mist" { return "🌫 misty" }
"Overcast" { return "☁️ overcast" }
2022-11-17 10:45:37 +01:00
"Partly cloudy" { return "partly cloudy" }
2022-12-14 12:16:36 +01:00
"Patchy heavy snow" { return "❄️ patchy heavy snow ⚠️" }
2022-12-12 08:57:22 +01:00
"Patchy light drizzle" { return "💧 patchy light drizzle" }
2022-11-17 10:45:37 +01:00
"Patchy light rain" { return "💧 patchy light rain" }
2023-04-27 17:55:05 +02:00
"Patchy light rain with thunder" { return "💧 patchy light rain with thunder" }
2022-12-07 08:09:56 +01:00
"Patchy light snow" { return "❄️ patchy light snow" }
2023-01-23 09:33:52 +01:00
"Patchy moderate snow" { return "❄️ patchy moderate snow" }
2022-11-17 10:45:37 +01:00
"Patchy rain possible" { return "💧 patchy rain possible" }
2023-01-31 13:31:05 +01:00
"Patchy snow possible" { return "❄️ patchy snow possible" }
2022-11-26 12:04:48 +01:00
"Sunny" { return "☀️ sunny" }
2022-11-17 10:45:37 +01:00
"Thundery outbreaks possible" { return "thundery outbreaks possible" }
2022-11-25 15:04:10 +01:00
default { return "$Text" }
}
}
function GetWindDir { param([string]$Text)
switch($Text) {
2022-11-26 12:04:48 +01:00
"NW" { return "" }
2022-11-25 21:02:41 +01:00
"NNW" { return "" }
2022-11-25 15:04:10 +01:00
"N" { return "" }
2022-11-25 21:02:41 +01:00
"NNE" { return "" }
"NE" { return "" }
"ENE" { return "" }
2022-11-25 15:04:10 +01:00
"E" { return "" }
2022-11-26 12:04:48 +01:00
"ESE" { return "" }
2022-11-25 15:04:10 +01:00
"SE" { return "" }
2022-11-25 21:02:41 +01:00
"SSE" { return "" }
2022-11-25 15:04:10 +01:00
"S" { return "" }
2022-11-25 21:02:41 +01:00
"SSW" { return "" }
2022-11-25 15:04:10 +01:00
"SW" { return "" }
2022-11-25 21:02:41 +01:00
"WSW" { return "" }
2022-11-25 15:04:10 +01:00
"W" { return "" }
2022-11-25 21:02:41 +01:00
"WNW" { return "" }
default { return "$Text" }
2022-11-05 14:50:19 +01:00
}
}
2021-04-19 11:12:21 +02:00
try {
Write-Progress "Loading weather data from http://wttr.in ..."
2022-11-05 12:13:46 +01:00
$Weather = (Invoke-WebRequest -URI http://wttr.in/${Location}?format=j1 -userAgent "curl" -useBasicParsing).Content | ConvertFrom-Json
2023-05-04 13:54:05 +02:00
Write-Progress -completed "."
2021-04-19 11:12:21 +02:00
$Area = $Weather.nearest_area.areaName.value
$Region = $Weather.nearest_area.region.value
2021-04-20 16:38:04 +02:00
$Country = $Weather.nearest_area.country.value
[int]$Day = 0
2022-11-05 14:50:19 +01:00
foreach($Hourly in $Weather.weather.hourly) {
2021-04-20 16:38:04 +02:00
$Hour = $Hourly.time / 100
2022-12-02 14:55:07 +01:00
$Temp = $(($Hourly.tempC.toString()).PadLeft(3))
2021-04-19 11:12:21 +02:00
$Precip = $Hourly.precipMM
2022-11-26 15:11:17 +01:00
$Humidity = $(($Hourly.humidity.toString()).PadLeft(3))
2021-04-19 11:12:21 +02:00
$Pressure = $Hourly.pressure
2022-11-26 12:04:48 +01:00
$WindSpeed = $(($Hourly.windspeedKmph.toString()).PadLeft(2))
2022-11-25 15:04:10 +01:00
$WindDir = GetWindDir $Hourly.winddir16Point
2021-04-19 11:12:21 +02:00
$UV = $Hourly.uvIndex
2022-11-26 15:11:17 +01:00
$Clouds = $(($Hourly.cloudcover.toString()).PadLeft(3))
2022-11-26 12:04:48 +01:00
$Visib = $(($Hourly.visibility.toString()).PadLeft(2))
2022-11-25 15:04:10 +01:00
$Desc = GetDescription $Hourly.weatherDesc.value
2021-04-20 16:38:04 +02:00
if ($Hour -eq 0) {
if ($Day -eq 0) {
2022-12-19 08:29:02 +01:00
Write-Host -foregroundColor green "TODAY 🌡°C ☂mm 💧 💨km/h ☀UV ☁️ 👁km at $Area ($Region, $Country)"
2021-04-20 16:38:04 +02:00
} elseif ($Day -eq 1) {
$Date = (Get-Date).AddDays(1)
[string]$Weekday = $Date.DayOfWeek
Write-Host -foregroundColor green "$($Weekday.toUpper())"
2021-04-20 16:38:04 +02:00
} else {
$Date = (Get-Date).AddDays(2)
[string]$Weekday = $Date.DayOfWeek
Write-Host -foregroundColor green "$($Weekday.toUpper())"
2021-04-20 16:38:04 +02:00
}
$Day++
}
2022-12-19 08:29:02 +01:00
"$(($Hour.toString()).PadLeft(2))°° $Temp° $Precip $Humidity% $($WindDir)$WindSpeed $UV $Clouds% $Visib $Desc"
2021-04-19 11:12:21 +02:00
}
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-04-19 11:12:21 +02:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-04-19 11:12:21 +02:00
exit 1
2023-08-06 21:35:36 +02:00
}