-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGet-WeatherReport.ps1
117 lines (105 loc) · 4.06 KB
/
Get-WeatherReport.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
Function Get-WeatherReport {
<#
.SYNOPSIS
Retrieves the current weather report or a forecast for a specified city.
.DESCRIPTION
This function fetches weather information for a specified city using the wttr, weather report can be displayed in the console or saved to a file.
.PARAMETER City
Mandatory - city for which the weather report should be retrieved, feel free to populate.
.PARAMETER Provider
NotMandatory - specifies the weather provider to use, both are API-less.
.PARAMETER ReportType
NotMandatory - type of weather report to retrieve.
.PARAMETER Units
NotMandatory - units of measurement to use.
.PARAMETER Encoding
NotMandatory - choose encoding to use for the output file.
.PARAMETER Days
NotMandatory - number of days to retrieve weather information for when the report type is set to daily.
.PARAMETER Lang
NotMandatory - language for the weather report when the report type is set to daily.
.PARAMETER OutputFile
NotMandatory - output file to which the weather report should be saved.
.PARAMETER ShowInBrowser
NotMandatory - if specified, opens the weather report URL in the default web browser.
.EXAMPLE
Get-WeatherReport -City Amsterdam -Provider wttrin -ShowInBrowser -Verbose
Get-WeatherReport -City Rome -Provider wttr -Units imperial -Verbose
"$env:USERPROFILE\Desktop\Weather_Report.csv" | Get-WeatherReport -City London -Provider wttr
.NOTES
v0.0.3
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateSet("Zagreb", "Paris", "Berlin", "Rome", "Madrid", "London", `
"Barcelona", "Amsterdam", "Brussels", "Stockholm", "Los Angeles")]
[string]$City,
[Parameter(Mandatory = $false, Position = 1)]
[ValidateSet("wttrin", "wttr")]
[string]$Provider = "wttrin",
[Parameter(Mandatory = $false)]
[ValidateSet("current", "hourly", "daily")]
[string]$ReportType = "current",
[Parameter(Mandatory = $false)]
[ValidateSet("metric", "imperial")]
[string]$Units = "metric",
[Parameter(Mandatory = $false)]
[ValidateSet("ascii", "bigendianunicode", "default", "oem", "string", `
"unicode", "unknown", "utf32", "utf7", "utf8")]
[string]$Encoding = "unicode",
[Parameter(Mandatory = $false)]
[ValidateRange(1, 365)]
[int]$Days = 1,
[Parameter(Mandatory = $false)]
[ValidateSet("en", "fr", "de", "it", "ja", "pt", "ru", "tr", "ar", "zh")]
[string]$Lang = "en",
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
[string]$OutputFile,
[Parameter(Mandatory = $false)]
[switch]$ShowInBrowser
)
$Url = $null ; Clear-Host
Write-Verbose -Message "Starting..."
switch ($Provider) {
"wttrin" {
$Url = "https://wttr.in/$City"
}
"wttr" {
$Url = "https://wttr.in/$City?format=%C+%t"
}
default {
Write-Error "Invalid provider: $Provider"
return
}
}
Write-Verbose -Message "Adding report type and units to the URL"
if ($Provider -eq "wttrin") {
$Url += "?$ReportType"
if ($Units -eq "imperial") {
$Url += "I"
}
}
Write-Verbose -Message "Adding number of days and language to the URL"
if ($ReportType -eq "daily") {
$Url += "&n=$Days&lang=$Lang"
}
Write-Host "Querying for weather, please wait..." -ForegroundColor Cyan
try {
$Result = Invoke-RestMethod $Url
if ($ShowInBrowser) {
Write-Output "Opening report in the default browser"
Start-Process $Url -Wait
}
if ($OutputFile) {
$Result | Out-File $OutputFile -Encoding $Encoding -Force
}
}
catch {
Write-Error "Error accessing $Provider API: $($_.Exception.Message)"
}
finally {
Write-Verbose -Message $Result | Format-Table -AutoSize
Write-Host "Finished, goodbye :)" -ForegroundColor DarkCyan
}
}