Parser Enhancement: Fallback to Folder name (#129)
* More cases for parsing regex * Implemented GetFoldersTillRoot for falling back on parsing when we can't get anything from the filename. * Implemented a fallback strategy. Not tested on large libraries yet. * Fallback tested and working great. * Removed a test case that won't pass and added some trims
This commit is contained in:
parent
d9246b7351
commit
a0deafe75b
4 changed files with 94 additions and 10 deletions
|
@ -3,6 +3,7 @@ using System.IO;
|
|||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using API.Entities.Enums;
|
||||
using API.Services;
|
||||
|
||||
namespace API.Parser
|
||||
{
|
||||
|
@ -343,9 +344,7 @@ namespace API.Parser
|
|||
public static ParserInfo Parse(string filePath, string rootPath, LibraryType type = LibraryType.Manga)
|
||||
{
|
||||
var fileName = Path.GetFileName(filePath);
|
||||
var directoryName = (new FileInfo(filePath)).Directory?.Name;
|
||||
var rootName = (new DirectoryInfo(rootPath)).Name;
|
||||
|
||||
|
||||
var ret = new ParserInfo()
|
||||
{
|
||||
Chapters = type == LibraryType.Manga ? ParseChapter(fileName) : ParseComicChapter(fileName),
|
||||
|
@ -355,11 +354,32 @@ namespace API.Parser
|
|||
Format = ParseFormat(filePath),
|
||||
FullFilePath = filePath
|
||||
};
|
||||
|
||||
if (ret.Series == string.Empty && directoryName != null && directoryName != rootName)
|
||||
|
||||
if (ret.Series == string.Empty)
|
||||
{
|
||||
ret.Series = ParseSeries(directoryName);
|
||||
if (ret.Series == string.Empty) ret.Series = CleanTitle(directoryName);
|
||||
// Try to parse information out of each folder all the way to rootPath
|
||||
var fallbackFolders = DirectoryService.GetFoldersTillRoot(rootPath, Path.GetDirectoryName(filePath)).ToList();
|
||||
for (var i = 0; i < fallbackFolders.Count; i++)
|
||||
{
|
||||
var folder = fallbackFolders[i];
|
||||
if (!string.IsNullOrEmpty(ParseMangaSpecial(folder))) continue;
|
||||
if (ParseVolume(folder) != "0" || ParseChapter(folder) != "0") continue;
|
||||
|
||||
var series = ParseSeries(folder);
|
||||
|
||||
if ((string.IsNullOrEmpty(series) && i == fallbackFolders.Count - 1))
|
||||
{
|
||||
ret.Series = CleanTitle(folder);
|
||||
break;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(series))
|
||||
{
|
||||
ret.Series = series;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var edition = ParseEdition(fileName);
|
||||
|
@ -562,7 +582,7 @@ namespace API.Parser
|
|||
{
|
||||
if (match.Success)
|
||||
{
|
||||
title = title.Replace(match.Value, "");
|
||||
title = title.Replace(match.Value, "").Trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -574,7 +594,7 @@ namespace API.Parser
|
|||
{
|
||||
if (match.Success)
|
||||
{
|
||||
title = title.Replace(match.Value, "");
|
||||
title = title.Replace(match.Value, "").Trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -591,7 +611,7 @@ namespace API.Parser
|
|||
{
|
||||
if (match.Success)
|
||||
{
|
||||
title = title.Replace(match.Value, "");
|
||||
title = title.Replace(match.Value, "").Trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,6 +40,40 @@ namespace API.Services
|
|||
reSearchPattern.IsMatch(Path.GetExtension(file)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of folders from end of fullPath to rootPath.
|
||||
///
|
||||
/// Example) (C:/Manga/, C:/Manga/Love Hina/Specials/Omake/) returns [Omake, Specials, Love Hina]
|
||||
/// </summary>
|
||||
/// <param name="rootPath"></param>
|
||||
/// <param name="fullPath"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<string> GetFoldersTillRoot(string rootPath, string fullPath)
|
||||
{
|
||||
var separator = Path.AltDirectorySeparatorChar;
|
||||
if (fullPath.Contains(Path.DirectorySeparatorChar))
|
||||
{
|
||||
fullPath = fullPath.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
|
||||
}
|
||||
|
||||
if (rootPath.Contains(Path.DirectorySeparatorChar))
|
||||
{
|
||||
rootPath = rootPath.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
|
||||
}
|
||||
|
||||
var path = fullPath.EndsWith(separator) ? fullPath.Substring(0, fullPath.Length - 1) : fullPath;
|
||||
var root = rootPath.EndsWith(separator) ? rootPath.Substring(0, rootPath.Length - 1) : rootPath;
|
||||
var paths = new List<string>();
|
||||
while (Path.GetDirectoryName(path) != Path.GetDirectoryName(root))
|
||||
{
|
||||
var folder = new DirectoryInfo(path).Name;
|
||||
paths.Add(folder);
|
||||
path = path.Replace(separator + folder, string.Empty);
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
public bool Exists(string directory)
|
||||
{
|
||||
var di = new DirectoryInfo(directory);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue