PowerShell/docs/list-weather.md

181 lines
6.6 KiB
Markdown
Raw Permalink Normal View History

2024-11-08 12:38:20 +01:00
The *list-weather.ps1* Script
===========================
2021-11-08 21:36:42 +01:00
2024-05-19 10:25:56 +02:00
This PowerShell script queries the 48h weather report from wttr.in and lists it in a nice table.
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/list-weather.ps1 [[-location] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
2024-05-19 10:25:56 +02:00
-location <String>
Specifies the location to use (determined automatically by default)
2021-11-08 21:36:42 +01:00
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.
```
2023-07-29 10:04:38 +02:00
Example
-------
2021-11-08 21:36:42 +01:00
```powershell
2023-08-06 21:36:33 +02:00
PS> ./list-weather.ps1
2024-05-19 10:25:56 +02:00
TODAY 🌡°C ☂mm 💧 💨km/h ☀UV ☁️ 👁km at Munich (Bayern, Germany)
0h 11° 0.0 88% ↖ 7 1 8% 10 🌙 clear
2023-05-26 12:20:18 +02:00
...
2021-11-08 21:36:42 +01:00
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
Lists the weather report
.DESCRIPTION
2024-05-19 10:25:56 +02:00
This PowerShell script queries the 48h weather report from wttr.in and lists it in a nice table.
.PARAMETER location
Specifies the location to use (determined automatically by default)
2022-11-17 20:02:26 +01:00
.EXAMPLE
2023-08-06 21:36:33 +02:00
PS> ./list-weather.ps1
2024-05-19 10:25:56 +02:00
TODAY 🌡°C ☂mm 💧 💨km/h ☀UV ☁️ 👁km at Munich (Bayern, Germany)
0h 11° 0.0 88% ↖ 7 1 8% 10 🌙 clear
2023-05-26 12:20:18 +02:00
...
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-05-19 10:25:56 +02:00
param([string]$location = "") # empty means determine automatically
2022-11-17 20:02:26 +01:00
2024-05-19 10:25:56 +02:00
function GetDescription([string]$text) {
switch ($text) {
2022-12-04 10:40:18 +01:00
"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" }
2024-05-19 10:25:56 +02:00
"Heavy rain" { return "💧 heavy rain ⚠️" }
2022-12-04 10:40:18 +01:00
"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-09-01 17:53:03 +02:00
"Moderate or heavy freezing rain"{return "💧 moderate or heavy freezing rain ⚠️" }
2023-12-07 20:24:45 +01:00
"Moderate or heavy sleet" { return "❄️ moderate or heavy sleet ⚠️" }
2023-09-01 17:53:03 +02:00
"Moderate or heavy rain shower" { return "💧 moderate or heavy rain shower ⚠️" }
2024-05-19 10:25:56 +02:00
"Moderate or heavy rain in area with thunder" { return "💧 moderate or heavy rain in area with thunder ⚠️" }
2023-05-26 12:20:18 +02:00
"Moderate or heavy snow showers"{ return "❄️ moderate or heavy snow showers ⚠️" }
2024-05-19 10:25:56 +02:00
"Moderate or heavy snow in area with thunder" { return "❄️ moderate or heavy snow in area with thunder ⚠️" }
2023-05-26 12:20:18 +02:00
"Moderate rain" { return "💧 moderate rain" }
2023-07-29 09:45:37 +02:00
"Moderate rain at times" { return "💧 moderate rain at times" }
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" }
2024-05-19 10:25:56 +02:00
"Patchy light rain in area with thunder" { return "💧 patchy light rain in area with thunder" }
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" }
2024-03-27 17:36:59 +01:00
"Patchy rain nearby" { return "💧 patchy rain nearby" }
"Patchy sleet nearby" { return "❄️ patchy sleet nearby" }
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" }
2024-05-19 10:25:56 +02:00
"Thundery outbreaks in nearby" { return "⚡thundery outbreaks in nearby" }
default { return $text }
2022-12-04 10:40:18 +01:00
}
}
2024-05-19 10:25:56 +02:00
function GetWindDir([string]$text) {
switch($text) {
2022-12-04 10:40:18 +01:00
"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 "→" }
2024-05-19 10:25:56 +02:00
default { return "$text" }
2022-11-17 20:02:26 +01:00
}
}
try {
2023-12-07 20:24:45 +01:00
Write-Progress "Loading weather data from http://wttr.in ..."
2024-05-19 10:25:56 +02:00
$weather = (Invoke-WebRequest -URI http://wttr.in/${location}?format=j1 -userAgent "curl" -useBasicParsing).Content | ConvertFrom-Json
$area = $weather.nearest_area.areaName.value
$region = $weather.nearest_area.region.value
$country = $weather.nearest_area.country.value
Write-Progress -completed "Done."
[int]$day = 0
foreach($hourly in $weather.weather.hourly) {
$hour = $hourly.time / 100
$tempC = $(($hourly.tempC.toString()).PadLeft(3))
$precip = $($($hourly.precipMM).PadLeft(4))
$humidity = $(($hourly.humidity.toString()).PadLeft(3))
$pressure = $hourly.pressure
$windSpeed = $(($hourly.windspeedKmph.toString()).PadLeft(2))
$windDir = GetWindDir $hourly.winddir16Point
$UV = $hourly.uvIndex
$clouds = $(($hourly.cloudcover.toString()).PadLeft(3))
$visib = $(($hourly.visibility.toString()).PadLeft(2))
$desc = GetDescription $hourly.weatherDesc.value.trim()
if ($hour -eq 0) {
if ($day -eq 0) {
2024-08-15 09:51:46 +02:00
Write-Host "TODAY 🌡°C ☂mm 💧 💨km/h ☀UV ☁️ 👁km at $area ($region, $country)" -foregroundColor green
2024-05-19 10:25:56 +02:00
} elseif ($day -eq 1) {
$date = (Get-Date).AddDays(1)
[string]$dayOfWeek = $date.DayOfWeek
Write-Host "$($dayOfWeek.toUpper())" -foregroundColor green
2022-11-17 20:02:26 +01:00
} else {
2024-05-19 10:25:56 +02:00
$date = (Get-Date).AddDays(2)
[string]$dayOfWeek = $date.DayOfWeek
Write-Host "$($dayOfWeek.toUpper())" -foregroundColor green
2022-11-17 20:02:26 +01:00
}
2024-05-19 10:25:56 +02:00
$day++
2022-11-17 20:02:26 +01:00
}
2024-05-19 10:25:56 +02:00
"$(($hour.toString()).PadLeft(2))h $tempC° $precip $humidity% $($windDir)$windSpeed $UV $clouds% $visib $desc"
2022-11-17 20:02:26 +01:00
}
exit 0 # success
} catch {
2024-11-08 12:35:11 +01:00
"⚠️ Sorry: $($Error[0]) in script line $($_.InvocationInfo.ScriptLineNumber)."
2022-11-17 20:02:26 +01:00
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2024-11-20 11:52:20 +01:00
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:57)*