PowerShell/docs/watch-news.md

119 lines
3.5 KiB
Markdown
Raw Normal View History

2024-05-19 10:25:56 +02:00
Script: *watch-news.ps1*
========================
2024-11-08 12:35:11 +01:00
This PowerShell script continuously lists the latest headlines by using a RSS (Really Simple Syndication) feed.
2024-05-19 10:25:56 +02:00
Parameters
----------
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/watch-news.ps1 [[-URL] <String>] [[-timeInterval] <Int32>] [<CommonParameters>]
2024-05-19 10:25:56 +02:00
2024-11-08 12:35:11 +01:00
-URL <String>
2024-05-19 10:25:56 +02:00
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
2024-08-15 09:51:46 +02:00
-timeInterval <Int32>
2024-11-08 12:35:11 +01:00
Specifies the time interval in seconds between two Web requests (30 seconds by default)
2024-08-15 09:51:46 +02:00
Required? false
2024-11-08 12:35:11 +01:00
Position? 2
2024-08-15 09:51:46 +02:00
Default value 30
2024-05-19 10:25:56 +02:00
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
2024-08-15 09:51:46 +02:00
PS> ./watch-news.ps1
2024-11-08 12:35:11 +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-19 10:25:56 +02:00
```
Notes
-----
Author: Markus Fleschutz | License: CC0
Related Links
-------------
https://github.com/fleschutz/PowerShell
Script Content
--------------
```powershell
<#
.SYNOPSIS
2024-11-08 12:35:11 +01:00
Watch the news
2024-05-19 10:25:56 +02:00
.DESCRIPTION
2024-11-08 12:35:11 +01:00
This PowerShell script continuously lists the latest headlines by using a RSS (Really Simple Syndication) feed.
.PARAMETER URL
2024-05-19 10:25:56 +02:00
Specifies the URL to the RSS feed (Yahoo World News by default)
2024-08-15 09:51:46 +02:00
.PARAMETER lines
Specifies the initial number of headlines
.PARAMETER timeInterval
2024-11-08 12:35:11 +01:00
Specifies the time interval in seconds between two Web requests (30 seconds by default)
2024-05-19 10:25:56 +02:00
.EXAMPLE
2024-08-15 09:51:46 +02:00
PS> ./watch-news.ps1
2024-11-08 12:35:11 +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-19 10:25:56 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-11-08 12:35:11 +01:00
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
2024-08-15 09:51:46 +02:00
}
2024-11-08 12:35:11 +01:00
return $latestTimestamp
2024-05-19 10:25:56 +02:00
}
try {
2024-11-08 12:35:11 +01:00
[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
2024-08-15 09:51:46 +02:00
Start-Sleep -seconds $timeInterval
2024-11-08 12:35:11 +01:00
[xml]$content = (Invoke-WebRequest -URI $URL -useBasicParsing).Content
} while ($true)
2024-05-19 10:25:56 +02:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
```
2024-11-08 12:35:11 +01:00
*(generated by convert-ps2md.ps1 using the comment-based help of watch-news.ps1 as of 11/08/2024 12:34:56)*