Updated watch-news.ps1

This commit is contained in:
Markus Fleschutz 2024-11-04 17:09:39 +01:00
parent 70ebba06ff
commit cf34f7dae2

View File

@ -1,35 +1,39 @@
<# <#
.SYNOPSIS .SYNOPSIS
Watch the latest headlines Watch the news
.DESCRIPTION .DESCRIPTION
This PowerShell script lists the latest headlines by using a RSS (Really Simple Syndication) feed. This PowerShell script continuously lists the latest headlines by using a RSS (Really Simple Syndication) feed.
.PARAMETER RSS_URL .PARAMETER 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 lines .PARAMETER lines
Specifies the initial number of headlines Specifies the initial number of headlines
.PARAMETER timeInterval .PARAMETER timeInterval
Specifies the time interval in seconds between the Web requests Specifies the time interval in seconds between two Web requests (30 seconds by default)
.EXAMPLE .EXAMPLE
PS> ./watch-news.ps1 PS> ./watch-news.ps1
Niger coup: Ecowas deadline sparks anxiety in northern Nigeria
UTC Yahoo News - Latest News & Headlines - https://www.yahoo.com/news/world
--- -----------------------------------------------------------------------
14:29 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]$lines = 10, [int]$timeInterval = 30) # in seconds param([string]$URL = "https://news.yahoo.com/rss/world", [int]$lines = 10, [int]$timeInterval = 30) # in seconds
function PrintLatestHeadlines([string]$previous, [int]$maxLines) { function PrintLatestHeadlines([string]$previous, [int]$maxLines) {
[xml]$content = (Invoke-WebRequest -URI $RSS_URL -useBasicParsing).Content [xml]$content = (Invoke-WebRequest -URI $URL -useBasicParsing).Content
[string]$latest = "" [string]$latest = ""
foreach ($item in $content.rss.channel.item) { foreach ($item in $content.rss.channel.item) {
$itemTitle = "$($item.title)" $title = $item.title
if ($latest -eq "") { $latest = $itemTitle } if ($latest -eq "") { $latest = $title }
if ($itemTitle -eq $previous) { break } if ($title -eq $previous) { break }
$time = $item.pubDate.Substring(11, 5)
& "$PSScriptRoot/write-animated.ps1" "❇️ $itemTitle ❇️" & "$PSScriptRoot/write-typewriter.ps1" "❇️ $time $title" 10
$maxLines-- $maxLines--
if ($maxLines -eq 0) { break } if ($maxLines -eq 0) { break }
} }
@ -37,11 +41,18 @@ function PrintLatestHeadlines([string]$previous, [int]$maxLines) {
} }
try { try {
[xml]$content = (Invoke-WebRequest -URI $URL -useBasicParsing).Content
$title = $content.rss.channel.title
$URL = $content.rss.channel.link
" "
" UTC $title - $URL"
" --- -----------------------------------------------------------------------"
$latest = "" $latest = ""
while ($true) { do {
$latest = PrintLatestHeadlines $latest $lines $latest = PrintLatestHeadlines $latest $lines
Start-Sleep -seconds $timeInterval Start-Sleep -seconds $timeInterval
} } while ($true)
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"