Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Get-EpochDate ($epochDate) {
- [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($epochDate))
- }
- #$apiKey = ''
- #$cityId = 0000000
- #$url = "https://api.openweathermap.org/data/2.5/forecast?id={0}&units=imperial&appid={1}" -f $cityId, $apiKey
- #$forecast = Invoke-RestMethod -Uri $url -Method Get
- $forecast = Get-Content .\ResponseExample.json -Raw | ConvertFrom-Json
- #$today = [datetime]::Today
- $today = Get-Date -Month 7 -Day 18 -Year 2020
- $tomorrow = $today.AddDays(1)
- $dayAfter = $today.AddDays(2)
- $todayTemps = New-Object 'System.Collections.Generic.List[decimal]'
- $tomorrowTemps = New-Object 'System.Collections.Generic.List[decimal]'
- $tomorrowWeather = New-Object 'System.Collections.Generic.List[object]'
- foreach ($item in $forecast.list) {
- $date = Get-EpochDate $item.dt
- if ($date -ge $today -and $date -lt $tomorrow) {
- # Add to today's tally
- $todayTemps.Add($item.main.temp)
- }
- elseif ($date -ge $tomorrow -and $date -lt $dayAfter) {
- # Add to tomorrow's tally
- $tomorrowTemps.Add($item.main.temp)
- $tomorrowWeather.Add(
- [pscustomobject]@{
- Time = $date
- Category = $item.weather.main
- Weather = switch ($item.weather.description) {
- "few clouds" {
- "scattered clouds"
- }
- "clear sky" {
- "clear skies"
- }
- default {
- $item.weather.description
- }
- }
- }
- )
- }
- }
- $tomorrowsHigh = $tomorrowTemps | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum
- $tomorrowsHigh = [math]::Round($tomorrowsHigh, 0, "AwayFromZero")
- if ([datetime]::Now -le $today.AddHours(14)) {
- $doToday = $true
- $todaysHigh = $todayTemps | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum
- $todaysHigh = [math]::Round($todaysHigh, 0, "AwayFromZero")
- }
- Add-Type -AssemblyName System.Speech
- $voice = New-Object System.Speech.Synthesis.SpeechSynthesizer
- $voice.Speak("Here's today and tomorrow's weather forecast for $($forecast.city.name), $($forecast.city.state).")
- # Say sunrise+sunset
- $sunrise = Get-EpochDate $forecast.city.sunrise
- if ([datetime]::Now -lt $sunrise) {
- $voice.Speak("The sun will rise at $($sunrise.ToString("h:mm tt"))")
- }
- $sunset = Get-EpochDate $forecast.city.sunset
- if ([datetime]::Now -lt $sunset) {
- $voice.Speak("Sunset will happen at $($sunset.ToString("h:mm tt"))")
- }
- #region TEMPERATURES
- if ($doToday) {
- $voice.Speak("The high temperature for today, $($today.ToString("MMMM d yyyy")), will be $todaysHigh degress fahrenheit.")
- }
- $voice.Speak("Tomorrow, the forecasted high is $tomorrowsHigh degrees fahrenheit.")
- #endregion
- #region TOMORROW FORECAST
- $tomMorning = $tomorrowWeather[2..3]
- $tomMorningGroup = $tomMorning | Group-Object -Property Weather
- $speakTomMorning = "Tomorrow's forecast calls for"
- if (@($tomMorningGroup).Count -ge 2) {
- $speakTomMorning = "$speakTomMorning {0} early in the morning, transitioning to {1} before noon." -f $tomMorning[0].Weather, $tomMorning[1].Weather
- }
- else {
- $speakTomMorning = "$speakTomMorning $($tomMorningGroup.Name) all morning."
- }
- $speakTomMorning
- $voice.Speak($speakTomMorning)
- $tomEvening = $tomorrowWeather[4..($tomorrowWeather.Count - 1)]
- $speakTomEvening = "In the afternoon and evening,"
- for ($i = 0; $i -lt $tomEvening.Count; $i++) {
- $wevent = $tomEvening[$i]
- switch ($i) {
- 0 {
- $twoEvent = $wevent.Weather
- $speakTomEvening = "$speakTomEvening $twoEvent can be expected around 2:00."
- $lastCat = $wevent.Category
- }
- default {
- $thisEvent = $wevent.Weather
- $thisCat = $wevent.Category
- $time = $wevent.Time.ToString("h:mm tt")
- if ($thisCat -ne $lastCat) {
- if ($i -eq $tomEvening.Count - 1) {
- $speakTomEvening = "$speakTomEvening And finally,"
- }
- $speakTomEvening = "$speakTomEvening The weather will change to $thisEvent around $time."
- }
- $lastCat = $thisCat
- }
- }
- }
- $speakTomEvening
- $voice.Speak($speakTomEvening)
- #endregion
- $voice.Dispose()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement