Compare commits

...

11 Commits

Author SHA1 Message Date
pcnick 1f8e9f42dd 修正錯誤 9 months ago
pcnick e31505173a 依目標檔案位置變更目錄 9 months ago
pcnick ebc972ba16 修正規則檔路徑錯誤 1 year ago
pcnick 1862772fdf 運行模式修正 1 year ago
pcnick de3298579e 修正規則檔路徑錯誤 1 year ago
pcnick 780e8d1d2f 運作模式新增視輸入檔案屬性進行連續處理 1 year ago
pcnick 51dae0f48e 以參數彈性設定資料檔外部化 1 year ago
pcnick 274c93c72f 正規表示設定與指令檔分離,方便出錯時修改 1 year ago
pcnick 555e3a828f Merge tag 'v0.2.2' into develop 3 years ago
pcnick 3f506e4872 Merge tag 'v0.2.1' into develop 3 years ago
pcnick d96757e39a Merge tag 'v0.2.0' into develop 3 years ago
  1. 134
      Animelibk.ps1
  2. 8
      RegexRule.ini

134
Animelibk.ps1

@ -1,9 +1,13 @@
param(
$Path,
$Index = "H:\AnimeLib",
$VarConfig,
$RecorDB = "animelibk.ini",
$Include = @(".rmvb", ".mp4", ".mkv", ".ass", ".ssa"),
[switch] $Single,
[switch] $Force,
[switch] $Sub
[switch] $Sub,
[switch] $testrun
)
Import-Module -Name ($PSScriptRoot + "\PathChar.psd1")
Import-Module -Name ($PSScriptRoot + "\NameRegex.psd1")
@ -13,16 +17,54 @@ $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"
#$Path = "H:\Data\Downloads\因為不是真正的夥伴而被逐出勇者隊伍\[Shin no Nakama][01][BIG5][1080P].mp4"
$JsonPath = $("$Index\Anime\* $TargetName", $($Path -replace '(.*\\[^\[\\\]]*)\\\[.*', '$1') | ? {Test-Path $_})
$WorkData = $(if (Test-Path "$JsonPath\animelibk.json") {
Get-Content "$JsonPath\animelibk.json" | ConvertFrom-Json
$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 ID, WorkName, TargetName, DestinName, Season, Episode
"" | Select-Object ID, WorkName, TargetName, DestinName, Season, Episode
})
$RecorDB
# 確認資料狀態,缺少則進行物件資料建立
if (-not($WorkData.WorkName)) {
@ -42,11 +84,26 @@ if (-not($WorkData.Episode)) {
}
$WorkData
$Seasons_TC = "", "第一季", "第二季", "第三季", "第四季", "第五季"
#$Seasons_TC = "", "第一季", "第二季", "第三季", "第四季", "第五季"
$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 -match $TargetDrive) {
$Index2 = Join-Path -Path $TargetDrive -ChildPath (Split-Path -Path $Index -noQualifier)
} else {
$Index2 = $Index
}
} else {
$Index2 = Join-Path $TargetDrive $Index
}
# 取得目標資料夾是否存在,不存在建立資料夾,並取得資料夾物件
$LinkPath = "{2}\{0}\{1}" -f $WorkData.DestinName, $Seasons_TC[$WorkData.Season], $Index
$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 {
@ -66,19 +123,7 @@ function Get-StandName {
Get-Item -Path $(Edit-PathChar $FileObj)
})
# 因應檔名不同而設定多組正規方式
$EpisodeRegex = @(
"(?: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})\w{2}\]"
"\[(?<Episode>\d{1,2})\]"
)
$NameRegex = @(
"[\[\b\s](?<Name>[^\[\]]*)[\]\b\s]?$"
)
$Result = $File.Name | % { Get-NameRegex $_ -Regex $EpisodeRegex } | % { $(Get-NameRegex $_["L"] -Regex $NameRegex -Exclude "(BD|DVD|HDTV)RIP" -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
# 建立回傳資料物件,以方便後續資料處理。
$OutputData = "" | Select newName, Season, Episode, newFullName;
@ -119,6 +164,14 @@ if ($Single) {
} 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 $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 | % {
@ -138,40 +191,5 @@ $ENDResult = $BuildList | % {
#$ENDResult
#$ENDResult.gettype();
if (-not($Sub)) { $ENDResult | % { if ($_.Episode) { $WorkData.Episode += $_.Episode }}}
$WorkData | ConvertTo-Json | Out-File -Encoding "UTF8" "$JsonPath\animelibk.json"
exit 0;
##### 以下試寫或測試用 #####
# SIG # Begin signature block
# MIIFxwYJKoZIhvcNAQcCoIIFuDCCBbQCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcC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uXLq8dG9gWT7OisKWe0hifSMGaZ9afpNeiFLuD1
# GSGk5E0W+bUyezCmjOdNWYIW452brt8rJS21WWVVvrsGjgJoTdsoAkFek8OEm82+
# 1UR7xZOHybLvi8N0gvD+UFlVvcv6pmgUqaz5DS7J2si75Xj9pThE6/bPu/fkpc53
# gMbleXNaqXIKeJTbI5hi2crlKyXBe/GfuK0Tv+06S1sL32VAs1VwmjGCAd8wggHb
# AgEBMD4wKjETMBEGA1UEAwwKTmljayBDaGFuZzETMBEGA1UECgwKZWFyaWNrLm9y
# ZwIQdMnAQ4BDAo9Lgfn+QeDdhjAJBgUrDgMCGgUAoHgwGAYKKwYBBAGCNwIBDDEK
# MAigAoAAoQKAADAZBgkqhkiG9w0BCQMxDAYKKwYBBAGCNwIBBDAcBgorBgEEAYI3
# AgELMQ4wDAYKKwYBBAGCNwIBFTAjBgkqhkiG9w0BCQQxFgQUEzAOjNkB8uaWsHtC
# RUzsrJzTfYMwDQYJKoZIhvcNAQEBBQAEggEAnvzVdQvGridnk2wdfYs8SjLEZYhc
# K3b7zpo5sNczj/zEFSsCskDik+oJ7p+vRszo7WGNt2/vJsbFPsIROYkxzV5UdgZR
# BDTpQhcRLPSdw6kipbKk1MPD6nmpCqQ1BdsvALJH8BXxr8UlaYZMhLiUS8dVx1Vh
# Cr4zTBc1nmHf/QdLlNyHieCdCqwLam87yN03u9NXDqearWwP+Zh/58I+qCQjs3sU
# coB9S7DCjoMp0NCpntohLkB2ZyLXdfbQM4bNUqQra5bcZAUAx6pPaDucCAqVbTtY
# fhXHdsfH9i1gd2TQUYkMBW59QwWy3EyeC5eKkBO+aQUj+85PH8DQftKFsg==
# SIG # End signature block
$WorkData | ConvertTo-Json | Out-File -Encoding "UTF8" $RecorDB
exit 0;

8
RegexRule.ini

@ -0,0 +1,8 @@
[EpisodeRegex]
"(?: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})\]"
[NameRegex]
"[\[\b\s](?<Name>[^\[\]]*)[\]\b\s]?$"
Loading…
Cancel
Save