From b779c92200b8e14398061199535bb3eb0841f626 Mon Sep 17 00:00:00 2001 From: Jessica Nguyen <122261093+jtnguyen45@users.noreply.github.com> Date: Sat, 22 Jul 2023 17:11:22 -0700 Subject: [PATCH] New: Simkl Anime List integration Co-authored-by: iceypotato Closes #5635 --- .../ImportLists/Simkl/SimklAPI.cs | 11 +++++ .../ImportLists/Simkl/SimklParser.cs | 47 ++++++++++++++++--- .../Simkl/User/SimklUserRequestGenerator.cs | 2 +- .../Simkl/User/SimklUserSettings.cs | 4 ++ .../Simkl/User/SimklUserShowType.cs | 8 ++++ .../Parser/Model/ImportListItemInfo.cs | 1 + 6 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserShowType.cs diff --git a/src/NzbDrone.Core/ImportLists/Simkl/SimklAPI.cs b/src/NzbDrone.Core/ImportLists/Simkl/SimklAPI.cs index 4472af249..0290dabfb 100644 --- a/src/NzbDrone.Core/ImportLists/Simkl/SimklAPI.cs +++ b/src/NzbDrone.Core/ImportLists/Simkl/SimklAPI.cs @@ -11,6 +11,7 @@ namespace NzbDrone.Core.ImportLists.Simkl public string Imdb { get; set; } public string Tmdb { get; set; } public string Tvdb { get; set; } + public string Mal { get; set; } } public class SimklSeriesPropsResource @@ -23,11 +24,15 @@ namespace NzbDrone.Core.ImportLists.Simkl public class SimklSeriesResource { public SimklSeriesPropsResource Show { get; set; } + + [JsonProperty("anime_type")] + public SimklAnimeType AnimeType { get; set; } } public class SimklResponse { public List Shows { get; set; } + public List Anime { get; set; } } public class RefreshRequestResponse @@ -66,4 +71,10 @@ namespace NzbDrone.Core.ImportLists.Simkl { public DateTime All { get; set; } } + + public enum SimklAnimeType + { + Tv, + Movie + } } diff --git a/src/NzbDrone.Core/ImportLists/Simkl/SimklParser.cs b/src/NzbDrone.Core/ImportLists/Simkl/SimklParser.cs index 6f5758369..1419c6be7 100644 --- a/src/NzbDrone.Core/ImportLists/Simkl/SimklParser.cs +++ b/src/NzbDrone.Core/ImportLists/Simkl/SimklParser.cs @@ -1,6 +1,8 @@ using System.Collections.Generic; using System.Net; +using NLog; using NzbDrone.Common.Extensions; +using NzbDrone.Common.Instrumentation; using NzbDrone.Common.Serializer; using NzbDrone.Core.ImportLists.Exceptions; using NzbDrone.Core.Parser.Model; @@ -10,6 +12,11 @@ namespace NzbDrone.Core.ImportLists.Simkl public class SimklParser : IParseImportListResponse { private ImportListResponse _importResponse; + private static readonly Logger Logger = NzbDroneLogger.GetLogger(typeof(SimklParser)); + + public SimklParser() + { + } public virtual IList ParseResponse(ImportListResponse importResponse) { @@ -22,7 +29,7 @@ namespace NzbDrone.Core.ImportLists.Simkl return series; } - var jsonResponse = STJson.Deserialize(_importResponse.Content); + var jsonResponse = Json.Deserialize(_importResponse.Content); // no shows were return if (jsonResponse == null) @@ -30,14 +37,40 @@ namespace NzbDrone.Core.ImportLists.Simkl return series; } - foreach (var show in jsonResponse.Shows) + if (jsonResponse.Anime != null) { - series.AddIfNotNull(new ImportListItemInfo() + foreach (var show in jsonResponse.Anime) { - Title = show.Show.Title, - TvdbId = int.TryParse(show.Show.Ids.Tvdb, out var tvdbId) ? tvdbId : 0, - ImdbId = show.Show.Ids.Imdb - }); + var tentativeTvdbId = int.TryParse(show.Show.Ids.Tvdb, out var tvdbId) ? tvdbId : 0; + + if (tentativeTvdbId > 0 && show.AnimeType == SimklAnimeType.Tv) + { + series.AddIfNotNull(new ImportListItemInfo() + { + Title = show.Show.Title, + ImdbId = show.Show.Ids.Imdb, + TvdbId = tvdbId, + MalId = int.TryParse(show.Show.Ids.Mal, out var malId) ? malId : 0 + }); + } + else + { + Logger.Warn("Skipping info grabbing for '{0}' because it is a movie or it is not the first season of the show", show.Show.Title); + } + } + } + + if (jsonResponse.Shows != null) + { + foreach (var show in jsonResponse.Shows) + { + series.AddIfNotNull(new ImportListItemInfo() + { + Title = show.Show.Title, + TvdbId = int.TryParse(show.Show.Ids.Tvdb, out var tvdbId) ? tvdbId : 0, + ImdbId = show.Show.Ids.Imdb + }); + } } return series; diff --git a/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserRequestGenerator.cs b/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserRequestGenerator.cs index 90bdccf1a..74ea05831 100644 --- a/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserRequestGenerator.cs +++ b/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserRequestGenerator.cs @@ -21,7 +21,7 @@ namespace NzbDrone.Core.ImportLists.Simkl.User private IEnumerable GetSeriesRequest() { - var link = $"{Settings.BaseUrl.Trim()}/sync/all-items/shows/{((SimklUserListType)Settings.ListType).ToString().ToLowerInvariant()}"; + var link = $"{Settings.BaseUrl.Trim()}/sync/all-items/{((SimklUserShowType)Settings.ShowType).ToString().ToLowerInvariant()}/{((SimklUserListType)Settings.ListType).ToString().ToLowerInvariant()}"; var request = new ImportListRequest(link, HttpAccept.Json); diff --git a/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserSettings.cs b/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserSettings.cs index 61cc48129..65f19aa3f 100644 --- a/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserSettings.cs +++ b/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserSettings.cs @@ -19,9 +19,13 @@ namespace NzbDrone.Core.ImportLists.Simkl.User public SimklUserSettings() { ListType = (int)SimklUserListType.Watching; + ShowType = (int)SimklUserShowType.Shows; } [FieldDefinition(1, Label = "List Type", Type = FieldType.Select, SelectOptions = typeof(SimklUserListType), HelpText = "Type of list you're seeking to import from")] public int ListType { get; set; } + + [FieldDefinition(1, Label = "Show Type", Type = FieldType.Select, SelectOptions = typeof(SimklUserShowType), HelpText = "Type of show you're seeking to import from")] + public int ShowType { get; set; } } } diff --git a/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserShowType.cs b/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserShowType.cs new file mode 100644 index 000000000..8ff2eb16d --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserShowType.cs @@ -0,0 +1,8 @@ +namespace NzbDrone.Core.ImportLists.Simkl.User +{ + public enum SimklUserShowType + { + Shows = 0, + Anime = 1 + } +} diff --git a/src/NzbDrone.Core/Parser/Model/ImportListItemInfo.cs b/src/NzbDrone.Core/Parser/Model/ImportListItemInfo.cs index df76ac2c2..182c2fca9 100644 --- a/src/NzbDrone.Core/Parser/Model/ImportListItemInfo.cs +++ b/src/NzbDrone.Core/Parser/Model/ImportListItemInfo.cs @@ -11,6 +11,7 @@ namespace NzbDrone.Core.Parser.Model public int TvdbId { get; set; } public int TmdbId { get; set; } public string ImdbId { get; set; } + public int MalId { get; set; } public DateTime ReleaseDate { get; set; } public override string ToString()