From f71ae0385ce16ddd70db6065a97e8103c56633c2 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sat, 17 Jul 2021 00:51:15 +0200 Subject: [PATCH] Finishing the register episode task --- Kyoo/Controllers/RegexIdentifier.cs | 16 ++- Kyoo/Startup.cs | 1 - Kyoo/Tasks/RegisterEpisode.cs | 193 +++++++--------------------- 3 files changed, 56 insertions(+), 154 deletions(-) diff --git a/Kyoo/Controllers/RegexIdentifier.cs b/Kyoo/Controllers/RegexIdentifier.cs index f46778eb..e104d70a 100644 --- a/Kyoo/Controllers/RegexIdentifier.cs +++ b/Kyoo/Controllers/RegexIdentifier.cs @@ -38,8 +38,7 @@ namespace Kyoo.Controllers /// public Task<(Collection, Show, Season, Episode)> Identify(string path) { - string pattern = _configuration.Value.Regex; - Regex regex = new(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled); + Regex regex = new(_configuration.Value.Regex, RegexOptions.IgnoreCase | RegexOptions.Compiled); Match match = regex.Match(path); if (!match.Success) @@ -51,9 +50,12 @@ namespace Kyoo.Controllers (Collection collection, Show show, Season season, Episode episode) ret = new(); ret.collection.Name = match.Groups["Collection"].Value; + ret.collection.Slug = Utility.ToSlug(ret.collection.Name); ret.show.Title = match.Groups["Show"].Value; + ret.show.Slug = Utility.ToSlug(ret.show.Title); ret.show.Path = Path.GetDirectoryName(path); + ret.episode.Path = path; if (match.Groups["StartYear"].Success && int.TryParse(match.Groups["StartYear"].Value, out int tmp)) ret.show.StartAir = new DateTime(tmp, 1, 1); @@ -70,9 +72,13 @@ namespace Kyoo.Controllers if (match.Groups["Absolute"].Success && int.TryParse(match.Groups["Absolute"].Value, out tmp)) ret.episode.AbsoluteNumber = tmp; - ret.show.IsMovie = ret.episode.SeasonNumber == null && ret.episode.EpisodeNumber == null - && ret.episode.AbsoluteNumber == null; - + if (ret.episode.SeasonNumber == null && ret.episode.EpisodeNumber == null + && ret.episode.AbsoluteNumber == null) + { + ret.show.IsMovie = true; + ret.episode.Title = ret.show.Title; + } + return Task.FromResult(ret); } } diff --git a/Kyoo/Startup.cs b/Kyoo/Startup.cs index 5557e872..9d3c19dd 100644 --- a/Kyoo/Startup.cs +++ b/Kyoo/Startup.cs @@ -3,7 +3,6 @@ using System.IO; using Autofac; using Kyoo.Authentication; using Kyoo.Controllers; -using Kyoo.Models; using Kyoo.Models.Options; using Kyoo.Postgresql; using Kyoo.Tasks; diff --git a/Kyoo/Tasks/RegisterEpisode.cs b/Kyoo/Tasks/RegisterEpisode.cs index 51949675..799ac8aa 100644 --- a/Kyoo/Tasks/RegisterEpisode.cs +++ b/Kyoo/Tasks/RegisterEpisode.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Kyoo.Controllers; @@ -52,6 +53,10 @@ namespace Kyoo.Tasks /// [Injected] public IThumbnailsManager ThumbnailsManager { private get; set; } /// + /// The transcoder used to extract subtitles and metadata. + /// + [Injected] public ITranscoder Transcoder { private get; set; } + /// /// The logger used to inform the current status to the console. /// [Injected] public ILogger Logger { private get; set; } @@ -62,7 +67,7 @@ namespace Kyoo.Tasks return new() { TaskParameter.CreateRequired("path", "The path of the episode file"), - TaskParameter.Create("library", "The library in witch the episode is") + TaskParameter.CreateRequired("library", "The library in witch the episode is") }; } @@ -74,42 +79,53 @@ namespace Kyoo.Tasks try { - (Collection collection, Show show, Season season, Episode episode) = await Identifier.Identify(path); if (library != null) + { + if (library.Providers == null) + await LibraryManager.Load(library, x => x.Providers); MetadataProvider.UseProviders(library.Providers); + } - if (collection != null) - collection.Slug ??= Utility.ToSlug(collection.Name); + (Collection collection, Show show, Season season, Episode episode) = await Identifier.Identify(path); + collection = await _RegisterAndFill(collection); - show = await _RegisterAndFill(show); - // if (isMovie) - // await libraryManager!.Create(await GetMovie(show, path)); - // else - // { - // Season season = seasonNumber != null - // ? await GetSeason(libraryManager, show, seasonNumber.Value, library) - // : null; - // Episode episode = await GetEpisode(libraryManager, - // show, - // season, - // episodeNumber, - // absoluteNumber, - // path, - // library); - // await libraryManager!.Create(episode); - // } - // - // await libraryManager.AddShowLink(show, library, collection); - // Console.WriteLine($"Episode at {path} registered."); + + Show registeredShow = await _RegisterAndFill(show); + if (registeredShow.Path != show.Path) + { + if (show.StartAir.HasValue) + { + show.Slug += $"-{show.StartAir.Value.Year}"; + show = await LibraryManager.Create(show); + } + else + { + Logger.LogError("Duplicated show found ({Slug}) at {Path1} and {Path2}", + show.Slug, registeredShow.Path, show.Path); + return; + } + } + else + show = registeredShow; + + if (season != null) + season.Show = show; + season = await _RegisterAndFill(season); + + episode = await MetadataProvider.Get(episode); + episode.Season = season; + episode.Tracks = (await Transcoder.ExtractInfos(episode, false)) + .Where(x => x.Type != StreamType.Attachment) + .ToArray(); + await ThumbnailsManager.DownloadImages(episode); + + await LibraryManager.Create(episode); + await LibraryManager.AddShowLink(show, library, collection); } catch (DuplicatedItemException ex) { Logger.LogWarning(ex, "Duplicated found at {Path}", path); } - catch (Exception ex) - { - Logger.LogCritical(ex, "Unknown exception thrown while registering episode at {Path}", path); - } } private async Task _RegisterAndFill(T item) @@ -125,49 +141,7 @@ namespace Kyoo.Tasks await ThumbnailsManager.DownloadImages(item); return await LibraryManager.CreateIfNotExists(item); } - - // private async Task GetShow(ILibraryManager libraryManager, - // string showTitle, - // string showPath, - // bool isMovie, - // Library library) - // { - // Show old = await libraryManager.GetOrDefault(x => x.Path == showPath); - // if (old != null) - // { - // await libraryManager.Load(old, x => x.ExternalIDs); - // return old; - // } - // - // Show show = await MetadataProvider.SearchShow(showTitle, isMovie, library); - // show.Path = showPath; - // show.People = await MetadataProvider.GetPeople(show, library); - // - // try - // { - // show = await libraryManager.Create(show); - // } - // catch (DuplicatedItemException) - // { - // old = await libraryManager.GetOrDefault(show.Slug); - // if (old != null && old.Path == showPath) - // { - // await libraryManager.Load(old, x => x.ExternalIDs); - // return old; - // } - // - // if (show.StartAir != null) - // { - // show.Slug += $"-{show.StartAir.Value.Year}"; - // await libraryManager.Create(show); - // } - // else - // throw; - // } - // await ThumbnailsManager.Validate(show); - // return show; - // } - // + /* * private async Task RegisterExternalSubtitle(string path, CancellationToken token) @@ -215,83 +189,6 @@ namespace Kyoo.Tasks await Console.Error.WriteLineAsync($"Unknown error while registering subtitle: {ex.Message}"); } } - - private async Task GetSeason(ILibraryManager libraryManager, - Show show, - int seasonNumber, - Library library) - { - try - { - Season season = await libraryManager.Get(show.Slug, seasonNumber); - season.Show = show; - return season; - } - catch (ItemNotFoundException) - { - Season season = new();//await MetadataProvider.GetSeason(show, seasonNumber, library); - try - { - await libraryManager.Create(season); - await ThumbnailsManager.Validate(season); - } - catch (DuplicatedItemException) - { - season = await libraryManager.Get(show.Slug, seasonNumber); - } - season.Show = show; - return season; - } - } - - private async Task GetEpisode(ILibraryManager libraryManager, - Show show, - Season season, - int? episodeNumber, - int? absoluteNumber, - string episodePath, - Library library) - { - Episode episode = new(); - //await MetadataProvider.GetEpisode(show, - // episodePath, - // season?.SeasonNumber, - // episodeNumber, - // absoluteNumber, - // library); - - if (episode.SeasonNumber != null) - { - season ??= await GetSeason(libraryManager, show, episode.SeasonNumber.Value, library); - episode.Season = season; - episode.SeasonID = season?.ID; - } - await ThumbnailsManager.Validate(episode); - await GetTracks(episode); - return episode; - } - - private async Task GetMovie(Show show, string episodePath) - { - Episode episode = new() - { - Title = show.Title, - Path = episodePath, - Show = show, - ShowID = show.ID, - ShowSlug = show.Slug - }; - episode.Tracks = await GetTracks(episode); - return episode; - } - - private async Task> GetTracks(Episode episode) - { - episode.Tracks = (await Transcoder.ExtractInfos(episode, false)) - .Where(x => x.Type != StreamType.Attachment) - .ToArray(); - return episode.Tracks; - } */ } } \ No newline at end of file