PowerShell/docs/watch-news.md
2024-11-08 12:38:20 +01:00

119 lines
3.5 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 (30 seconds by default)
Required? false
Position? 2
Default value 30
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 Yahoo News - Latest News & Headlines - 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 (30 seconds by default)
.EXAMPLE
PS> ./watch-news.ps1
UTC Yahoo News - Latest News & Headlines - 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 = 30) # in seconds
function PrintLatestHeadlines([xml]$content, [string]$latestTimestamp) {
$items = $content.rss.channel.item
[array]::Reverse($items)
foreach($item in $items) {
if ($($item.pubDate) -le $latestTimestamp) { continue }
$title = $item.title
$time = $item.pubDate.Substring(11, 5)
& "$PSScriptRoot/write-typewriter.ps1" "❇️ $time $title" 2
$latestTimestamp = $item.pubDate
}
return $latestTimestamp
}
try {
[xml]$content = (Invoke-WebRequest -URI $URL -useBasicParsing).Content
$title = $content.rss.channel.title
$link = $content.rss.channel.link
" "
" UTC $title - $link"
" --- -----------------------------------------------------------------------"
$latestTimestamp = "2000-01-01"
do {
$latestTimestamp = PrintLatestHeadlines $content $latestTimestamp
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 using the comment-based help of watch-news.ps1 as of 11/08/2024 12:37:57)*