Compare commits

..

18 Commits

  1. 96
      Animelibk.ps1

96
Animelibk.ps1

@ -3,12 +3,17 @@
$Index = "H:\AnimeLib", $Index = "H:\AnimeLib",
$VarConfig, $VarConfig,
$RecorDB = "animelibk.ini", $RecorDB = "animelibk.ini",
$Include = @(".rmvb", ".mp4", ".mkv", ".avi", ".ass", ".ssa"), $Include = @(".rmvb", ".mp4", ".mkv", ".avi"),
[switch] $Single, [switch] $Single,
[switch] $Force, [switch] $Force,
[switch] $Sub, [switch] $Sub,
[switch] $testrun [switch] $testrun
) )
#$Include = ".rmvb", ".mp4", ".mkv", ".avi"
$SubExt = if ($Sub) { ".ssa", ".ass", ".srt" }
$Include = $Include + $SubExt
Import-Module -Name ($PSScriptRoot + "\PathChar.psd1") Import-Module -Name ($PSScriptRoot + "\PathChar.psd1")
Import-Module -Name ($PSScriptRoot + "\NameRegex.psd1") Import-Module -Name ($PSScriptRoot + "\NameRegex.psd1")
Edit-PathChar $Path Edit-PathChar $Path
@ -62,8 +67,8 @@ if ($RecorDB -notmatch ":") { $RecorDB = "$JsonPath\$RecorDB" }
$WorkData = $(if (Test-Path $RecorDB) { $WorkData = $(if (Test-Path $RecorDB) {
Get-Content $RecorDB | ConvertFrom-Json Get-Content $RecorDB | ConvertFrom-Json
} else { } else {
"" | Select-Object ID, WorkName, TargetName, DestinName, Season, Episode ""
}) }) | Select-Object ID, WorkName, TargetName, DestinName, Latinization, Standard, Season, Offset, Episode
$RecorDB $RecorDB
# 確認資料狀態,缺少則進行物件資料建立 # 確認資料狀態,缺少則進行物件資料建立
@ -79,13 +84,40 @@ if (-not($WorkData.DestinName)) {
if (-not($WorkData.Season)) { if (-not($WorkData.Season)) {
[int] $WorkData.Season = Read-Host "請輸入季別:"; [int] $WorkData.Season = Read-Host "請輸入季別:";
} }
if (-not($WorkData.Offset)) {
[int] $WorkData.Offset = if ($WorkData.Season -gt 1) {
Read-Host "請輸入集數偏移修正";
} else {
0
}
}
if (-not($WorkData.Episode)) { if (-not($WorkData.Episode)) {
$WorkData.Episode = @(); $WorkData.Episode = @();
} }
# Latinization
$Latinization = {
param($String)
# 載入置換檔
$C = if (!$WorkData.Standard -and (Test-path $PSScriptRoot\C2E.csv)) {Import-Csv -Encoding UTF8 $PSScriptRoot\C2E.csv}
if ($WorkData.Latinization.Length -eq 0) {
$T = Read-Host "請輸入拉丁化作品名稱"
# 常見字詞置換
$N = $C | ForEach-Object {$String = $String -replace $_.C, $_.EN}
# 尋找非拉丁字元,以產生取代規則
$TString = $String
while($TString -match '[^\[\]\d\s\-\.A-Za-z]+') {
if ($RuleString.Length -gt 0) {$RuleString = $RuleString + '.+'}
$RuleString = $RuleString + $Matches[0]
$TString = $TString -replace $Matches[0], '-'
}
return ($String -replace "$RuleString\s?", "$T ").Trim()
}
}
$WorkData $WorkData
#$Seasons_TC = "", "第一季", "第二季", "第三季", "第四季", "第五季" # $Seasons_TC = "", "Seasons 1", "Seasons 2", "Seasons 3", "Seasons 4", "Seasons 5"
$Seasons_TC = "", "Seasons 1", "Seasons 2", "Seasons 3", "Seasons 4", "Seasons 5"
$Lang = @{ $Lang = @{
Season = "Season {0:00}" Season = "Season {0:00}"
} }
@ -126,12 +158,19 @@ function Get-StandName {
$Result = $File.Name | % { Get-NameRegex $_ -Regex $EpisodeRegex } | % { $(Get-NameRegex $_["L"] -Regex $NameRegex -Exclude "(BD|DVD|HDTV)RIP|BIG5" -End) + $_ } | Select -First 1 $Result = $File.Name | % { Get-NameRegex $_ -Regex $EpisodeRegex } | % { $(Get-NameRegex $_["L"] -Regex $NameRegex -Exclude "(BD|DVD|HDTV)RIP|BIG5" -End) + $_ } | Select -First 1
# 拉丁化、標準化
if ($LS = $WorkData.Standard -or ($Result.Name -match '[^\[\]\d\s\-\.A-Za-z]+')) {
if ($WorkData.Latinization.Length -eq 0) {
$WorkData.Latinization = &$Latinization -String $Result.Name.Trim()
}
}
# 建立回傳資料物件,以方便後續資料處理。 # 建立回傳資料物件,以方便後續資料處理。
$OutputData = "" | Select newName, Season, Episode, newFullName; $OutputData = "" | Select-Object newName, Season, Episode, newFullName;
$OutputData.newName = $Result.Name; $OutputData.newName = if ($LS) { $WorkData.Latinization } else { $Result.Name.Trim() }
$OutputData.Season = $WorkData.Season; $OutputData.Season = $WorkData.Season;
$OutputData.Episode = [int] $Result.Episode; $OutputData.Episode = [int] $Result.Episode;
$OutputData.newFullName = "{0} S{3:00}E{1}{4}{2}" -f $Result.Name, $Result.Episode, $File.Extension, $WorkData.Season, $(if($Sub) {".default"}); $OutputData.newFullName = "{0} S{3:00}E{1:00}{4}{2}" -f $OutputData.newName, $(if (($nE = $OutputData.Episode + $WorkData.Offset) -gt 0) { $nE } else { $OutputData.Episode }), $File.Extension, $WorkData.Season, $(if($File.Extension -in $SubExt) {".default"});
return $OutputData; return $OutputData;
} }
@ -160,37 +199,42 @@ $StructPathObj = @{
if ($Single) { if ($Single) {
# 單集模式 # 單集模式
$List = @(Get-Item -Path $(Edit-PathChar $Path)) $List = @(Get-Item -Path $(Edit-PathChar $Path))
$BuildList = $List | select $FileObj, $StructPathObj, $NewNameObj | ? {$_.NewName.Episode -notin $WorkData.Episode} $BuildList = $List | Select-Object $FileObj, $StructPathObj, $NewNameObj | ? {$_.NewName.Episode -notin $WorkData.Episode}
#$BuildList | ? {$_.NewName.Episode -notin $WorkData.Episode} | Format-List #$BuildList | ? {$_.NewName.Episode -notin $WorkData.Episode} | Format-List
} elseif ($Sub) { # } elseif ($Sub) {
$List = @(Get-Item -Path $(Edit-PathChar $Path)) # $List = @(Get-Item -Path $(Edit-PathChar $Path))
$BuildList = $List | select $FileObj, $StructPathObj, $NewNameObj | ? {$_.File.Extension -in @(".ass", ".ssa")} # $BuildList = $List | select $FileObj, $StructPathObj, $NewNameObj | ? {$_.File.Extension -in @(".ass", ".ssa")}
} elseif ($testrun) { } elseif ($testrun) {
$List = @(Edit-PathChar $Path | Get-Item | ? { $_.GetType().Name -eq "FileInfo" }) + $List = @(Edit-PathChar $Path | Get-Item | ? { $_.GetType().Name -eq "FileInfo" }) +
@(Edit-PathChar $Path | Get-Item | ? { $_.GetType().Name -eq "DirectoryInfo" } | % { $_.GetFiles() }) @(Edit-PathChar $Path | Get-Item | ? { $_.GetType().Name -eq "DirectoryInfo" } | % { $_.GetFiles() })
$BuildList = $List | select $FileObj, $StructPathObj, $NewNameObj $BuildList = $List | Select-Object $FileObj, $StructPathObj, $NewNameObj
if ($Include -ne "") { $BuildList = $BuildList | ? {$_.File.Extension -in $Include} } if ($Include -ne "") { $BuildList = $BuildList | ? {$_.File.Extension -in $Include} }
if (-not($Force)) { $BuildList = $BuildList | ? {$_.NewName.Episode -notin $WorkData.Episode} } if (-not($Force)) { $BuildList = $BuildList | ? {$_.NewName.Episode -notin $WorkData.Episode} }
} else { } else {
exit 0; exit 0;
} }
$_.NewName | Out-Host
$ENDResult = $BuildList | % { $ENDResult = $BuildList | ForEach-Object {
$Command = @{ if ($_.NewName.Episode -and ($_.NewName.newName.Length -gt 0)) {
ItemType = 'HardLink' $Command = @{
Path = $("{0}\{1}" -f $_.StructPath, $_.NewName.newFullName) ItemType = 'HardLink'
Target = $(Edit-PathChar $_.File.Fullname -nowlid) Path = $("{0}\{1}" -f $_.StructPath, $_.NewName.newFullName)
} Target = $(Edit-PathChar $_.File.Fullname -nowlid)
#try { }
if (New-Item @Command) { #try {
Write-Host $_.NewName.newFullName if (New-Item @Command) {
$_.NewName } Write-Host $_.NewName.newFullName
$_.NewName }
#} catch { #} catch {
#} } else {
"{0} 處理錯誤" -f $_.File.FullName | Write-Error
$_.NewName | Out-Host
}
} }
#$ENDResult #$ENDResult
#$ENDResult.gettype(); #$ENDResult.gettype();
if (-not($Sub)) { $ENDResult | % { if ($_.Episode) { $WorkData.Episode += $_.Episode }}} #if (-not($Sub)) { $ENDResult | % { if ($_.Episode) { $WorkData.Episode += $_.Episode }}}
$ENDResult | % { if ($_.Episode) { $WorkData.Episode += $_.Episode }}
$WorkData | ConvertTo-Json | Out-File -Encoding "UTF8" $RecorDB $WorkData | ConvertTo-Json | Out-File -Encoding "UTF8" $RecorDB
exit 0; exit 0;

Loading…
Cancel
Save