PowerShell/scripts/watch-news.ps1

50 lines
1.4 KiB
PowerShell
Raw Normal View History

2024-10-01 15:24:16 +02:00
<#
2024-05-16 13:14:01 +02:00
.SYNOPSIS
2024-05-16 17:25:44 +02:00
Watch the latest headlines
2024-05-16 13:14:01 +02:00
.DESCRIPTION
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)
2024-06-13 12:01:41 +02:00
.PARAMETER lines
Specifies the initial number of headlines
2024-05-21 21:37:41 +02:00
.PARAMETER timeInterval
2024-06-13 12:01:41 +02:00
Specifies the time interval in seconds between the Web requests
2024-05-16 13:14:01 +02:00
.EXAMPLE
2024-05-21 21:37:41 +02:00
PS> ./watch-news.ps1
Niger coup: Ecowas deadline sparks anxiety in northern Nigeria
2024-05-16 13:14:01 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-06-13 12:01:41 +02:00
param([string]$RSS_URL = "https://news.yahoo.com/rss/world", [int]$lines = 10, [int]$timeInterval = 30) # in seconds
2024-05-16 13:14:01 +02:00
2024-05-21 21:37:41 +02:00
function PrintLatestHeadlines([string]$previous, [int]$maxLines) {
2024-05-16 13:14:01 +02:00
[xml]$content = (Invoke-WebRequest -URI $RSS_URL -useBasicParsing).Content
2024-05-21 21:37:41 +02:00
[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
2024-05-16 17:25:44 +02:00
}
try {
2024-05-21 21:37:41 +02:00
$latest = ""
2024-05-16 17:25:44 +02:00
while ($true) {
2024-06-13 12:01:41 +02:00
$latest = PrintLatestHeadlines $latest $lines
Start-Sleep -seconds $timeInterval
2024-05-16 13:14:01 +02:00
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}