Список фильмов и сериалов
Хранит watch list в JSON, фильтрует по статусу и выбирает случайный непросмотренный пункт.
Что делает скрипт
Запись содержит название, год, тип, статус и оценку. Команды добавляют, обновляют, удаляют и случайно выбирают фильм или сериал на вечер. Файл обновляется с резервной копией.
- Файл watch list изменяется при добавлении и обновлении, создаётся копия .bak.
Быстрый старт
powershell -File WatchList.ps1 -Action Add -Title "Пример фильма" -Year 2024 -Type film
Скачайте файл, откройте терминал в папке со скриптом и выполните команду. Запускайте в PowerShell 5.1 или PowerShell 7.
Другие варианты
powershell -File WatchList.ps1 -Action Add -Title "Пример фильма" -Year 2024 -Type filmpowershell -File WatchList.ps1 -Action Listpowershell -File WatchList.ps1 -Action Random
Что понадобится
Встроен в Windows 10/11 (PowerShell 5.1)
WatchList.ps1
[CmdletBinding()]
param(
[ValidateSet('Add', 'List', 'Update', 'Remove', 'Random')]
[string]$Action = 'List',
[string]$Title,
[int]$Year = 0,
[ValidateSet('film', 'series')]
[string]$Type = 'film',
[ValidateSet('planned', 'watching', 'watched')]
[string]$Status = 'planned',
[int]$Rating = 0,
[int]$Id,
[string]$Data = (Join-Path $env:APPDATA 'Productivity\watchlist.json'),
[switch]$Force
)
$ErrorActionPreference = 'Stop'
function Read-Items {
param([string]$Path)
if (-not (Test-Path -LiteralPath $Path)) { return @() }
$raw = Get-Content -LiteralPath $Path -Raw -Encoding UTF8
if ([string]::IsNullOrWhiteSpace($raw)) { return @() }
$items = $raw | ConvertFrom-Json
if ($null -eq $items) { return @() }
return @($items)
}
function Save-Items {
param([string]$Path, [object[]]$Items)
$directory = Split-Path -LiteralPath $Path
if ($directory -and -not (Test-Path -LiteralPath $directory)) {
New-Item -ItemType Directory -Path $directory -Force | Out-Null
}
if (Test-Path -LiteralPath $Path) {
Copy-Item -LiteralPath $Path -Destination ($Path + '.bak') -Force
}
$json = ConvertTo-Json -InputObject @($Items) -Depth 5
Set-Content -LiteralPath $Path -Value $json -Encoding UTF8
}
$items = Read-Items -Path $Data
switch ($Action) {
'Add' {
if ([string]::IsNullOrWhiteSpace($Title)) { throw 'Укажите название через -Title.' }
if ($Year -lt 0 -or $Year -gt 2100) { throw 'Год должен быть в диапазоне 0..2100.' }
if ($Rating -lt 0 -or $Rating -gt 10) { throw 'Оценка должна быть от 0 до 10.' }
$nextId = 1
if ($items.Count -gt 0) {
$nextId = ([int]($items | Measure-Object -Property id -Maximum).Maximum) + 1
}
$entry = [pscustomobject]@{
id = $nextId
title = $Title
year = $Year
type = $Type
status = $Status
rating = $Rating
}
$items = @($items) + $entry
Save-Items -Path $Data -Items $items
Write-Host "Добавлено #${nextId}: $Title"
}
'List' {
if ($items.Count -eq 0) { Write-Host 'Список пуст.'; break }
$items | Sort-Object -Property id | ForEach-Object {
$ratingText = if ($_.rating -gt 0) { "$($_.rating)/10" } else { 'без оценки' }
Write-Host ("#{0} [{1}] {2} ({3}) - {4}" -f $_.id, $_.status, $_.title, $_.year, $ratingText)
}
}
'Update' {
if ($Id -le 0) { throw 'Укажите номер через -Id.' }
if ($Rating -lt 0 -or $Rating -gt 10) { throw 'Оценка должна быть от 0 до 10.' }
$found = $false
foreach ($item in $items) {
if ([int]$item.id -eq $Id) {
if ($PSBoundParameters.ContainsKey('Status')) { $item.status = $Status }
if ($PSBoundParameters.ContainsKey('Rating') -and $Rating -gt 0) { $item.rating = $Rating }
$found = $true
}
}
if (-not $found) { throw "Запись #$Id не найдена." }
Save-Items -Path $Data -Items $items
Write-Host "Обновлено: #$Id"
}
'Remove' {
if ($Id -le 0) { throw 'Укажите номер через -Id.' }
if (-not $Force) { throw 'Удаление меняет файл. Добавьте -Force для подтверждения.' }
$kept = @($items | Where-Object { [int]$_.id -ne $Id })
if ($kept.Count -eq $items.Count) { throw "Запись #$Id не найдена." }
Save-Items -Path $Data -Items $kept
Write-Host "Удалено: #$Id"
}
'Random' {
$planned = @($items | Where-Object { $_.status -eq 'planned' })
if ($planned.Count -eq 0) { Write-Host 'Нет запланированных фильмов или сериалов.'; break }
$choice = $planned | Get-Random
Write-Host ("На вечер: {0} ({1}), {2}" -f $choice.title, $choice.year, $choice.type)
}
}
Скрипты работают с файлами и настройками системы. Перед запуском прочитайте код и проверьте пути. Особенно если скрипт удаляет, перезаписывает или перемещает данные.