Public/Get-Weather.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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
using namespace System.Text function Get-Weather { [CmdletBinding(DefaultParameterSetName='ZipCodeForecast')] [OutputType([string])] PARAM ( [Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,ParameterSetName='ZipCodeForecast')] [Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,ParameterSetName='ZipCodeAlert')] [Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,ParameterSetName='ZipCodeForecastAlert')] [int]$ZipCode, [Parameter(Mandatory=$true,Position=0,ValueFromPipelineByPropertyName=$true,ParameterSetName='GeoForecast')] [Parameter(Mandatory=$true,Position=0,ValueFromPipelineByPropertyName=$true,ParameterSetName='GeoAlert')] [Parameter(Mandatory=$true,Position=0,ValueFromPipelineByPropertyName=$true,ParameterSetName='GeoForecastAlert')] [double]$Latitude, [Parameter(Mandatory=$true,Position=1,ValueFromPipelineByPropertyName=$true,ParameterSetName='GeoForecast')] [Parameter(Mandatory=$true,Position=1,ValueFromPipelineByPropertyName=$true,ParameterSetName='GeoAlert')] [Parameter(Mandatory=$true,Position=1,ValueFromPipelineByPropertyName=$true,ParameterSetName='GeoForecastAlert')] [double]$Longitude, [Parameter(ParameterSetName='ZipCodeForecast')] [Parameter(ParameterSetName='GeoForecast')] [Parameter(ParameterSetName='ZipCodeForecastAlert')] [Parameter(ParameterSetName='GeoForecastAlert')] [ValidateSet(0,1,2,3,4,5,6,7,8,9,10,11,12,13)][int]$PeriodsFromNow = 0, [Parameter(ParameterSetName='ZipCodeForecast')] [Parameter(ParameterSetName='GeoForecast')] [Parameter(Mandatory=$true,ParameterSetName='ZipCodeForecastAlert')] [Parameter(Mandatory=$true,ParameterSetName='GeoForecastAlert')] [ValidateSet('Short', 'Detailed')][string]$ForecastType = 'Short', [Parameter(ParameterSetName='ZipCodeForecast')] [Parameter(ParameterSetName='GeoForecast')] [Parameter(ParameterSetName='ZipCodeForecastAlert')] [Parameter(ParameterSetName='GeoForecastAlert')] [switch]$IncludeLocation, [Parameter(ParameterSetName='ZipCodeForecast')] [Parameter(ParameterSetName='GeoForecast')] [Parameter(ParameterSetName='ZipCodeForecastAlert')] [Parameter(ParameterSetName='GeoForecastAlert')] [switch]$IncludeTime, [Parameter(Mandatory=$true,ParameterSetName='ZipCodeAlert')] [Parameter(Mandatory=$true,ParameterSetName='GeoAlert')] [Parameter(Mandatory=$true,ParameterSetName='ZipCodeForecastAlert')] [Parameter(Mandatory=$true,ParameterSetName='GeoForecastAlert')] [ValidateSet('Short', 'Detailed', 'Full')][string]$AlertType, [Parameter(ParameterSetName='ZipCodeAlert')] [Parameter(ParameterSetName='GeoAlert')] [Parameter(ParameterSetName='ZipCodeForecastAlert')] [Parameter(ParameterSetName='GeoForecastAlert')] [switch]$IncludeAlertCount, [Parameter()] [switch]$Speakable ) begin { $GeoData = $null $Abbreviations = @{ AL = 'Alabama'; AK = 'Alaska'; AZ = 'Arizona' AR = 'Arkansas'; CA = 'California'; CO = 'Colorado' CT = 'Connecticut'; DE = 'Delaware'; DC = 'DC' FL = 'Florida'; GA = 'Georgia'; HI = 'Hawaii' ID = 'Idaho'; IL = 'Illinois'; IN = 'Indiana' IA = 'Iowa'; KS = 'Kansas'; KY = 'Kentucky' LA = 'Louisiana'; ME = 'Maine'; MD = 'Maryland' MA = 'Massachusetts'; MI = 'Michigan'; MN = 'Minnesota' MS = 'Mississippi'; MO = 'Missouri'; MT = 'Montana' NE = 'Nebraska'; NV = 'Nevada'; NH = 'New Hampshire' NJ = 'New Jersey'; NM = 'New Mexico'; NY = 'New York' NC = 'North Carolina'; ND = 'North Dakota'; OH = 'Ohio' OK = 'Oklahoma'; OR = 'Oregon'; PA = 'Pennsylvania' RI = 'Rhode Island'; SC = 'South Carolina'; CZ = 'in the Panama Canal Zone' TN = 'Tennessee'; TX = 'Texas'; AE = 'in the U.S. Armed Forces - Europe' VT = 'Vermont'; VA = 'Virginia'; CM = 'in the Northern Mariana Islands' WV = 'West Virginia'; WI = 'Wisconsin'; AA = 'in the U.S. Armed Forces - Americas' AS = 'American Samoa'; GU = 'Guam'; MP = 'Northern Mariana Islands' PR = 'Puerto Rico'; NB = 'Nebraska'; VI = 'in the U.S. Virgin Islands' WY = 'Wyoming'; UT = 'Utah'; AP = 'in the U.S. Armed Forces - Pacific' WA = 'Washington'; SD = 'South Dakota'; PI = 'in the Philippine Islands' TT = 'in the Trust Territory of the Pacific Islands' } function Format-NiceDate ([datetime]$Date) { $Date.ToString('dddd MMMM * a\t h:mm tt') -replace '\*', (Get-OrdinalNumber -Number $Date.Day) } function Format-AlertHeadline ($Alert) { [datetime]$Issued = $Alert.sent [datetime]$Effective = $Alert.effective [datetime]$Ends = if($Alert.ends) { $Alert.ends } else { $Alert.expires } [string[]]$Areas = $Alert.areaDesc -split '; ' [string]$AreaDescription = if($Areas.Count -gt 1) { ($Areas[0..$($Areas.Count-2)] -join ', ') + ' and ' + ($Areas[-1]) } else { $Areas[0] } if($Issued -eq $Effective) { "$($Alert.status) $($Alert.severity) $($Alert.event) issued $(Format-NiceDate $Issued) for $AreaDescription, effective until $(Format-NiceDate $Ends). " } else { "$($Alert.status) $($Alert.severity) $($Alert.event) issued $(Format-NiceDate $Issued) for $AreaDescription, effective from $(Format-NiceDate $Effective) until $(Format-NiceDate $Ends). " } } } process { # Get the $Location table for our $PointURI $Location = @{ LAT = $Latitude; LNG = $Longitude } if($PSCmdlet.ParameterSetName.StartsWith('ZipCode')) { if($null -eq $GeoData) { # Don't import the Zip/GPS mapping until we need it. $GeoData = Import-Csv -Path "$PSScriptRoot/../data/zip-geo.csv" } $Location = $GeoData | Where-Object ZIP -EQ $ZipCode if(!$Location) { throw "Could not find location $ZipCode in the available data." } } [string]$PointUri = "https://api.weather.gov/points/$($Location.LAT),$($Location.LNG)" $PointData = Invoke-RestMethod -Uri $PointUri -UseBasicParsing -Headers @{Accept='application/geo+json'} [string]$ZoneID = $PointData.properties.forecastZone -replace 'https?://api\.weather\.gov/zones/forecast/' # After this statement, assume $Location is a string. if($PointData.properties.relativeLocation.properties.city) { $Location = "$($PointData.properties.relativeLocation.properties.city) $($Abbreviations[$PointData.properties.relativeLocation.properties.state])" } elseif($PSCmdlet.ParameterSetName -eq 'ZipCode') { $Location = "Zip Code $("$ZipCode" -split '' -join ' ')" } else { $Location = "Latitude $Latitude, Longitude $Longitude, " } $APIResponse = Invoke-RestMethod -Uri "$PointUri/forecast" -UseBasicParsing -Headers @{Accept='application/geo+json'} $ForecastData = $APIResponse.properties.periods[$PeriodsFromNow] [StringBuilder]$Output = [StringBuilder]::new() [bool]$IsForecast = $PSCmdlet.ParameterSetName.Contains('Forecast') -or $PSBoundParameters.ContainsKey('ForecastType') [bool]$IsAlert = $PSBoundParameters.ContainsKey('AlertType') if($IsForecast) { if($IncludeLocation) { if($IncludeTime) { [void]$Output.Append("The forecast $($ForecastData.name) for $Location is: ") } else { [void]$Output.Append("The forecast for $Location is: ") } } elseif($IncludeTime) { [void]$Output.Append("The forecast for $($ForecastData.name) is: ") } if($ForecastType -eq 'Short') { [void]$Output.Append($ForecastData.shortForecast) } else { [void]$Output.Append($ForecastData.detailedForecast) } [void]$Output.Append("`n") } if($IsAlert) { $APIResponse = Invoke-RestMethod -URI "https://api.weather.gov/alerts/active/zone/$ZoneID" if($APIResponse.features.Count -eq 0) { [void]$Output.Append("There are no current alerts for $($Location -replace ', $').") [string]$OutputString = $Output.ToString().Trim() if($Speakable) { $OutputString = $OutputString -replace '\bwind(s?)\b', 'winnd$1' } return $OutputString # early-out } if($IncludeAlertCount) { [void]$Output.Append("There $(if($APIResponse.features.Count -eq 1) { 'is' } else { 'are' }) $($APIResponse.features.Count) current alerts for $($Location -replace ', $'):`n") } foreach($Alert in $APIResponse.features) { $Alert = $Alert.properties [string]$Headline = Format-AlertHeadline $Alert switch ($AlertType) { 'Short' { [void]$Output.Append($Headline) } 'Detailed' { [void]$Output.Append($Headline) [void]$Output.Append("The description is: ").Append(($Alert.description -replace '^\.\.', '' -replace '\\n', ' ' -replace "`n", ' ' -replace '\* ', ' ')) [void]$Output.Append("`n") } 'Full' { [void]$Output.Append($Headline) [void]$Output.Append("The description is: ").Append(($Alert.description -replace '^\.\.', '' -replace '\\n', ' ' -replace "`n", ' ' -replace '\* ', ' ')) [void]$Output.Append(($Alert.instruction -replace '\\n', ' ' -replace "`n", '' -replace '\* ', ' ')) [void]$Output.Append("`n") } } [void]$Output.Append("`n") } } [string]$OutputString = $Output.ToString().Trim() if($Speakable) { $OutputString = $OutputString -replace '\bwind(s?)\b', 'winnd$1' -replace '\bmph\b', 'miles per hour' } return $OutputString } } |