PowerShell/docs/watch-news.md
2024-11-20 11:52:20 +01:00

125 lines
3.6 KiB
Markdown

The *watch-news.ps1* Script
===========================
This PowerShell script continuously lists the latest headlines by using a RSS (Really Simple Syndication) feed.
Parameters
----------
```powershell
/home/markus/Repos/PowerShell/scripts/watch-news.ps1 [[-URL] <String>] [[-timeInterval] <Int32>] [<CommonParameters>]
-URL <String>
Specifies the URL to the RSS feed (Yahoo World News by default)
Required? false
Position? 1
Default value https://news.yahoo.com/rss/world
Accept pipeline input? false
Accept wildcard characters? false
-timeInterval <Int32>
Specifies the time interval in seconds between two Web requests (60 seconds by default)
Required? false
Position? 2
Default value 60
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
Example
-------
```powershell
PS> ./watch-news.ps1
UTC HEADLINES (by: https://www.yahoo.com/news/world)
--- ------------------------------------------------
14:29 Niger coup: Ecowas deadline sparks anxiety in northern Nigeria
...
```
Notes
-----
Author: Markus Fleschutz | License: CC0
Related Links
-------------
https://github.com/fleschutz/PowerShell
Script Content
--------------
```powershell
<#
.SYNOPSIS
Watch the news
.DESCRIPTION
This PowerShell script continuously lists the latest headlines by using a RSS (Really Simple Syndication) feed.
.PARAMETER URL
Specifies the URL to the RSS feed (Yahoo World News by default)
.PARAMETER lines
Specifies the initial number of headlines
.PARAMETER timeInterval
Specifies the time interval in seconds between two Web requests (60 seconds by default)
.EXAMPLE
PS> ./watch-news.ps1
UTC HEADLINES (by: https://www.yahoo.com/news/world)
--- ------------------------------------------------
14:29 Niger coup: Ecowas deadline sparks anxiety in northern Nigeria
...
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$URL = "https://news.yahoo.com/rss/world", [int]$timeInterval = 60) # in seconds
function PrintLatestHeadlines([xml]$content, [string]$latestTimestamp, [string]$icon) {
$items = $content.rss.channel.item
[array]::Reverse($items)
$newLatest = $latestTimestamp
foreach($item in $items) {
$pubDate = $item.pubDate
if ($pubDate -le $latestTimestamp) { continue }
$title = $item.title
$time = $pubDate.Substring(11, 5)
Write-Host "$time $title$icon"
Start-Sleep -milliseconds 500
if ($pubDate -gt $newLatest) { $newLatest = $pubDate }
}
return $newLatest
}
try {
[xml]$content = (Invoke-WebRequest -URI $URL -useBasicParsing).Content
$title = $content.rss.channel.title.toUpper()
$link = $content.rss.channel.link
Write-Host "`n UTC HEADLINES (by: " -noNewline
Write-Host $link -foregroundColor blue -noNewline
Write-Host ")"
Write-Host " --- ------------------------------------------------"
$latestTimestamp = "2000-01-01"
$icon = ""
do {
$latestTimestamp = PrintLatestHeadlines $content $latestTimestamp $icon
$icon = "🆕"
Start-Sleep -seconds $timeInterval
[xml]$content = (Invoke-WebRequest -URI $URL -useBasicParsing).Content
} while ($true)
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
```
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:52:01)*