0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

最新のニュースをPCでしゃべらす。(PowerShell)

0
Posted at

はじめに

PCで最新のニュースをPCでしゃべらす。
5分周期で監視する。
02:00 ~ 08:00 は読み上げをスキップします。
プログラムはPowerShellで作成

最新のニュースをPCでしゃべらす(news_reader.ps1)

# --- 設定 ---
$RSS_URL = "https://news.google.com/rss?hl=ja&gl=JP&ceid=JP:ja"
$HISTORY_FILE = "news_history.txt"
$CHECK_INTERVAL = 300

# 読み上げ設定
Add-Type -AssemblyName System.Speech
$Synthesizer = New-Object System.Speech.Synthesis.SpeechSynthesizer

function Get-NewsHistory {
    if (Test-Path $HISTORY_FILE) {
        return Get-Content $HISTORY_FILE -Encoding UTF8
    }
    return @()
}

function Save-NewsHistory($title) {
    $title | Out-File -FilePath $HISTORY_FILE -Append -Encoding UTF8
}

Write-Host "?? ニュース監視を開始しました($CHECK_INTERVAL 秒おき)" -ForegroundColor Cyan
Write-Host "?? 02:00 ~ 08:00 は読み上げをスキップします。" -ForegroundColor Gray

while ($true) {
    $history = Get-NewsHistory
    
    # 現在の時間を取得 (24時間制)
    $currentHour = (Get-Date).Hour
    # 2時以上、かつ 8時未満(7:59まで)は消音モード
    $isSilentTime = ($currentHour -ge 2 -and $currentHour -lt 8)

    try {
        $xmlData = Invoke-WebRequest -Uri $RSS_URL -UseBasicParsing
        [xml]$feed = $xmlData.Content
        $items = $feed.SelectNodes("//item") | Select-Object -First 10
        
        if ($null -ne $items) {
            [array]::Reverse($items)
            $newNewsCount = 0

            foreach ($item in $items) {
                $rawTitle = $item.title
                if ($rawTitle -match " - [^ -]+$") {
                    $rawTitle = $rawTitle -replace " - [^ -]+$", ""
                }

                if ($history -notcontains $rawTitle) {
                    $cleanTitle = $rawTitle.Replace(" ", "。").Replace(" ", "。")
                    
                    # 画面表示(時間は関係なく表示)
                    Write-Host "? 新着: $rawTitle" -ForegroundColor Yellow
                    
                    # 読み上げ判定
                    if (-not $isSilentTime) {
                        $Synthesizer.Speak("新着ニュースです。$cleanTitle")
                    } else {
                        Write-Host "   (?? 深夜・早朝のため読み上げスキップ)" -ForegroundColor Gray
                    }

                    Save-NewsHistory $rawTitle
                    $history += $rawTitle
                    $newNewsCount++
                    Start-Sleep -Seconds 1
                }
            }

            if ($newNewsCount -eq 0) {
                $currentTime = Get-Date -Format "HH:mm:ss"
                $status = if ($isSilentTime) { "消音モード中" } else { "待機中" }
                Write-Host "?? 新着なし ($currentTime / $status)" -ForegroundColor Gray
            }
        }
    }
    catch {
        Write-Host "? エラー: $($_.Exception.Message)" -ForegroundColor Red
    }

    Start-Sleep -Seconds $CHECK_INTERVAL
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?