PowerShell/scripts/watch-news.ps1

58 lines
1.9 KiB
PowerShell
Raw Normal View History

2024-10-01 15:24:16 +02:00
<#
2024-05-16 13:14:01 +02:00
.SYNOPSIS
2024-11-04 17:09:39 +01:00
Watch the news
2024-05-16 13:14:01 +02:00
.DESCRIPTION
2024-11-04 17:09:39 +01:00
This PowerShell script continuously lists the latest headlines by using a RSS (Really Simple Syndication) feed.
.PARAMETER URL
2024-05-16 13:14:01 +02:00
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-11-04 17:09:39 +01:00
Specifies the time interval in seconds between two Web requests (30 seconds by default)
2024-05-16 13:14:01 +02:00
.EXAMPLE
2024-05-21 21:37:41 +02:00
PS> ./watch-news.ps1
2024-11-04 17:09:39 +01:00
UTC Yahoo News - Latest News & Headlines - https://www.yahoo.com/news/world
--- -----------------------------------------------------------------------
14:29 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-11-04 21:21:53 +01:00
param([string]$URL = "https://news.yahoo.com/rss/world", [int]$timeInterval = 30) # in seconds
2024-05-16 13:14:01 +02:00
2024-11-04 21:21:53 +01:00
function PrintLatestHeadlines([xml]$content, [string]$latestTimestamp) {
$items = $content.rss.channel.item
[array]::Reverse($items)
foreach($item in $items) {
if ($($item.pubDate) -le $latestTimestamp) { continue }
2024-11-04 17:09:39 +01:00
$title = $item.title
$time = $item.pubDate.Substring(11, 5)
2024-11-04 21:21:53 +01:00
& "$PSScriptRoot/write-typewriter.ps1" "❇️ $time $title" 2
$latestTimestamp = $item.pubDate
2024-05-21 21:37:41 +02:00
}
2024-11-04 21:21:53 +01:00
return $latestTimestamp
2024-05-16 17:25:44 +02:00
}
try {
2024-11-04 17:09:39 +01:00
[xml]$content = (Invoke-WebRequest -URI $URL -useBasicParsing).Content
$title = $content.rss.channel.title
2024-11-04 21:21:53 +01:00
$link = $content.rss.channel.link
2024-11-04 17:09:39 +01:00
" "
2024-11-04 21:21:53 +01:00
" UTC $title - $link"
2024-11-04 17:09:39 +01:00
" --- -----------------------------------------------------------------------"
2024-11-04 21:21:53 +01:00
$latestTimestamp = "2000-01-01"
2024-11-04 17:09:39 +01:00
do {
2024-11-04 21:21:53 +01:00
$latestTimestamp = PrintLatestHeadlines $content $latestTimestamp
2024-06-13 12:01:41 +02:00
Start-Sleep -seconds $timeInterval
2024-11-04 21:21:53 +01:00
[xml]$content = (Invoke-WebRequest -URI $URL -useBasicParsing).Content
2024-11-04 17:09:39 +01:00
} while ($true)
2024-05-16 13:14:01 +02:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}