Updated watch-news.ps1

This commit is contained in:
Markus Fleschutz 2024-11-13 11:47:48 +01:00
parent 430074a982
commit 6e27c12b46

View File

@ -8,13 +8,13 @@
.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 two Web requests (30 seconds by default) Specifies the time interval in seconds between two Web requests (60 seconds by default)
.EXAMPLE .EXAMPLE
PS> ./watch-news.ps1 PS> ./watch-news.ps1
UTC Yahoo News - Latest News & Headlines - https://www.yahoo.com/news/world UTC Yahoo News - Latest News & Headlines - https://www.yahoo.com/news/world
--- ----------------------------------------------------------------------- --- -----------------------------------------------------------------------
14:29 Niger coup: Ecowas deadline sparks anxiety in northern Nigeria 14:29 Niger coup: Ecowas deadline sparks anxiety in northern Nigeria
... ...
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
@ -22,31 +22,36 @@
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
param([string]$URL = "https://news.yahoo.com/rss/world", [int]$timeInterval = 30) # in seconds param([string]$URL = "https://news.yahoo.com/rss/world", [int]$timeInterval = 60) # in seconds
function PrintLatestHeadlines([xml]$content, [string]$latestTimestamp) { function PrintLatestHeadlines([xml]$content, [string]$latestTimestamp, [string]$icon) {
$items = $content.rss.channel.item $items = $content.rss.channel.item
[array]::Reverse($items) [array]::Reverse($items)
$newLatest = $latestTimestamp
foreach($item in $items) { foreach($item in $items) {
if ($($item.pubDate) -le $latestTimestamp) { continue } $pubDate = $item.pubDate
if ($pubDate -le $latestTimestamp) { continue }
$title = $item.title $title = $item.title
$time = $item.pubDate.Substring(11, 5) $time = $pubDate.Substring(11, 5)
& "$PSScriptRoot/write-typewriter.ps1" "❇️ $time $title" 2 Write-Host "$time $title$icon"
$latestTimestamp = $item.pubDate Start-Sleep -milliseconds 500
if ($pubDate -gt $newLatest) { $newLatest = $pubDate }
} }
return $latestTimestamp return $newLatest
} }
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
$link = $content.rss.channel.link $link = $content.rss.channel.link
" " Write-Host "`n UTC $title - " -noNewline
" UTC $title - $link" Write-Host $link -foregroundColor blue
" --- -----------------------------------------------------------------------" Write-Host " --- -----------------------------------------------------------------------"
$latestTimestamp = "2000-01-01" $latestTimestamp = "2000-01-01"
$icon = ""
do { do {
$latestTimestamp = PrintLatestHeadlines $content $latestTimestamp $latestTimestamp = PrintLatestHeadlines $content $latestTimestamp $icon
$icon = "🆕"
Start-Sleep -seconds $timeInterval Start-Sleep -seconds $timeInterval
[xml]$content = (Invoke-WebRequest -URI $URL -useBasicParsing).Content [xml]$content = (Invoke-WebRequest -URI $URL -useBasicParsing).Content
} while ($true) } while ($true)