RawTitle解析

This commit is contained in:
limil 2026-01-03 13:34:58 +08:00
parent 3525df1e95
commit 596f2bc693
2 changed files with 70 additions and 35 deletions

View File

@ -43,6 +43,8 @@ public static class ItemFields
public static string Key_Type => "Type";
public static string Key_SubtitleLanguage => "SubLang";
public static string Key_Episode => "Episode";
public static ItemType Type(this Item item)
{

View File

@ -19,7 +19,7 @@ public class RawParser(Configs configs) : ItemParser
var result = parts.ToList();
foreach (var regex in config.TokenFilterRules.Regexes)
{
result.RemoveAll(part => Regex.Match(part.Trim(), regex).Success);
result.RemoveAll(part => string.IsNullOrEmpty(part.Trim()) || Regex.Match(part.Trim(), regex).Success);
}
return result;
}
@ -97,12 +97,11 @@ public class RawParser(Configs configs) : ItemParser
return false;
}
private bool TryParseSeason(TreeNode node, out string season, out MatchInfo matchInfo)
private bool TryParseSeason(List<string> parts, Item item, out string season, out MatchInfo matchInfo)
{
season = null;
matchInfo = null;
var parts = GetParts(node.Info);
for (int i = 0; i < parts.Count; i++)
{
var part = parts[i];
@ -148,12 +147,11 @@ public class RawParser(Configs configs) : ItemParser
return false;
}
private bool TryParseGroup(TreeNode node, out string group, out MatchInfo matchInfo)
private bool TryParseGroup(List<string> parts, Item item, out string group, out MatchInfo matchInfo)
{
group = null;
matchInfo = null;
var parts = GetParts(node.Info);
for (int i = 0; i < parts.Count; i++)
{
if (IsFullMatch(parts[i], config.GroupsMatchRules?.Full))
@ -184,23 +182,23 @@ public class RawParser(Configs configs) : ItemParser
return false;
}
private ItemFields.ItemType ParseItemType(TreeNode node)
private ItemFields.ItemType ParseItemType(Item item)
{
// 1. 判断是否属于Extras
foreach (var extraMatchName in config.TypeMatchRules.Extra.IfDirNameIs)
{
if (node.Info.Name().Equals(extraMatchName, StringComparison.OrdinalIgnoreCase))
if (item.Name().Equals(extraMatchName, StringComparison.OrdinalIgnoreCase))
{
return ItemFields.ItemType.Extra;
}
}
if (node.Info.IsFolder())
if (item.IsFolder())
{
return ItemFields.ItemType.Unknown;
}
var infoExt = Path.GetExtension(node.Info.Name());
var infoExt = Path.GetExtension(item.Name());
if (string.IsNullOrEmpty(infoExt)) return ItemFields.ItemType.Extra;
foreach (var ext in config.TypeMatchRules.Extra.IfFileExtensionIs)
{
@ -232,42 +230,42 @@ public class RawParser(Configs configs) : ItemParser
return ItemFields.ItemType.Unknown;
}
private bool TryParseType(TreeNode node, out string type, out MatchInfo matchInfo)
private bool TryParseType(List<string> parts, Item item, out string type, out MatchInfo matchInfo)
{
matchInfo = null;
type = null;
var typeEnum = ParseItemType(node);
var typeEnum = ParseItemType(item);
if (typeEnum == ItemFields.ItemType.Unknown) return false;
type = typeEnum.ToString();
return true;
}
private bool TryParseSubtitleLanguage(TreeNode node, out string language, out MatchInfo matchInfo)
private bool TryParseSubtitleLanguage(List<string> parts, Item item, out string language, out MatchInfo matchInfo)
{
language = null;
matchInfo = null;
if (node.Info.Type() != ItemFields.ItemType.Subtitle)
if (item.Type() != ItemFields.ItemType.Subtitle)
{
return false;
}
var name = node.Info.Name();
var parts = name.Split(".");
if (parts.Length < 3) return false;
var name = item.Name();
var tokens = name.Split(".");
if (tokens.Length < 3) return false;
language = parts[^2];
language = tokens[^2];
return true;
}
private bool TryParseEpisode(TreeNode node, out string episode, out MatchInfo matchInfo)
private bool TryParseEpisode(List<string> parts, Item item, out string episode, out MatchInfo matchInfo)
{
episode = null;
matchInfo = null;
if (node.Info.Type() != ItemFields.ItemType.Episode)
if (item.Type() == ItemFields.ItemType.Extra ||
item.Type() == ItemFields.ItemType.Unknown)
{
return false;
}
var parts = GetParts(node.Info);
for (int i = 0; i < parts.Count; i++)
{
var part = parts[i];
@ -277,25 +275,58 @@ public class RawParser(Configs configs) : ItemParser
var match = Regex.Match(token.Trim(), @"^\d{1,2}$");
if (match.Success)
{
episode = int.Parse(match.Value).ToString();
matchInfo = new MatchInfo
{
content = content,
content = match.Value,
partIndex = i
};
return true;
}
return true;
}
}
return false;
}
//
// private bool TryParseRawTitle(TreeNode node, out string rawTitle, out MatchInfo matchInfo)
// {
//
// }
private bool TryParseRawTitle(List<string> parts, Item item, out string rawTitle, out MatchInfo matchInfo)
{
rawTitle = null;
matchInfo = null;
if (item.Name().Contains("夏目"))
{
return false;
}
if (item.Type() == ItemFields.ItemType.Extra)
{
return false;
}
while(TryParseGroup(parts, item, out _, out var groupMatch))
{
parts.RemoveAt(groupMatch.partIndex);
}
var tokens = new List<string>();
foreach (var part in parts)
{
tokens.AddRange(part.Split("-"));
}
while(TryParseSeason(tokens, item, out _, out var seasonMatch))
{
tokens[seasonMatch.partIndex] = tokens[seasonMatch.partIndex].Replace(seasonMatch.content, "");
}
tokens = FilterParts(tokens);
if(tokens.Count == 0) return false;
rawTitle = tokens.First().Trim();
return true;
}
class MatchInfo
@ -304,18 +335,20 @@ public class RawParser(Configs configs) : ItemParser
public int partIndex;
}
private delegate bool FieldParser(TreeNode node, out string result, out MatchInfo matchInfo);
private delegate bool FieldParser(List<string> parts, Item item, out string result, out MatchInfo matchInfo); // 最好将item换成parts
private bool TryParseField(TreeNode node, FieldParser fieldParser, out string result)
{
result = null;
if (node.Info == null) return false;
if (!fieldParser(node, out var fieldValue, out _)) return false;
var item = node.Info;
if (item == null) return false;
var parts = GetParts(item);
if (!fieldParser(parts, item, out var fieldValue, out _)) return false;
var parsed = new List<string>();
foreach (var child in node.Children)
{
if (fieldParser(child, out var childFieldValue, out _))
var childParts = GetParts(child.Info);
if (fieldParser(childParts, child.Info, out var childFieldValue, out _))
{
parsed.Add(childFieldValue);
}
@ -348,7 +381,6 @@ public class RawParser(Configs configs) : ItemParser
while (queue.Count > 0)
{
var current = queue.Dequeue();
if(current.Info.Info.ContainsKey(fieldName)) continue;
if (TryParseField(current, fieldParser, out var fieldValue))
{
current.Info.Info.TryAdd(fieldName, fieldValue);
@ -369,6 +401,7 @@ public class RawParser(Configs configs) : ItemParser
DoParse(node, TryParseGroup, ItemFields.Key_Group);
DoParse(node, TryParseType, ItemFields.Key_Type);
DoParse(node, TryParseSubtitleLanguage, ItemFields.Key_SubtitleLanguage);
DoParse(node, TryParseEpisode, ItemFields.Key_Episode);
DoParse(node, TryParseRawTitle, ItemFields.Key_RawTitle);
}
}