Advertisement
Yevrag35

GetAndSayWeather

Jul 21st, 2020
2,429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Get-EpochDate ($epochDate) {
  2.     [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($epochDate))
  3. }
  4.  
  5. #$apiKey = ''
  6. #$cityId = 0000000
  7.  
  8. #$url = "https://api.openweathermap.org/data/2.5/forecast?id={0}&units=imperial&appid={1}" -f $cityId, $apiKey
  9.  
  10. #$forecast = Invoke-RestMethod -Uri $url -Method Get
  11. $forecast = Get-Content .\ResponseExample.json -Raw | ConvertFrom-Json
  12. #$today = [datetime]::Today
  13. $today = Get-Date -Month 7 -Day 18 -Year 2020
  14. $tomorrow = $today.AddDays(1)
  15. $dayAfter = $today.AddDays(2)
  16.  
  17. $todayTemps = New-Object 'System.Collections.Generic.List[decimal]'
  18. $tomorrowTemps = New-Object 'System.Collections.Generic.List[decimal]'
  19.  
  20. $tomorrowWeather = New-Object 'System.Collections.Generic.List[object]'
  21.  
  22. foreach ($item in $forecast.list) {
  23.  
  24.     $date = Get-EpochDate $item.dt
  25.  
  26.     if ($date -ge $today -and $date -lt $tomorrow) {
  27.         # Add to today's tally
  28.         $todayTemps.Add($item.main.temp)
  29.     }
  30.     elseif ($date -ge $tomorrow -and $date -lt $dayAfter) {
  31.         # Add to tomorrow's tally
  32.         $tomorrowTemps.Add($item.main.temp)
  33.         $tomorrowWeather.Add(
  34.        
  35.             [pscustomobject]@{
  36.                 Time     = $date
  37.                 Category = $item.weather.main
  38.                 Weather  = switch ($item.weather.description) {
  39.                     "few clouds" {
  40.                         "scattered clouds"
  41.                     }
  42.                     "clear sky" {
  43.                         "clear skies"
  44.                     }
  45.                     default {
  46.                         $item.weather.description
  47.                     }
  48.                 }
  49.             }
  50.         )
  51.     }
  52. }
  53.  
  54. $tomorrowsHigh = $tomorrowTemps | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum
  55. $tomorrowsHigh = [math]::Round($tomorrowsHigh, 0, "AwayFromZero")
  56.  
  57. if ([datetime]::Now -le $today.AddHours(14)) {
  58.     $doToday = $true
  59.     $todaysHigh = $todayTemps | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum
  60.     $todaysHigh = [math]::Round($todaysHigh, 0, "AwayFromZero")
  61. }
  62.  
  63. Add-Type -AssemblyName System.Speech
  64. $voice = New-Object System.Speech.Synthesis.SpeechSynthesizer
  65.  
  66. $voice.Speak("Here's today and tomorrow's weather forecast for $($forecast.city.name), $($forecast.city.state).")
  67.  
  68. # Say sunrise+sunset
  69. $sunrise = Get-EpochDate $forecast.city.sunrise
  70. if ([datetime]::Now -lt $sunrise) {
  71.     $voice.Speak("The sun will rise at $($sunrise.ToString("h:mm tt"))")
  72. }
  73.  
  74. $sunset = Get-EpochDate $forecast.city.sunset
  75. if ([datetime]::Now -lt $sunset) {
  76.     $voice.Speak("Sunset will happen at $($sunset.ToString("h:mm tt"))")
  77. }
  78.  
  79. #region TEMPERATURES
  80. if ($doToday) {
  81.     $voice.Speak("The high temperature for today, $($today.ToString("MMMM d yyyy")), will be $todaysHigh degress fahrenheit.")
  82. }
  83. $voice.Speak("Tomorrow, the forecasted high is $tomorrowsHigh degrees fahrenheit.")
  84.  
  85. #endregion
  86.  
  87. #region TOMORROW FORECAST
  88. $tomMorning = $tomorrowWeather[2..3]
  89. $tomMorningGroup = $tomMorning | Group-Object -Property Weather
  90.  
  91. $speakTomMorning = "Tomorrow's forecast calls for"
  92. if (@($tomMorningGroup).Count -ge 2) {
  93.  
  94.     $speakTomMorning = "$speakTomMorning {0} early in the morning, transitioning to {1} before noon." -f $tomMorning[0].Weather, $tomMorning[1].Weather
  95. }
  96. else {
  97.  
  98.     $speakTomMorning = "$speakTomMorning $($tomMorningGroup.Name) all morning."
  99. }
  100. $speakTomMorning
  101. $voice.Speak($speakTomMorning)
  102.  
  103. $tomEvening = $tomorrowWeather[4..($tomorrowWeather.Count - 1)]
  104.  
  105. $speakTomEvening = "In the afternoon and evening,"
  106. for ($i = 0; $i -lt $tomEvening.Count; $i++) {
  107.  
  108.     $wevent = $tomEvening[$i]
  109.    
  110.     switch ($i) {
  111.         0 {
  112.             $twoEvent = $wevent.Weather
  113.             $speakTomEvening = "$speakTomEvening $twoEvent can be expected around 2:00."
  114.             $lastCat = $wevent.Category
  115.         }
  116.         default {
  117.             $thisEvent = $wevent.Weather
  118.             $thisCat = $wevent.Category
  119.             $time = $wevent.Time.ToString("h:mm tt")
  120.             if ($thisCat -ne $lastCat) {
  121.  
  122.                 if ($i -eq $tomEvening.Count - 1) {
  123.  
  124.                     $speakTomEvening = "$speakTomEvening And finally,"
  125.                 }
  126.  
  127.                 $speakTomEvening = "$speakTomEvening The weather will change to $thisEvent around $time."
  128.             }
  129.             $lastCat = $thisCat
  130.         }
  131.     }
  132. }
  133. $speakTomEvening
  134. $voice.Speak($speakTomEvening)
  135. #endregion
  136.  
  137. $voice.Dispose()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement