From 4c0179ad4d572fd3f26a6c46122dff814aec1fb8 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 19 Jul 2021 17:52:24 +0200 Subject: [PATCH] Fixing tvdb provider and adding fields=all --- Kyoo.Common/Models/Link.cs | 5 ++-- Kyoo.Common/Models/Resources/Show.cs | 2 ++ Kyoo.CommonAPI/ResourceViewAttribute.cs | 39 +++++++++++++++---------- Kyoo.TheTvdb/Convertors.cs | 6 ++-- Kyoo.TheTvdb/ProviderTvdb.cs | 33 +++++++++++++++++++-- Kyoo/Tasks/Crawler.cs | 2 +- Kyoo/Tasks/RegisterEpisode.cs | 7 +++-- Kyoo/settings.json | 4 +++ 8 files changed, 72 insertions(+), 26 deletions(-) diff --git a/Kyoo.Common/Models/Link.cs b/Kyoo.Common/Models/Link.cs index 6d815af9..09c519da 100644 --- a/Kyoo.Common/Models/Link.cs +++ b/Kyoo.Common/Models/Link.cs @@ -1,5 +1,6 @@ using System; using System.Linq.Expressions; +using Kyoo.Models.Attributes; namespace Kyoo.Models { @@ -107,12 +108,12 @@ namespace Kyoo.Models /// /// A reference of the first resource. /// - public T1 First { get; set; } + [SerializeIgnore] public T1 First { get; set; } /// /// A reference to the second resource. /// - public T2 Second { get; set; } + [SerializeIgnore] public T2 Second { get; set; } /// diff --git a/Kyoo.Common/Models/Resources/Show.cs b/Kyoo.Common/Models/Resources/Show.cs index ffb2ae49..656d6ffb 100644 --- a/Kyoo.Common/Models/Resources/Show.cs +++ b/Kyoo.Common/Models/Resources/Show.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using JetBrains.Annotations; using Kyoo.Common.Models.Attributes; using Kyoo.Controllers; using Kyoo.Models.Attributes; @@ -157,6 +158,7 @@ namespace Kyoo.Models /// This method will never return anything if the are not loaded. /// The slug of the provider /// The field of the asked provider. + [CanBeNull] public string GetID(string provider) { return ExternalIDs?.FirstOrDefault(x => x.Second.Slug == provider)?.DataID; diff --git a/Kyoo.CommonAPI/ResourceViewAttribute.cs b/Kyoo.CommonAPI/ResourceViewAttribute.cs index 06198663..487373c4 100644 --- a/Kyoo.CommonAPI/ResourceViewAttribute.cs +++ b/Kyoo.CommonAPI/ResourceViewAttribute.cs @@ -43,22 +43,31 @@ namespace Kyoo.CommonApi PropertyInfo[] properties = type.GetProperties() .Where(x => x.GetCustomAttribute() != null) .ToArray(); - fields = fields.Select(x => - { - string property = properties - .FirstOrDefault(y => string.Equals(x, y.Name, StringComparison.InvariantCultureIgnoreCase)) - ?.Name; - if (property != null) - return property; - context.Result = new BadRequestObjectResult(new + if (fields.Count == 1 && fields.Contains("all")) + { + fields = properties.Select(x => x.Name).ToList(); + } + else + { + fields = fields + .Select(x => { - Error = $"{x} does not exist on {type.Name}." - }); - return null; - }) - .ToList(); - if (context.Result != null) - return; + string property = properties + .FirstOrDefault(y + => string.Equals(x, y.Name, StringComparison.InvariantCultureIgnoreCase)) + ?.Name; + if (property != null) + return property; + context.Result = new BadRequestObjectResult(new + { + Error = $"{x} does not exist on {type.Name}." + }); + return null; + }) + .ToList(); + if (context.Result != null) + return; + } } context.HttpContext.Items["fields"] = fields; base.OnActionExecuting(context); diff --git a/Kyoo.TheTvdb/Convertors.cs b/Kyoo.TheTvdb/Convertors.cs index b7edd2ea..6aa5c3ef 100644 --- a/Kyoo.TheTvdb/Convertors.cs +++ b/Kyoo.TheTvdb/Convertors.cs @@ -33,7 +33,8 @@ namespace Kyoo.TheTvdb /// The parsed or null. private static DateTime ParseDate(string date) { - DateTime.TryParseExact(date, "yyyy-mm-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime parsed); + DateTime.TryParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture, + DateTimeStyles.None, out DateTime parsed); return parsed; } @@ -122,7 +123,8 @@ namespace Kyoo.TheTvdb } } }, - Role = actor.Role + Role = actor.Role, + Type = "Actor" }; } diff --git a/Kyoo.TheTvdb/ProviderTvdb.cs b/Kyoo.TheTvdb/ProviderTvdb.cs index c6d57fd0..78834bfd 100644 --- a/Kyoo.TheTvdb/ProviderTvdb.cs +++ b/Kyoo.TheTvdb/ProviderTvdb.cs @@ -71,11 +71,21 @@ namespace Kyoo.TheTvdb }; } + /// + /// Retrieve metadata about a show. + /// + /// The base show to retrieve metadata for. + /// A new show filled with metadata from the tvdb. [ItemCanBeNull] private async Task _GetShow([NotNull] Show show) { if (!int.TryParse(show.GetID(Provider.Slug), out int id)) - return (await _SearchShow(show.Title)).FirstOrDefault(); + { + Show found = (await _SearchShow(show.Title)).FirstOrDefault(); + if (found == null) + return null; + return await Get(found); + } TvDbResponse series = await _client.Series.GetAsync(id); Show ret = series.Data.ToShow(Provider); @@ -84,6 +94,11 @@ namespace Kyoo.TheTvdb return ret; } + /// + /// Retrieve metadata about an episode. + /// + /// The base episode to retrieve metadata for. + /// A new episode filled with metadata from the tvdb. [ItemCanBeNull] private async Task _GetEpisode([NotNull] Episode episode) { @@ -106,11 +121,23 @@ namespace Kyoo.TheTvdb return ArraySegment.Empty; } + /// + /// Search for shows in the tvdb. + /// + /// The query to ask the tvdb about. + /// A list of shows that could be found on the tvdb. [ItemNotNull] private async Task> _SearchShow(string query) { - TvDbResponse shows = await _client.Search.SearchSeriesByNameAsync(query); - return shows.Data.Select(x => x.ToShow(Provider)).ToArray(); + try + { + TvDbResponse shows = await _client.Search.SearchSeriesByNameAsync(query); + return shows.Data.Select(x => x.ToShow(Provider)).ToArray(); + } + catch (TvDbServerException) + { + return ArraySegment.Empty; + } } } } \ No newline at end of file diff --git a/Kyoo/Tasks/Crawler.cs b/Kyoo/Tasks/Crawler.cs index e65bdd87..0d38e34e 100644 --- a/Kyoo/Tasks/Crawler.cs +++ b/Kyoo/Tasks/Crawler.cs @@ -150,7 +150,7 @@ namespace Kyoo.Tasks string[] subtitles = files .Where(FileExtensions.IsSubtitle) - .Where(x => x.Contains("/Extra/")) + .Where(x => !x.Contains("/Extra/")) .Where(x => tracks.All(y => y.Path != x)) .ToArray(); percent = 0; diff --git a/Kyoo/Tasks/RegisterEpisode.cs b/Kyoo/Tasks/RegisterEpisode.cs index 0498dd68..674ae477 100644 --- a/Kyoo/Tasks/RegisterEpisode.cs +++ b/Kyoo/Tasks/RegisterEpisode.cs @@ -112,14 +112,15 @@ namespace Kyoo.Tasks if (season != null) season.Show = show; - season = await _RegisterAndFill(season); + if (season != null) + season.Title ??= $"Season {season.SeasonNumber}"; progress.Report(60); - episode = await MetadataProvider.Get(episode); - progress.Report(70); episode.Show = show; episode.Season = season; + episode = await MetadataProvider.Get(episode); + progress.Report(70); episode.Tracks = (await Transcoder.ExtractInfos(episode, false)) .Where(x => x.Type != StreamType.Attachment) .ToArray(); diff --git a/Kyoo/settings.json b/Kyoo/settings.json index 3233e7f5..ffc9b446 100644 --- a/Kyoo/settings.json +++ b/Kyoo/settings.json @@ -60,5 +60,9 @@ }, "profilePicturePath": "users/", "clients": [] + }, + + "tvdb": { + "apiKey": "REDACTED" } }