Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Weather Retrieval Method #158

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 77 additions & 4 deletions winfetch.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ $defaultConfig = @'
# $diskstyle = 'bartext'
# $batterystyle = 'bartext'

# Configure whether to use Metric or Imperial units when fetching the weather
# Default is Metric unless your location is in the US
# $units = 'Metric'

# Provide your own API key for OpenWeatherMap weather info
$authKey = ""


# Remove the '#' from any of the lines in
# the following to **enable** their output.
Expand Down Expand Up @@ -1365,13 +1372,79 @@ function info_locale {

# ===== WEATHER =====
function info_weather {
# Weather Conditions Lookup Hashtable
$conditionLookup = @{
# Group 2xx: Thunderstorm
200 = "Thunderstorms with Light Rain"; 201 = "Thunderstorms with Rain"; 202 = "Thunderstorms with Heavy Rain";
210 = "Light Thunderstorms"; 211 = "Thunderstorms"; 212 = "Heavy Thunderstorms";
221 = "Ragged Thunderstorms"; 230 = "Thunderstorms with Light Drizzle"; 231 = "Thunderstorms with Drizzle";
232 = "Thunderstorm with Heavy Drizzle";
# Group 3xx: Drizzle
300 = "Light Drizzle"; 301 = "Drizzle"; 302 = "Heavy Drizzle";
310 = "Light Drizzle Rain"; 311 = "Drizzle Rain"; 312 = "Heavy Drizzle Rain";
313 = "Shower Rain and Drizzle"; 314 = "Heavy Shower Rain and Drizzle"; 321 = "Shower Drizzle";
# Group 5xx: Rain
500 = "Light Rain"; 501 = "Moderate Rain"; 502 = "Heavy Rain";
503 = "Very Heavy Rain"; 504 = "Extreme Rain"; 511 = "Freezing Rain";
520 = "Light Showers"; 521 = "Showers"; 522 = "Heavy Showers";
531 = "Ragged Showers";
# Group 6xx: Snow
600 = "Light Snow"; 601 = "Snow"; 602 = "Heavy Snow";
611 = "Sleet"; 612 = "Light Shower Sleet"; 613 = "Shower Sleet";
615 = "Light Rain and Snow"; 616 = "Rain and Snow"; 620 = "Light Shower Snow";
621 = "Shower Snow"; 622 = "Heavy Shower Snow";
# Group 7xx: Atmosphere
701 = "Mist"; 711 = "Smoke"; 721 = "Haze";
731 = "Sand/Dust Whirls"; 741 = "Fog"; 751 = "Sand";
761 = "Dust"; 762 = "Volcanic Ash"; 771 = "Squalls";
781 = "Tornados";
# Group 800: Clear
800 = "Clear Skies";
# Group 80x: Clouds
801 = "Few Clouds"; <#11-25%#> 802 = "Scattered Clouds"; <#25-50%#> 803 = "Broken Clouds"; #51-84%
804 = "Overcast"; #85-100%
}

return @{
title = "Weather"
content = try {
(Invoke-RestMethod wttr.in/?format="%t+-+%C+(%l)").TrimStart("+")
} catch {
"$e[91m(Network Error)"
}
# Import System.Device.dll to get location from GPS
$null = [System.Reflection.Assembly]::LoadWithPartialName("System.Device")

$watcher = [System.Device.Location.GeoCoordinateWatcher]::new()
# Start monitoring GPS location
$watcher.Start()

if ($watcher.Permission -eq "Granted") {
# Wait for the GPS watcher to initialize
while ($watcher.Status -eq "Initializing") {}

# Get coordinates
$lat = $watcher.Position.Location.Latitude
$lon = $watcher.Position.Location.Longitude

# Change units based on locale
if ([Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('CurrentUser', $Env:COMPUTERNAME).OpenSubKey("Control Panel\International\Geo").Nation -contains "US") {
$units = "imperial"
} else {
$units = "metric"
}
} else {
# Gets Location from IP using ip-api.com
$location = ConvertFrom-Json ([System.Net.WebClient]::new().DownloadString("http://ip-api.com/json/"))
$lat = $location.lat
$lon = $location.lon

# Change units based on location
$units = if($location.country -eq "United States"){"imperial"}else{"metric"}
}

# Get Current Weather from OpenWeatherMap API
$currentWeather = ConvertFrom-Json ([System.Net.WebClient]::new().DownloadString("https://api.openweathermap.org/data/2.5/weather?lat=$($lat)&lon=$($lon)&appid=$authKey&units=$units"))
"$($currentWeather.main.temp)$(if($units -eq "imperial"){"°F"}else{"°C"}) - $($conditionLookup[[int]$currentWeather.weather.id]) ($($location.city), $($location.regionName), $($location.country))"
} catch {
"$e[91m(Network Error)"
}
}
}

Expand Down