From 587c6948a2894b23105a26a172713dbcbd6ffcfe Mon Sep 17 00:00:00 2001 From: Carterpersall Date: Fri, 30 Sep 2022 16:25:00 -0500 Subject: [PATCH 1/5] New Method Working - Replaces the method of fetching the weather with a new one - First grabs the location of the user using ip-api.com - TODO: First try to grab location using Windows Location services (If not disabled) - Takes the coordinates found, and calls OpenWeatherMap's API using a key I created to get the current weather conditions - This new method is faster than grabbing the info from wttr.in due to the high latency of wttr.in - wttr.in Ping is 115ms vs ip-api's 20ms and api.openweathermap.org 25ms under my favorable conditions - This could vary based on location --- winfetch.ps1 | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/winfetch.ps1 b/winfetch.ps1 index aadf9ac..5978b44 100644 --- a/winfetch.ps1 +++ b/winfetch.ps1 @@ -1365,13 +1365,52 @@ function info_locale { # ===== WEATHER ===== function info_weather { + $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% + } + $authKey = 7783b2da70874d3e84836faf299dbffc # Auth Key for OpenWeatherMap API + return @{ title = "Weather" content = try { - (Invoke-RestMethod wttr.in/?format="%t+-+%C+(%l)").TrimStart("+") - } catch { - "$e[91m(Network Error)" - } + # Gets Location from IP using ip-api.com + $location = Invoke-RestMethod -Uri "http://ip-api.com/json/" + # Changes units used based on location + $units = if($location.country -eq "United States"){"imperial"}else{"metric"} + # Get current weather from OpenWeatherMap API + $currentWeather = (Invoke-RestMethod "https://api.openweathermap.org/data/2.5/weather?lat=$($location.lat)&lon=$($location.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)" + } } } From 041e14120fd644b3ac0a27464e4f4cca54a5b955 Mon Sep 17 00:00:00 2001 From: Carterpersall Date: Mon, 3 Oct 2022 10:49:58 -0500 Subject: [PATCH 2/5] Fix Bug --- winfetch.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/winfetch.ps1 b/winfetch.ps1 index 5978b44..8ae5cda 100644 --- a/winfetch.ps1 +++ b/winfetch.ps1 @@ -1365,6 +1365,7 @@ 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"; @@ -1396,7 +1397,7 @@ function info_weather { 801 = "Few Clouds"; <#11-25%#> 802 = "Scattered Clouds"; <#25-50%#> 803 = "Broken Clouds"; #51-84% 804 = "Overcast"; #85-100% } - $authKey = 7783b2da70874d3e84836faf299dbffc # Auth Key for OpenWeatherMap API + $authKey = "7783b2da70874d3e84836faf299dbffc" # Auth Key for OpenWeatherMap API return @{ title = "Weather" From e1343b7c82d31ac74eb689b711d041d36719f2ac Mon Sep 17 00:00:00 2001 From: Carterpersall Date: Mon, 3 Oct 2022 19:05:10 -0500 Subject: [PATCH 3/5] Slight Grammar Tweaks --- winfetch.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/winfetch.ps1 b/winfetch.ps1 index 8ae5cda..9ef9eae 100644 --- a/winfetch.ps1 +++ b/winfetch.ps1 @@ -1404,9 +1404,9 @@ function info_weather { content = try { # Gets Location from IP using ip-api.com $location = Invoke-RestMethod -Uri "http://ip-api.com/json/" - # Changes units used based on location + # Change units used based on location $units = if($location.country -eq "United States"){"imperial"}else{"metric"} - # Get current weather from OpenWeatherMap API + # Get Current Weather from OpenWeatherMap API $currentWeather = (Invoke-RestMethod "https://api.openweathermap.org/data/2.5/weather?lat=$($location.lat)&lon=$($location.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 { From 7d23394849de89393b6ddfdf9cb67cb17f7f158f Mon Sep 17 00:00:00 2001 From: Carterpersall Date: Fri, 21 Oct 2022 15:17:50 -0500 Subject: [PATCH 4/5] Replace Invoke-RestMethod with .NET calls - The .NET calls are ~10% faster than `Invoke-RestMethod` --- winfetch.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/winfetch.ps1 b/winfetch.ps1 index 9ef9eae..7730e0c 100644 --- a/winfetch.ps1 +++ b/winfetch.ps1 @@ -1403,11 +1403,11 @@ function info_weather { title = "Weather" content = try { # Gets Location from IP using ip-api.com - $location = Invoke-RestMethod -Uri "http://ip-api.com/json/" + $location = ConvertFrom-Json ([System.Net.WebClient]::new().DownloadString("http://ip-api.com/json/")) # Change units used based on location $units = if($location.country -eq "United States"){"imperial"}else{"metric"} # Get Current Weather from OpenWeatherMap API - $currentWeather = (Invoke-RestMethod "https://api.openweathermap.org/data/2.5/weather?lat=$($location.lat)&lon=$($location.lon)&appid=$authKey&units=$units") + $currentWeather = ConvertFrom-Json ([System.Net.WebClient]::new().DownloadString("https://api.openweathermap.org/data/2.5/weather?lat=$($location.lat)&lon=$($location.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)" From 13cdb46f46e530e9da7e4fd1fce21652bebd6028 Mon Sep 17 00:00:00 2001 From: Carterpersall Date: Wed, 1 Feb 2023 11:26:22 -0600 Subject: [PATCH 5/5] GPS Location fetching and config entries --- winfetch.ps1 | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/winfetch.ps1 b/winfetch.ps1 index 7730e0c..8360ead 100644 --- a/winfetch.ps1 +++ b/winfetch.ps1 @@ -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. @@ -1397,17 +1404,43 @@ function info_weather { 801 = "Few Clouds"; <#11-25%#> 802 = "Scattered Clouds"; <#25-50%#> 803 = "Broken Clouds"; #51-84% 804 = "Overcast"; #85-100% } - $authKey = "7783b2da70874d3e84836faf299dbffc" # Auth Key for OpenWeatherMap API return @{ title = "Weather" content = try { - # Gets Location from IP using ip-api.com - $location = ConvertFrom-Json ([System.Net.WebClient]::new().DownloadString("http://ip-api.com/json/")) - # Change units used based on location - $units = if($location.country -eq "United States"){"imperial"}else{"metric"} + # 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=$($location.lat)&lon=$($location.lon)&appid=$authKey&units=$units")) + $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)"