From cbf0c4ce14892724ddbed6f41597a7d2e6214767 Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Tue, 21 May 2024 21:37:41 +0200 Subject: [PATCH] Improved watch-news.ps1 --- scripts/watch-news.ps1 | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/scripts/watch-news.ps1 b/scripts/watch-news.ps1 index 968b6ed2..6ff129fa 100755 --- a/scripts/watch-news.ps1 +++ b/scripts/watch-news.ps1 @@ -5,9 +5,11 @@ This PowerShell script lists the latest headlines by using a RSS (Really Simple Syndication) feed. .PARAMETER RSS_URL Specifies the URL to the RSS feed (Yahoo World News by default) +.PARAMETER timeInterval + Specifies the time interval in millisec between the Web requests .EXAMPLE - PS> ./watch-headlines.ps1 - ❇️ Niger coup: Ecowas deadline sparks anxiety in northern Nigeria ❇️ + PS> ./watch-news.ps1 + ❇️ Niger coup: Ecowas deadline sparks anxiety in northern Nigeria ❇️ .LINK https://github.com/fleschutz/PowerShell .NOTES @@ -16,20 +18,26 @@ param([string]$RSS_URL = "https://news.yahoo.com/rss/world", [int]$timeInterval = 30000) # in ms -function GetLatestHeadline { +function PrintLatestHeadlines([string]$previous, [int]$maxLines) { [xml]$content = (Invoke-WebRequest -URI $RSS_URL -useBasicParsing).Content - foreach ($item in $content.rss.channel.item) { return "$($item.title)" } - return "" + [string]$latest = "" + + foreach ($item in $content.rss.channel.item) { + $itemTitle = "$($item.title)" + if ($latest -eq "") { $latest = $itemTitle } + if ($itemTitle -eq $previous) { break } + + & "$PSScriptRoot/write-animated.ps1" "❇️ $itemTitle ❇️" + $maxLines-- + if ($maxLines -eq 0) { break } + } + return $latest } try { - $previous = "" + $latest = "" while ($true) { - $latest = GetLatestHeadline - if ($latest -ne $previous) { - & "$PSScriptRoot/write-animated.ps1" "❇️ $latest ❇️" - $previous = $latest - } + $latest = PrintLatestHeadlines $latest 10 Start-Sleep -milliseconds $timeInterval } exit 0 # success