mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Adding more tests and fixing subtitle identification
This commit is contained in:
parent
e55f60166e
commit
03bf2c4f10
@ -1,6 +1,7 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Kyoo.Controllers;
|
using Kyoo.Controllers;
|
||||||
using Kyoo.Models;
|
using Kyoo.Models;
|
||||||
|
using Kyoo.Models.Exceptions;
|
||||||
using Kyoo.Models.Options;
|
using Kyoo.Models.Options;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Moq;
|
using Moq;
|
||||||
@ -23,6 +24,10 @@ namespace Kyoo.Tests.Identifier
|
|||||||
"^\\/?(?<Collection>.+)?\\/(?<Show>.+?)(?: \\((?<StartYear>\\d+)\\))?\\/\\k<Show>(?: \\(\\d+\\))? S(?<Season>\\d+)E(?<Episode>\\d+)\\..*$",
|
"^\\/?(?<Collection>.+)?\\/(?<Show>.+?)(?: \\((?<StartYear>\\d+)\\))?\\/\\k<Show>(?: \\(\\d+\\))? S(?<Season>\\d+)E(?<Episode>\\d+)\\..*$",
|
||||||
"^\\/?(?<Collection>.+)?\\/(?<Show>.+?)(?: \\((?<StartYear>\\d+)\\))?\\/\\k<Show>(?: \\(\\d+\\))? (?<Absolute>\\d+)\\..*$",
|
"^\\/?(?<Collection>.+)?\\/(?<Show>.+?)(?: \\((?<StartYear>\\d+)\\))?\\/\\k<Show>(?: \\(\\d+\\))? (?<Absolute>\\d+)\\..*$",
|
||||||
"^\\/?(?<Collection>.+)?\\/(?<Show>.+?)(?: \\((?<StartYear>\\d+)\\))?\\/\\k<Show>(?: \\(\\d+\\))?\\..*$"
|
"^\\/?(?<Collection>.+)?\\/(?<Show>.+?)(?: \\((?<StartYear>\\d+)\\))?\\/\\k<Show>(?: \\(\\d+\\))?\\..*$"
|
||||||
|
},
|
||||||
|
SubtitleRegex = new[]
|
||||||
|
{
|
||||||
|
"^(?<Episode>.+)\\.(?<Language>\\w{1,3})\\.(?<Default>default\\.)?(?<Forced>forced\\.)?.*$"
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -131,5 +136,57 @@ namespace Kyoo.Tests.Identifier
|
|||||||
Assert.Null(episode.EpisodeNumber);
|
Assert.Null(episode.EpisodeNumber);
|
||||||
Assert.Null(episode.AbsoluteNumber);
|
Assert.Null(episode.AbsoluteNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task InvalidEpisodeIdentification()
|
||||||
|
{
|
||||||
|
_manager.Setup(x => x.GetAll(null, new Sort<Library>(), default)).ReturnsAsync(new[]
|
||||||
|
{
|
||||||
|
new Library {Paths = new [] {"/kyoo", "/kyoo/Library/"}}
|
||||||
|
});
|
||||||
|
await Assert.ThrowsAsync<IdentificationFailedException>(() => _identifier.Identify("/invalid/path"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task SubtitleIdentification()
|
||||||
|
{
|
||||||
|
_manager.Setup(x => x.GetAll(null, new Sort<Library>(), default)).ReturnsAsync(new[]
|
||||||
|
{
|
||||||
|
new Library {Paths = new [] {"/kyoo", "/kyoo/Library/"}}
|
||||||
|
});
|
||||||
|
Track track = await _identifier.IdentifyTrack("/kyoo/Library/Collection/Show (2000)/Show.eng.default.str");
|
||||||
|
Assert.True(track.IsExternal);
|
||||||
|
Assert.Equal("eng", track.Language);
|
||||||
|
Assert.Equal("subrip", track.Codec);
|
||||||
|
Assert.True(track.IsDefault);
|
||||||
|
Assert.False(track.IsForced);
|
||||||
|
Assert.StartsWith("/kyoo/Library/Collection/Show (2000)/Show", track.Episode.Path);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task SubtitleIdentificationUnknownCodec()
|
||||||
|
{
|
||||||
|
_manager.Setup(x => x.GetAll(null, new Sort<Library>(), default)).ReturnsAsync(new[]
|
||||||
|
{
|
||||||
|
new Library {Paths = new [] {"/kyoo", "/kyoo/Library/"}}
|
||||||
|
});
|
||||||
|
Track track = await _identifier.IdentifyTrack("/kyoo/Library/Collection/Show (2000)/Show.eng.default.extension");
|
||||||
|
Assert.True(track.IsExternal);
|
||||||
|
Assert.Equal("eng", track.Language);
|
||||||
|
Assert.Equal("extension", track.Codec);
|
||||||
|
Assert.True(track.IsDefault);
|
||||||
|
Assert.False(track.IsForced);
|
||||||
|
Assert.StartsWith("/kyoo/Library/Collection/Show (2000)/Show", track.Episode.Path);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task InvalidSubtitleIdentification()
|
||||||
|
{
|
||||||
|
_manager.Setup(x => x.GetAll(null, new Sort<Library>(), default)).ReturnsAsync(new[]
|
||||||
|
{
|
||||||
|
new Library {Paths = new [] {"/kyoo", "/kyoo/Library/"}}
|
||||||
|
});
|
||||||
|
await Assert.ThrowsAsync<IdentificationFailedException>(() => _identifier.IdentifyTrack("/invalid/path"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
@ -109,32 +110,32 @@ namespace Kyoo.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task<Track> IdentifyTrack(string path)
|
public Task<Track> IdentifyTrack(string path)
|
||||||
{
|
{
|
||||||
string relativePath = await _GetRelativePath(path);
|
|
||||||
Match match = _configuration.CurrentValue.SubtitleRegex
|
Match match = _configuration.CurrentValue.SubtitleRegex
|
||||||
.Select(x => new Regex(x, RegexOptions.IgnoreCase | RegexOptions.Compiled))
|
.Select(x => new Regex(x, RegexOptions.IgnoreCase | RegexOptions.Compiled))
|
||||||
.Select(x => x.Match(relativePath))
|
.Select(x => x.Match(path))
|
||||||
.FirstOrDefault(x => x.Success);
|
.FirstOrDefault(x => x.Success);
|
||||||
|
|
||||||
if (match == null)
|
if (match == null)
|
||||||
throw new IdentificationFailedException($"The subtitle at {path} does not match the subtitle's regex.");
|
throw new IdentificationFailedException($"The subtitle at {path} does not match the subtitle's regex.");
|
||||||
|
|
||||||
string episodePath = match.Groups["Episode"].Value;
|
string episodePath = match.Groups["Episode"].Value;
|
||||||
return new Track
|
string extension = Path.GetExtension(path);
|
||||||
|
return Task.FromResult(new Track
|
||||||
{
|
{
|
||||||
Type = StreamType.Subtitle,
|
Type = StreamType.Subtitle,
|
||||||
Language = match.Groups["Language"].Value,
|
Language = match.Groups["Language"].Value,
|
||||||
IsDefault = match.Groups["Default"].Value.Length > 0,
|
IsDefault = match.Groups["Default"].Success,
|
||||||
IsForced = match.Groups["Forced"].Value.Length > 0,
|
IsForced = match.Groups["Forced"].Success,
|
||||||
Codec = FileExtensions.SubtitleExtensions[Path.GetExtension(path)],
|
Codec = FileExtensions.SubtitleExtensions.GetValueOrDefault(extension, extension[1..]),
|
||||||
IsExternal = true,
|
IsExternal = true,
|
||||||
Path = path,
|
Path = path,
|
||||||
Episode = new Episode
|
Episode = new Episode
|
||||||
{
|
{
|
||||||
Path = episodePath
|
Path = episodePath
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user