Tyderion / AniDb-AniAdd-CLI

1 stars 0 forks source link

Smart Truncating #10

Open Tyderion opened 1 month ago

Tyderion commented 1 month ago

When the full path of the renamed file is > 240 characters the filename currently get's truncated in order to fit that limit.

In some cases this can lead to the episode number being removed from the filename (which results in all episodes of the same series with which that happens to have the same name). this completely defeats the purpose of this tools.

Ideas for solution: Support a new result-tag in the tagsystem (e.g. FinalEpisodeNumber) which, if set by the tagsystem, will be used to make sure to always keep that part in the resulting filename. It's also has to be made sure that if we truncate the anime name (in an episode), that all episodes get truncated the same way. This will be very difficult to do. Last point could be fixed by Implementing a max length for anime names (foldername/part of the filename before episode number) so that the chance of this happening is a lot lower.

Tyderion commented 1 month ago

@runecalico Feel free to pitch in here if you have any ideas on how to do this in reliable way

runecalico commented 1 month ago

My thoughts are that, in theory, my requirements if I was using the TagSystem are the following:

  1. There needs to be a way we can reference the "full path" of the file within the tagging system, right now we have PathName and FileName, which when conjoined would be the "full path" .. I think, is this correct?
  2. There needs to be a way to evaluate if the full path of the file is > 240, and then take one or more actions based on that evaluation, for example if it's > 240, then modify FileName and check again if it's > 240.

The big problem to me isn't entirely the "is it greater then 240", but that we want to try various permutations on the BasePath and/or PathName until it is less then 240.

it has been a long time since I last changed my tagging configuration .. but I suspect that integer comparisons are not currently supported by the tagging system, which might make it difficult to do something like "is the final path greater then 240", unless you add a built-in tag, that evaluates the PathName/FileName value every time it's invoked and returns a true/false that could be used in the current tagging which does have a ternary operator.

Something like this (This probably would still need tweaking)

MovieFileName:=%ShowTitle% %FileInfo%
TVShowFileNameWithEpisodeTitle:=%ShowTitle% %EpNoFull% %EpisodeTitle% %FileInfo%
TVShowFileNameWithoutEpisodeTitle:=%ShowTitle% %EpNoFull% %FileInfo%
BasePath:={%IsMovie% ? { %IsEnglish% ? %BaseEnglishMoviePath% : %BaseMoviePath% } : { %IsEnglish% ? %BaseEnglishTVShowPath% : %BaseTVShowPath% } }
FileName:={%IsMovie% ? %MovieFileName% : %TVShowFileNameWithEpisodeTitle% }  
PathName:=%BasePath%  %ShowTitle% " [anidb-"%AId%"]"
FilePathLengthToLarge ? FileName:={%IsMovie% ? %MovieFileName% : %TVShowFileNameWithoutEpisodeTitle% }  : ""

I'll do some testing to see if it's possible to do this right now, which if works would still likely be cumbersome.

runecalico commented 1 month ago

Well .. it can be done using the current Tagging System .. but it is ugly, cumbersome, not intuitive and kinda limited (but does work)

BaseTVShowPath:="Z:\2-WorkingImport-AniAdd\releases\"
BaseEnglishTVShowPath:="Z:\2-WorkingImport-English-AniAdd\releases\"
BaseMoviePath:="Z:\2-WorkingImport-AniAdd\movies\"
BaseEnglishMoviePath:="Z:\2-WorkingImport-English-AniAdd\movies\"
ShowTitle:=[%ATe%, %ATr%, %ATk%]
EpisodeTitle:=[%ETe%, %ETr%, %ETk%]
ShowTitle:=$repl(%ShowTitle%, "\?|\s*\.+$", "_")
ShowTitle:=$repl(%ShowTitle%, '[\\":/*|<>?]', " ")
EpisodeTitle:=$repl(%EpisodeTitle%, '[\\":/*|<>?]', " ")
ShowTitle:=$repl(%ShowTitle%, "\s+", " ")
EpisodeTitle:=$repl(%EpisodeTitle%, "\s+", " ")
ShowTitle:=$repl(%ShowTitle%, "^\s|\s$", "")
EpisodeTitle:=$repl(%EpisodeTitle%, "^\s|\s$", "")
ShowTitle:=$repl(%ShowTitle%, "`", "'")
EpisodeTitle:=$repl(%EpisodeTitle%, "`", "'")
Trunc(str, len):=$repl(%str%, ".{" $len($repl(%str%, "(.?){" %len% "}$", "")) "}$", "")
TruncEllipse(str, len):={$len(%str%) = $len($Trunc(%str%, %len%)) ? %str% : $Trunc(%str%, %len%) "…" }
EpisodeTitle:=$TruncEllipse(%EpisodeTitle%, "24")
ShowTitleTrunc:=$TruncEllipse(%ShowTitle%, "24")
Regular:=""
Special:="S"
#Separator:=" - "
SpecialEp:=$repl(%EpNo%, "[1234567890]", "")
EpNo:=$repl(%EpNo%, "[SCTPO]", "")
Ver:={%Ver% = "1" ? "" : "v" %Ver% }
Pad:={%SpecialEp% ? "2" : $max($len(%EpHiNo%), $len(%EpCount%)) }
Pad:={$match(%EpCount%, "0") ? $max("2", %Pad%) : %Pad% }
EpNoPad:=$pad(%EpNo%, %Pad%, "0")
EpNoPad:={%SpecialEp%       ? %EpNoPad%  :  %Regular%   %EpNoPad% }
EpNoPad:={%SpecialEp% = "S" ? %Special%     %EpNoPad% : %EpNoPad% }
EpNoPad:={%SpecialEp% = "C" ? %Special% "1" %EpNoPad% : %EpNoPad% }
EpNoPad:={%SpecialEp% = "T" ? %Special% "2" %EpNoPad% : %EpNoPad% }
EpNoPad:={%SpecialEp% = "P" ? %Special% "3" %EpNoPad% : %EpNoPad% }
EpNoPad:={%SpecialEp% = "O" ? %Special% "4" %EpNoPad% : %EpNoPad% }
EpNoFull:=%Separator% %EpNoPad% %Ver% %Separator%
GT:="[" [%GTs%, %GTl%] "]"
Src:="[" $repl(%Source%, "Blu-ray", "BluRay") "]"
Cen:={%Cen% ? "[Cen]" : "" }
Res:="[" %FVideoRes% "]"
VCodec:="[" $repl(%FVCodec%,"H264/AVC","h264") "]"
ACodec:="[" %FACodec% "]"
CRC:="[" $uc(%FCrc%) "]"
FileInfo:=" " %GT% %Src% %Cen% %Res% %VCodec% %CRC%
MovieTypes:="Movie|OVA"
IsMovie:={$match(%Type%, %MovieTypes%) ? {%EpCount% = "1" ? {%SpecialEp% ? "" : "1" } : "" } : "" }
LangTypes:=".*([eE]nglish).*"
IsEnglish:={$match(%FALng%, %LangTypes%) ? "1" : "" }
MovieFileName:=%ShowTitle%" - "%FileInfo%
MovieFileNameMinimal:=%ShowTitleTrunc%" - "%FileInfo%
TVShowFileName:=%ShowTitle%" - "%EpNoFull%" - "%EpisodeTitle%" - "%FileInfo%
TVShowFileNameWithoutEpisodeTitle:=%ShowTitle%" - "%EpNoFull%" - "%FileInfo%
TVShowFileNameMinimal:=%EpNoFull%" - "%FileInfo%
BasePath:={%IsMovie% ? { %IsEnglish% ? %BaseEnglishMoviePath% : %BaseMoviePath% } : { %IsEnglish% ? %BaseEnglishTVShowPath% : %BaseTVShowPath% } }
FileName:={%IsMovie% ? %MovieFileName% : %TVShowFileName% }
PathName:=%BasePath%  %ShowTitle% " [anidb-"%AId%"]"
PathNameTruncated:=%BasePath%  %ShowTitleTrunc% " [anidb-"%AId%"]"
# Change FileName based on truncation, will use TVShowFileNameWithoutEpisodeTitle or MovieFileNameMinimal
CheckPathLengthPad:=$max($len($pad(%PathName% %FileName%,"240","o")) , $len(%PathName% %FileName%))
FileName:= { %CheckPathLengthPad% = $len(%PathName% %FileName%) ? {%IsMovie% ? %MovieFileNameMinimal% : %TVShowFileNameWithoutEpisodeTitle% } : {%IsMovie% ? %MovieFileName% : %TVShowFileName% } }
# Change FileName based on truncation, will use TVShowFileNameMinimal or MovieFileNameMinimal
CheckPathLengthPad:=$max($len($pad(%PathName% %FileName%,"240","o")) , $len(%PathName% %FileName%))
FileName:= { %CheckPathLengthPad% = $len(%PathName% %FileName%) ? {%IsMovie% ? %MovieFileNameMinimal% : %TVShowFileNameMinimal% } : {%IsMovie% ? %MovieFileNameMinimal% : %TVShowFileNameWithoutEpisodeTitle% } }
# Change PathName based on truncation will use PathNameTruncated or PathName
CheckPathLengthPad:=$max($len($pad(%PathName% %FileName%,"240","o")) , $len(%PathName% %FileName%))
PathName:= { %CheckPathLengthPad% = $len(%PathName% %FileName%) ? %PathNameTruncated% : %PathName% }
runecalico commented 1 month ago

I verified this works using episodes from 7th Time Loop The Villainess Enjoys a Carefree Life Married to Her Worst Enemy! The end result is Episode # - info as the Anime Title is stupidly long, so the filename with either the Anime Title OR Episode Title is just too long

Tyderion commented 1 month ago

yeah, using the tagging system is a possibility, but kinda annoying and everyone has to do it themselves. I would prefer a way that involves smaller changes to the tagsystem code, as that one is kinda confusing even if you don't do advanced things like that. but at least your version correctly sorts aftewards (as the anime title is left as is in the filename)

I guess adding your changes to the default tagging system (and also giving users the hint to add these parts to it) might be the easiest and most consistent way to solve this

Your changes would also work identically in my tagging system as mine is very similar to yours (apart from the seperate path for english things)

runecalico commented 1 month ago

Perhaps something more "automatic" for those not using the tagging system? As anyone not using the tagging system is implicitly accepting whatever default naming scheme aniAdd is going to create, so having a cascading decision tree from longest to shortest to avoid truncation would be fine.

Whereas those using the tagging system .. are on their own .. but then anyone using the tagging system is likely very opinionated on what they want for names of both the files and directories :)

runecalico commented 1 month ago

I'll have to modify the tagging I included here as I found a new stupidly long anime name to play with "The Misfit of Demon King Academy History's Strongest Demon King Reincarnates and Goes to School with His Descendants II (2024)"

I barely have enough length to even write the episode # and file info (I'm a big fan of the file info, as i do further pruning/sorting on that)

Tyderion commented 1 month ago

oh wow, that name is a mouthful 🤣 Another idea i just had: Add a new function that can be used in the tagging system $trim (or something) which will trim strings on the java side. And then we could do something like:

TVShowFileName:=$trim(%TVShowFileName%, 80)
ShowTitle:=$trim(%ShowTitle%, 80)

or something, this way you could ensure consistent episode/show name length (that is, all episodes of the same series have the same prefix) and also ensure that the path length is a maximum of "whatever-you-need"

runecalico commented 1 month ago

$trim would certainly be easier then the current way to do it, and would greatly simplify some of the tagging "code" needed to do that now. I am certainly in favor of it.

This is the current way to do that (not exactly simple, or easy to understand)

Trunc(str, len):=$repl(%str%, ".{" $len($repl(%str%, "(.?){" %len% "}$", "")) "}$", "")
TruncEllipse(str, len):={$len(%str%) = $len($Trunc(%str%, %len%)) ? %str% : $Trunc(%str%, %len%) "…" }
EpisodeTitle:=$TruncEllipse(%EpisodeTitle%, "24")
Tyderion commented 1 month ago

wow, if you didn't name the functions Trunc and TruncEllipse I wouldn't even know what they do I'll try to implement those 2 (i'll have to see about names) until sunday so you can test them out. I'm thinking about doing something like Trunc(%var%, length) and Trunc(%var%, length, "...") (so you could specify if it should replace truncated data with anything you like), but i can't promise it'll be the same name both times