Updated watch-news.ps1

This commit is contained in:
Markus Fleschutz 2024-05-16 17:25:44 +02:00
parent 6136ae80f2
commit a2681adbf9

View File

@ -1,34 +1,37 @@
<# <#
.SYNOPSIS .SYNOPSIS
Lists the latest headlines Watch the latest headlines
.DESCRIPTION .DESCRIPTION
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 maxLines
Specifies the maximum number of lines to list (24 by default)
.EXAMPLE .EXAMPLE
PS> ./list-headlines.ps1 PS> ./watch-headlines.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
.NOTES .NOTES
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
param([string]$RSS_URL = "https://news.yahoo.com/rss/world", [int]$maxLines = 24) param([string]$RSS_URL = "https://news.yahoo.com/rss/world", [int]$timeInterval = 30000) # in ms
function GetLatestHeadline {
[xml]$content = (Invoke-WebRequest -URI $RSS_URL -useBasicParsing).Content
foreach ($item in $content.rss.channel.item) { return "$($item.title)" }
return ""
}
try { try {
[xml]$content = (Invoke-WebRequest -URI $RSS_URL -useBasicParsing).Content $previous = ""
[int]$count = 1 while ($true) {
foreach ($item in $content.rss.channel.item) { $latest = GetLatestHeadline
& "$PSScriptRoot/write-animated.ps1" "* $($item.title) *" if ($latest -ne $previous) {
if ($count++ -eq $maxLines) { break } & "$PSScriptRoot/write-animated.ps1" "❇️ $latest ❇️"
$previous = $latest
}
Start-Sleep -milliseconds $timeInterval
} }
$source = $content.rss.channel.title
$date = $content.rss.channel.pubDate
" (by $source as of $date)"
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"