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:
Joseph Milazzo 2021-03-29 17:37:35 -05:00 committed by GitHub
parent d9246b7351
commit a0deafe75b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 94 additions and 10 deletions

View File

@ -303,6 +303,7 @@ namespace API.Tests
[InlineData("Scott Pilgrim 01 - Scott Pilgrim's Precious Little Life (2004)", "1")] [InlineData("Scott Pilgrim 01 - Scott Pilgrim's Precious Little Life (2004)", "1")]
[InlineData("Teen Titans v1 001 (1966-02) (digital) (OkC.O.M.P.U.T.O.-Novus)", "1")] [InlineData("Teen Titans v1 001 (1966-02) (digital) (OkC.O.M.P.U.T.O.-Novus)", "1")]
[InlineData("Scott Pilgrim 02 - Scott Pilgrim vs. The World (2005)", "2")] [InlineData("Scott Pilgrim 02 - Scott Pilgrim vs. The World (2005)", "2")]
[InlineData("Superman v1 024 (09-10 1943)", "1")]
public void ParseComicVolumeTest(string filename, string expected) public void ParseComicVolumeTest(string filename, string expected)
{ {
Assert.Equal(expected, ParseComicVolume(filename)); Assert.Equal(expected, ParseComicVolume(filename));
@ -322,6 +323,7 @@ namespace API.Tests
[InlineData("Babe 01", "0")] [InlineData("Babe 01", "0")]
[InlineData("Scott Pilgrim 01 - Scott Pilgrim's Precious Little Life (2004)", "0")] [InlineData("Scott Pilgrim 01 - Scott Pilgrim's Precious Little Life (2004)", "0")]
[InlineData("Teen Titans v1 001 (1966-02) (digital) (OkC.O.M.P.U.T.O.-Novus)", "1")] [InlineData("Teen Titans v1 001 (1966-02) (digital) (OkC.O.M.P.U.T.O.-Novus)", "1")]
[InlineData("Superman v1 024 (09-10 1943)", "24")]
public void ParseComicChapterTest(string filename, string expected) public void ParseComicChapterTest(string filename, string expected)
{ {
Assert.Equal(expected, ParseComicChapter(filename)); Assert.Equal(expected, ParseComicChapter(filename));
@ -336,6 +338,22 @@ namespace API.Tests
{ {
Assert.Equal(expected, IsImage(filename)); Assert.Equal(expected, IsImage(filename));
} }
[Theory]
[InlineData("C:/", "C:/Love Hina/Love Hina - Special.cbz", "Love Hina")]
[InlineData("C:/", "C:/Love Hina/Specials/Ani-Hina Art Collection.cbz", "Love Hina")]
[InlineData("C:/", "C:/Mujaki no Rakuen Something/Mujaki no Rakuen Vol12 ch76.cbz", "Mujaki no Rakuen")]
public void FallbackTest(string rootDir, string inputPath, string expectedSeries)
{
var actual = Parse(inputPath, rootDir);
if (actual == null)
{
Assert.NotNull(actual);
return;
}
Assert.Equal(expectedSeries, actual.Series);
}
[Fact] [Fact]

View File

@ -74,5 +74,17 @@ namespace API.Tests.Services
Assert.DoesNotContain(dirs, s => s.Contains("regex")); Assert.DoesNotContain(dirs, s => s.Contains("regex"));
} }
[Theory]
[InlineData("C:/Manga/", "C:/Manga/Love Hina/Specials/Omake/", "Omake,Specials,Love Hina")]
[InlineData("C:/Manga/", "C:/Manga/Love Hina/Specials/Omake", "Omake,Specials,Love Hina")]
[InlineData("C:/Manga", "C:/Manga/Love Hina/Specials/Omake/", "Omake,Specials,Love Hina")]
[InlineData("C:/Manga", @"C:\Manga\Love Hina\Specials\Omake\", "Omake,Specials,Love Hina")]
[InlineData(@"/manga/", @"/manga/Love Hina/Specials/Omake/", "Omake,Specials,Love Hina")]
public void GetFoldersTillRoot_Test(string rootPath, string fullpath, string expectedArray)
{
var expected = expectedArray.Split(",");
Assert.Equal(expected, DirectoryService.GetFoldersTillRoot(rootPath, fullpath));
}
} }
} }

View File

@ -3,6 +3,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using API.Entities.Enums; using API.Entities.Enums;
using API.Services;
namespace API.Parser namespace API.Parser
{ {
@ -343,9 +344,7 @@ namespace API.Parser
public static ParserInfo Parse(string filePath, string rootPath, LibraryType type = LibraryType.Manga) public static ParserInfo Parse(string filePath, string rootPath, LibraryType type = LibraryType.Manga)
{ {
var fileName = Path.GetFileName(filePath); var fileName = Path.GetFileName(filePath);
var directoryName = (new FileInfo(filePath)).Directory?.Name;
var rootName = (new DirectoryInfo(rootPath)).Name;
var ret = new ParserInfo() var ret = new ParserInfo()
{ {
Chapters = type == LibraryType.Manga ? ParseChapter(fileName) : ParseComicChapter(fileName), Chapters = type == LibraryType.Manga ? ParseChapter(fileName) : ParseComicChapter(fileName),
@ -355,11 +354,32 @@ namespace API.Parser
Format = ParseFormat(filePath), Format = ParseFormat(filePath),
FullFilePath = filePath FullFilePath = filePath
}; };
if (ret.Series == string.Empty && directoryName != null && directoryName != rootName) if (ret.Series == string.Empty)
{ {
ret.Series = ParseSeries(directoryName); // Try to parse information out of each folder all the way to rootPath
if (ret.Series == string.Empty) ret.Series = CleanTitle(directoryName); 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); var edition = ParseEdition(fileName);
@ -562,7 +582,7 @@ namespace API.Parser
{ {
if (match.Success) if (match.Success)
{ {
title = title.Replace(match.Value, ""); title = title.Replace(match.Value, "").Trim();
} }
} }
} }
@ -574,7 +594,7 @@ namespace API.Parser
{ {
if (match.Success) if (match.Success)
{ {
title = title.Replace(match.Value, ""); title = title.Replace(match.Value, "").Trim();
} }
} }
} }
@ -591,7 +611,7 @@ namespace API.Parser
{ {
if (match.Success) if (match.Success)
{ {
title = title.Replace(match.Value, ""); title = title.Replace(match.Value, "").Trim();
} }
} }
} }

View File

@ -40,6 +40,40 @@ namespace API.Services
reSearchPattern.IsMatch(Path.GetExtension(file))); 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) public bool Exists(string directory)
{ {
var di = new DirectoryInfo(directory); var di = new DirectoryInfo(directory);