PowerShell/Docs/list-weather.md

164 lines
5.7 KiB
Markdown
Raw Normal View History

2022-12-04 10:40:18 +01:00
## The *list-weather.ps1* Script
2021-11-08 21:36:42 +01:00
2022-12-04 10:40:18 +01:00
This PowerShell script lists the hourly weather report in a nice table.
2021-11-08 21:36:42 +01:00
## Parameters
```powershell
2023-05-26 12:20:18 +02:00
/home/mf/Repos/PowerShell/Scripts/list-weather.ps1 [[-Location] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
-Location <String>
Specifies the location to use (determined automatically per default)
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
## Example
```powershell
PS> ./list-weather
2023-05-26 12:20:18 +02:00
TODAY 🌡°C ☂mm 💧 💨km/h ☀UV ☁️ 👁km at Munich (Bayern, Germany)
0°° -2° 0.0 93% ↗ 6 1 21% 10 🌙 clear
...
2021-11-08 21:36:42 +01:00
```
## Notes
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
## Related Links
https://github.com/fleschutz/PowerShell
2022-11-17 20:02:26 +01:00
## Source Code
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
Lists the weather report
.DESCRIPTION
2022-12-04 10:40:18 +01:00
This PowerShell script lists the hourly weather report in a nice table.
2022-11-17 20:02:26 +01:00
.PARAMETER Location
Specifies the location to use (determined automatically per default)
.EXAMPLE
PS> ./list-weather
2023-05-26 12:20:18 +02:00
TODAY 🌡°C ☂mm 💧 💨km/h ☀UV ☁️ 👁km at Munich (Bayern, Germany)
0°° -2° 0.0 93% ↗ 6 1 21% 10 🌙 clear
...
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$Location = "") # empty means determine automatically
2022-12-04 10:40:18 +01:00
function GetDescription { param([string]$Text)
switch($Text) {
"Blizzard" { return "❄️ blizzard ⚠️" }
2023-05-26 12:20:18 +02:00
"Blowing snow" { return "❄️ blowing snow ⚠️" }
2022-12-04 10:40:18 +01:00
"Clear" { return "🌙 clear" }
"Cloudy" { return "☁️ cloudy" }
"Fog" { return "🌫 fog" }
"Freezing fog" { return "🌫 freezing fog" }
"Heavy snow" { return "❄️ heavy snow ⚠️" }
2022-11-17 20:02:26 +01:00
"Light drizzle" { return "💧 light drizzle" }
2023-05-26 12:20:18 +02:00
"Light freezing rain" { return "💧 light freezing rain ⚠️" }
2022-12-04 10:40:18 +01:00
"Light rain" { return "💧 light rain" }
2022-11-18 17:02:20 +01:00
"Light rain shower" { return "💧 light rain shower" }
2022-12-04 10:40:18 +01:00
"Light sleet" { return "❄️ light sleet" }
2023-05-26 12:20:18 +02:00
"Light sleet showers" { return "❄️ light sleet showers" }
2022-12-04 10:40:18 +01:00
"Light snow" { return "❄️ light snow" }
"Light snow showers" { return "❄️ light snow showers" }
2023-05-26 12:20:18 +02:00
"Moderate or heavy snow showers"{ return "❄️ moderate or heavy snow showers ⚠️" }
"Moderate or heavy freezing rain"{ return "💧 moderate or heavy freezing rain ⚠️" }
"Moderate rain" { return "💧 moderate rain" }
2022-12-04 10:40:18 +01:00
"Moderate snow" { return "❄️ moderate snow" }
"Mist" { return "🌫 misty" }
"Overcast" { return "☁️ overcast" }
2022-11-17 20:02:26 +01:00
"Partly cloudy" { return "⛅partly cloudy" }
2023-05-26 12:20:18 +02:00
"Patchy heavy snow" { return "❄️ patchy heavy snow ⚠️" }
"Patchy light drizzle" { return "💧 patchy light drizzle" }
2022-11-17 20:02:26 +01:00
"Patchy light rain" { return "💧 patchy light rain" }
2023-05-26 12:20:18 +02:00
"Patchy light rain with thunder" { return "💧 patchy light rain with thunder" }
"Patchy light snow" { return "❄️ patchy light snow" }
"Patchy moderate snow" { return "❄️ patchy moderate snow" }
2022-11-17 20:02:26 +01:00
"Patchy rain possible" { return "💧 patchy rain possible" }
2023-05-26 12:20:18 +02:00
"Patchy snow possible" { return "❄️ patchy snow possible" }
2022-12-04 10:40:18 +01:00
"Sunny" { return "☀️ sunny" }
2022-11-17 20:02:26 +01:00
"Thundery outbreaks possible" { return "⚡thundery outbreaks possible" }
2022-12-04 10:40:18 +01:00
default { return "$Text" }
}
}
function GetWindDir { param([string]$Text)
switch($Text) {
"NW" { return "↘" }
"NNW" { return "↓" }
"N" { return "↓" }
"NNE" { return "↓" }
"NE" { return "↙" }
"ENE" { return "←" }
"E" { return "←" }
"ESE" { return "←" }
"SE" { return "↖" }
"SSE" { return "↑" }
"S" { return "↑" }
"SSW" { return "↑" }
"SW" { return "↗" }
"WSW" { return "→" }
"W" { return "→" }
"WNW" { return "→" }
default { return "$Text" }
2022-11-17 20:02:26 +01:00
}
}
try {
2023-05-26 12:20:18 +02:00
Write-Progress "⏳ Loading data from http://wttr.in ..."
2022-11-17 20:02:26 +01:00
$Weather = (Invoke-WebRequest -URI http://wttr.in/${Location}?format=j1 -userAgent "curl" -useBasicParsing).Content | ConvertFrom-Json
2023-05-26 12:20:18 +02:00
Write-Progress -completed "."
2022-11-17 20:02:26 +01:00
$Area = $Weather.nearest_area.areaName.value
$Region = $Weather.nearest_area.region.value
$Country = $Weather.nearest_area.country.value
[int]$Day = 0
foreach($Hourly in $Weather.weather.hourly) {
$Hour = $Hourly.time / 100
2022-12-04 10:40:18 +01:00
$Temp = $(($Hourly.tempC.toString()).PadLeft(3))
2022-11-17 20:02:26 +01:00
$Precip = $Hourly.precipMM
2022-12-04 10:40:18 +01:00
$Humidity = $(($Hourly.humidity.toString()).PadLeft(3))
2022-11-17 20:02:26 +01:00
$Pressure = $Hourly.pressure
2022-12-04 10:40:18 +01:00
$WindSpeed = $(($Hourly.windspeedKmph.toString()).PadLeft(2))
$WindDir = GetWindDir $Hourly.winddir16Point
2022-11-17 20:02:26 +01:00
$UV = $Hourly.uvIndex
2022-12-04 10:40:18 +01:00
$Clouds = $(($Hourly.cloudcover.toString()).PadLeft(3))
$Visib = $(($Hourly.visibility.toString()).PadLeft(2))
$Desc = GetDescription $Hourly.weatherDesc.value
2022-11-17 20:02:26 +01:00
if ($Hour -eq 0) {
if ($Day -eq 0) {
2023-05-26 12:20:18 +02:00
Write-Host -foregroundColor green "TODAY 🌡°C ☂mm 💧 💨km/h ☀UV ☁️ 👁km at $Area ($Region, $Country)"
2022-11-17 20:02:26 +01:00
} elseif ($Day -eq 1) {
2023-05-26 12:20:18 +02:00
$Date = (Get-Date).AddDays(1)
[string]$Weekday = $Date.DayOfWeek
Write-Host -foregroundColor green "$($Weekday.toUpper())"
2022-11-17 20:02:26 +01:00
} else {
2023-05-26 12:20:18 +02:00
$Date = (Get-Date).AddDays(2)
[string]$Weekday = $Date.DayOfWeek
Write-Host -foregroundColor green "$($Weekday.toUpper())"
2022-11-17 20:02:26 +01:00
}
$Day++
}
2023-05-26 12:20:18 +02:00
"$(($Hour.toString()).PadLeft(2))°° $Temp° $Precip $Humidity% $($WindDir)$WindSpeed $UV $Clouds% $Visib $Desc"
2022-11-17 20:02:26 +01:00
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2021-11-08 21:36:42 +01:00
*Generated by convert-ps2md.ps1 using the comment-based help of list-weather.ps1*