Improved watch-news.ps1

This commit is contained in:
Markus Fleschutz 2024-05-21 21:37:41 +02:00
parent 659807f16c
commit cbf0c4ce14

View File

@ -5,8 +5,10 @@
This PowerShell script lists the latest headlines by using a RSS (Really Simple Syndication) feed. This PowerShell script lists the latest headlines by using a RSS (Really Simple Syndication) feed.
.PARAMETER RSS_URL .PARAMETER RSS_URL
Specifies the URL to the RSS feed (Yahoo World News by default) 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 .EXAMPLE
PS> ./watch-headlines.ps1 PS> ./watch-news.ps1
Niger coup: Ecowas deadline sparks anxiety in northern Nigeria Niger coup: Ecowas deadline sparks anxiety in northern Nigeria
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
@ -16,20 +18,26 @@
param([string]$RSS_URL = "https://news.yahoo.com/rss/world", [int]$timeInterval = 30000) # in ms 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 [xml]$content = (Invoke-WebRequest -URI $RSS_URL -useBasicParsing).Content
foreach ($item in $content.rss.channel.item) { return "$($item.title)" } [string]$latest = ""
return ""
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 { try {
$previous = "" $latest = ""
while ($true) { while ($true) {
$latest = GetLatestHeadline $latest = PrintLatestHeadlines $latest 10
if ($latest -ne $previous) {
& "$PSScriptRoot/write-animated.ps1" "❇️ $latest ❇️"
$previous = $latest
}
Start-Sleep -milliseconds $timeInterval Start-Sleep -milliseconds $timeInterval
} }
exit 0 # success exit 0 # success