You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

233 lines
8.9 KiB

param(
$Path,
$Index = "H:\AnimeLib",
$VarConfig,
$RecorDB = "animelibk.ini",
$Include = @(".rmvb", ".mp4", ".mkv", ".avi"),
[switch] $Single,
[switch] $Force,
[switch] $Sub,
[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 + "\NameRegex.psd1")
Edit-PathChar $Path
#$TargetName = "俺だけ入れる隠しダンジョン〜こっそり鍛えて世界最強〜";
$TargetName = ""
#$DestinName = "真の仲間"
Write-Output $Path
#$Index = "L:\AnimeLib"
# scriptblock 減少重複
$RegexpSet = {
# 全域化
# 因應檔名不同而設定多組正規方式
Set-Variable -Name "EpisodeRegex" -Scope global -Value @(
"(?:S\d{2})?[\s]?-[\s]E?(?<Episode>\d{2})"
"[\[\b\s](?<Episode>\d{2})[\s_]?(?:[vV]\d|END|FIN)[\]\b\s]"
"\[(?<Episode>\d{1,2})\D{2}\]"
"\[(?<Episode>\d{1,2})\]"
)
Set-Variable -Name "NameRegex" -Scope global -Value @(
"[\[\b\s](?<Name>[^\[\]]*)[\]\b\s]?$"
)
}
if ($VarConfig) {
if (Test-Path $PSScriptRoot\$VarConfig) {
$T = Get-Content -Encoding UTF8 $PSScriptRoot\$VarConfig | % { $H = @{} ; $S = $null} {
if ($_ -match "^\[(?<T>\w*)\]" ) {
$S = $Matches["T"]
$H.add($S, @())
} elseif ($_ -match ".+" ) {
$H[$S] = $H[$S] + @($_ -replace '"', '')
}
} { $H ; Clear-Variable -Name "H"}
$T.GetEnumerator() | % { Set-Variable -Name $_.name -Value $_.value}
} else {
&$RegexpSet
$EpisodeRegex | % {"[EpisodeRegex]"}{ """$_""" }{""} | Out-File -Encoding UTF8 $PSScriptRoot\$VarConfig
$NameRegex | % {"[NameRegex]"}{ """$_""" } | Out-File -Encoding UTF8 -Append $PSScriptRoot\$VarConfig
}
} else {
&$RegexpSet
}
#$Path = "L:\Anime\202001 ネコぱら\[Nekomoe kissaten][Nekopara][01-12][BDRip][1080p][CHT]\[Nekomoe kissaten][Nekopara][12][1080p][CHT].mp4"
#$Path = "L:\Anime\202004 ストライク・ザ・ブラッド OVA IV\[Suzu-Kaze][Strike_the_Blood_IV_OVA][01-12FIN][BDRip_1080P][HEVC_YUV420P10]\[Suzu-Kaze] Strike the Blood IV 12FIN [BDRip 1920x1080 HEVC YUV420P10 FLAC].mkv"
#$Path = "H:\Anime\201901 盾の勇者の成り上がり\[LKSUB][Tate no Yuusha no Nariagari][01-25][GB][720P][MP4]\[LKSUB][Tate no Yuusha no Nariagari][01][GB][720P].mp4"
$JsonPath = "$Index\Anime\* $TargetName", $($Path -replace '(.*\\[^\[\\\]]*)\\\[.*', '$1') | ? {Test-Path $_}
if ($RecorDB -notmatch ":") { $RecorDB = "$JsonPath\$RecorDB" }
$WorkData = $(if (Test-Path $RecorDB) {
Get-Content $RecorDB | ConvertFrom-Json
} else {
""
}) | Select-Object ID, WorkName, TargetName, DestinName, Latinization, Standard, Season, Episode
$RecorDB
# 確認資料狀態,缺少則進行物件資料建立
if (-not($WorkData.WorkName)) {
$WorkData.WorkName = Read-Host "請輸入作品名稱:";
}
if (-not($WorkData.TargetName)) {
$WorkData.TargetName = $($JsonPath -replace '.*\\[0-9\s]*(.*)$', '$1');
}
if (-not($WorkData.DestinName)) {
$WorkData.DestinName = Read-Host "請輸入目的地名稱:";
}
if (-not($WorkData.Season)) {
[int] $WorkData.Season = Read-Host "請輸入季別:";
}
if (-not($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
# $Seasons_TC = "", "Seasons 1", "Seasons 2", "Seasons 3", "Seasons 4", "Seasons 5"
$Lang = @{
Season = "Season {0:00}"
}
$Year = @($($JsonPath -replace '.*\\((?<Y>\d{4})?(?<M>\d{2})?).*$', '${Y}'), $(get-date -Format 'yyyy')) | ? {-not([string]::IsNullOrEmpty($_))} | Select -First 1
# 依目標檔案位置變更磁碟代號
$TargetDrive = Split-Path -Path $JsonPath -Qualifier
if (Split-Path -Path $Index -IsAbsolute) {
if ($Index -notmatch $TargetDrive) {
$Index2 = Join-Path -Path $TargetDrive -ChildPath (Split-Path -Path $Index -noQualifier)
} else {
$Index2 = $Index
}
} else {
$Index2 = Join-Path $TargetDrive $Index
}
Write-Host $Index2
# 取得目標資料夾是否存在,不存在建立資料夾,並取得資料夾物件
$LinkPath = "{2}\{0}\{1}" -f $WorkData.DestinName, $($Lang["Season"] -f $WorkData.Season), $Index2
$LinkDir = $(if (-not(Test-Path -Path "$LinkPath*")) {
New-Item -Path "$LinkPath ($Year)" -ItemType Directory -Force
} else {
Get-Item -Path "$LinkPath*"
})
#$DestDir = $(New-Item -Path ".\$WorkData.DestinName\$($Seasons_TC[$WorkData.Season]) ($Year)" -ItemType Directory -Force)
function Get-StandName {
param (
$FileObj
)
# 進行傳入變數判斷,非 FileInfo 物件則視為 String,先進特殊字元處理,再取得物件
$File = $(if ($FileObj.GetType().Name -eq 'FileInfo') {
$FileObj
} else {
Get-Item -Path $(Edit-PathChar $FileObj)
})
$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-Object newName, Season, Episode, newFullName;
$OutputData.newName = if ($LS) { $WorkData.Latinization } else { $Result.Name.Trim() }
$OutputData.Season = $WorkData.Season;
$OutputData.Episode = [int] $Result.Episode;
$OutputData.newFullName = "{0} S{3:00}E{1:00}{4}{2}" -f $OutputData.newName, $OutputData.Episode, $File.Extension, $WorkData.Season, $(if($File.Extension -in $SubExt) {".default"});
return $OutputData;
}
### Select Object Mode START ###
# 建立自定屬性 File 和運算公式
$FileObj = @{
label = "File";
Expression = {$_};
}
# 建立自定屬性 NewName 和運算公式
$NewNameObj = @{
label = "NewName";
Expression = {Get-StandName $_};
}
$StructPathObj = @{
label = "StructPath";
#Expression = {$(New-Item -Path $(".\{0}\{1} ({2})" -f $WorkData.DestinName, $Seasons_TC[$WorkData.Season], $Year) -ItemType Directory -Force).FullName};
Expression = {$LinkDir};
}
### # Select Object Mode END ###
# 運作模式
if ($Single) {
# 單集模式
$List = @(Get-Item -Path $(Edit-PathChar $Path))
$BuildList = $List | Select-Object $FileObj, $StructPathObj, $NewNameObj | ? {$_.NewName.Episode -notin $WorkData.Episode}
#$BuildList | ? {$_.NewName.Episode -notin $WorkData.Episode} | Format-List
# } elseif ($Sub) {
# $List = @(Get-Item -Path $(Edit-PathChar $Path))
# $BuildList = $List | select $FileObj, $StructPathObj, $NewNameObj | ? {$_.File.Extension -in @(".ass", ".ssa")}
} elseif ($testrun) {
$List = @(Edit-PathChar $Path | Get-Item | ? { $_.GetType().Name -eq "FileInfo" }) +
@(Edit-PathChar $Path | Get-Item | ? { $_.GetType().Name -eq "DirectoryInfo" } | % { $_.GetFiles() })
$BuildList = $List | Select-Object $FileObj, $StructPathObj, $NewNameObj
if ($Include -ne "") { $BuildList = $BuildList | ? {$_.File.Extension -in $Include} }
if (-not($Force)) { $BuildList = $BuildList | ? {$_.NewName.Episode -notin $WorkData.Episode} }
} else {
exit 0;
}
$ENDResult = $BuildList | ForEach-Object {
if ($_.NewName.Episode -and ($_.NewName.newName.Length -gt 0)) {
$Command = @{
ItemType = 'HardLink'
Path = $("{0}\{1}" -f $_.StructPath, $_.NewName.newFullName)
Target = $(Edit-PathChar $_.File.Fullname -nowlid)
}
#try {
if (New-Item @Command) {
Write-Host $_.NewName.newFullName
$_.NewName }
#} catch {
} else {
"{0} 處理錯誤" -f $_.File.FullName | Write-Error
$_.NewName
}
}
#$ENDResult
#$ENDResult.gettype();
#if (-not($Sub)) { $ENDResult | % { if ($_.Episode) { $WorkData.Episode += $_.Episode }}}
$ENDResult | % { if ($_.Episode) { $WorkData.Episode += $_.Episode }}
$WorkData | ConvertTo-Json | Out-File -Encoding "UTF8" $RecorDB
exit 0;