PowerShell/Scripts/list-weather.ps1

65 lines
2.1 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-10-04 21:29:23 +02:00
Lists the hourly weather report
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2022-01-29 12:47:46 +01:00
This PowerShell script lists the hourly weather report.
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
2021-09-24 17:19:49 +02:00
PS> ./list-weather
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-05 14:50:19 +01:00
function Describe { param([string]$Desc)
switch($Desc) {
"Clear" { return "clear " }
"Cloudy" { return "cloudy " }
"Light drizzle" { return "💧light drizzle" }
"Light rain shower" { return "💧light rain " }
"Mist" { return "🌫 misty " }
"Partly cloudy" { return "bit cloudy " }
"Sunny" { return "sunny " }
default { return "$Desc" }
}
}
2021-04-19 11:12:21 +02:00
try {
2022-11-05 12:13:46 +01:00
$Weather = (Invoke-WebRequest -URI http://wttr.in/${Location}?format=j1 -userAgent "curl" -useBasicParsing).Content | ConvertFrom-Json
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
2021-04-19 11:12:21 +02:00
$Temp = $Hourly.tempC
$Precip = $Hourly.precipMM
$Humidity = $Hourly.humidity
$Pressure = $Hourly.pressure
$WindSpeed = $Hourly.windspeedKmph
$WindDir = $Hourly.winddir16Point
$UV = $Hourly.uvIndex
$Clouds = $Hourly.cloudcover
$Desc = $Hourly.weatherDesc.value
2021-04-20 16:38:04 +02:00
if ($Hour -eq 0) {
if ($Day -eq 0) {
2022-11-05 14:50:19 +01:00
Write-Host -foregroundColor green "Today 🌡°C ☂mm 💧 💨km/h from ☀UV ☁️ at $Area ($Region, $Country)"
2021-04-20 16:38:04 +02:00
} elseif ($Day -eq 1) {
2022-11-05 14:50:19 +01:00
Write-Host -foregroundColor green "Tomorrow"
2021-04-20 16:38:04 +02:00
} else {
2022-11-05 14:50:19 +01:00
Write-Host -foregroundColor green "Day after tomorrow"
2021-04-20 16:38:04 +02:00
}
$Day++
}
2022-11-05 14:50:19 +01:00
"$(($Hour.toString()).PadLeft(2))°° $(($Temp.toString()).PadLeft(2))° $($Precip) $(($Humidity.toString()).PadLeft(3))% $(($WindSpeed.toString()).PadLeft(2)) $WindDir`t$($UV) $(($Clouds.toString()).PadLeft(3))% $(Describe $Desc)"
2021-04-19 11:12:21 +02:00
$Hour++
}
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
2022-11-05 14:50:19 +01:00
}