Updated watch-news.ps1

This commit is contained in:
Markus Fleschutz 2024-11-04 21:21:53 +01:00
parent cd3ad88b65
commit 7a175c2a2e

View File

@ -22,36 +22,33 @@
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
param([string]$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]$timeInterval = 30) # in seconds
function PrintLatestHeadlines([string]$previous, [int]$maxLines) { function PrintLatestHeadlines([xml]$content, [string]$latestTimestamp) {
[xml]$content = (Invoke-WebRequest -URI $URL -useBasicParsing).Content $items = $content.rss.channel.item
[string]$latest = "" [array]::Reverse($items)
foreach($item in $items) {
foreach ($item in $content.rss.channel.item) { if ($($item.pubDate) -le $latestTimestamp) { continue }
$title = $item.title $title = $item.title
if ($latest -eq "") { $latest = $title }
if ($title -eq $previous) { break }
$time = $item.pubDate.Substring(11, 5) $time = $item.pubDate.Substring(11, 5)
& "$PSScriptRoot/write-typewriter.ps1" "❇️ $time $title" 10 & "$PSScriptRoot/write-typewriter.ps1" "❇️ $time $title" 2
$maxLines-- $latestTimestamp = $item.pubDate
if ($maxLines -eq 0) { break }
} }
return $latest return $latestTimestamp
} }
try { try {
[xml]$content = (Invoke-WebRequest -URI $URL -useBasicParsing).Content [xml]$content = (Invoke-WebRequest -URI $URL -useBasicParsing).Content
$title = $content.rss.channel.title $title = $content.rss.channel.title
$URL = $content.rss.channel.link $link = $content.rss.channel.link
" " " "
" UTC $title - $URL" " UTC $title - $link"
" --- -----------------------------------------------------------------------" " --- -----------------------------------------------------------------------"
$latest = "" $latestTimestamp = "2000-01-01"
do { do {
$latest = PrintLatestHeadlines $latest $lines $latestTimestamp = PrintLatestHeadlines $content $latestTimestamp
Start-Sleep -seconds $timeInterval Start-Sleep -seconds $timeInterval
[xml]$content = (Invoke-WebRequest -URI $URL -useBasicParsing).Content
} while ($true) } while ($true)
exit 0 # success exit 0 # success
} catch { } catch {