326
Post New Requests Here / Re: IDEA: Display the weather in the command line (Weather forcast to text)
« Last post by wraith808 on May 13, 2021, 11:17 PM »So, this isn't exactly what you asked for, but it was an interesting problem, so I did it anyway 
It uses wttr.in (I couldn't figure out that other site- when I put in a location, it required a more specific name for the results page than I could figure out quickly).
It's in powershell, and I documented it in the comments. Save it to Get-Weather.ps1 and run it, i.e.
.\Get-Weather.ps1
It will prompt you for the city- you can specify it just by city and it will sometimes get it right from your location, but you can also use state/province/country, etc - just separate with ,. i.e.
.\Get-Weather.ps1
Enter name of the City to get weather report: Cairo
This would display the weather for Cairo,Egypt, but
.\Get-Weather.ps1
Enter name of the City to get weather report: Cairo,Illinois
Would display the weather for Cairo,Illinois

It uses wttr.in (I couldn't figure out that other site- when I put in a location, it required a more specific name for the results page than I could figure out quickly).
It's in powershell, and I documented it in the comments. Save it to Get-Weather.ps1 and run it, i.e.
.\Get-Weather.ps1
It will prompt you for the city- you can specify it just by city and it will sometimes get it right from your location, but you can also use state/province/country, etc - just separate with ,. i.e.
.\Get-Weather.ps1
Enter name of the City to get weather report: Cairo
This would display the weather for Cairo,Egypt, but
.\Get-Weather.ps1
Enter name of the City to get weather report: Cairo,Illinois
Would display the weather for Cairo,Illinois
Code: PowerShell [Select]
- <#
- Get-Weather
- Created in response to a request on DonationCoder
- - https://www.donationcoder.com/forum/index.php?topic=51394
- Retrieves weather from http://wttr.in/ and prints it to commandline
- Parameters:
- -City: [required] City to retrieve the weather for
- -IncludeTomorrow : Also include tomorrow's weather
- -IncludeNext2Days : Also include the weather for the next two days
- #>
- param (
- [string]$City = $( Read-Host "Enter name of the City to get weather report" ),
- [switch] $IncludeTomorrow,
- [switch] $IncludeNext2Days
- )
- try {
- $url = "http://wttr.in/$($City)"
- $Weather = $(Invoke-WebRequest $url –UserAgent curl).content -split "`n"
- if($Weather)
- {
- $Weather[0..16]
- if(($IncludeTomorrow) -or ($IncludeNext2Days)){ $Weather[17..26] }
- if($IncludeNext2Days){ $Weather[27..36] }
- }
- }
- catch {
- $_.exception.Message
- }

Recent Posts