From 0673374e97bf508fca935bed222379ad26252f22 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Mon, 18 Dec 2023 03:13:37 +0200 Subject: [PATCH] New: Watch list sorting and rate limit for Trakt Import Lists --- .../ImportLists/Trakt/User/TraktUserImport.cs | 6 +-- .../Trakt/User/TraktUserListType.cs | 8 +-- .../Trakt/User/TraktUserRequestGenerator.cs | 49 +++++++++++++------ .../Trakt/User/TraktUserSettings.cs | 14 +++++- .../Trakt/User/TraktUserWatchedListType.cs | 8 +-- 5 files changed, 57 insertions(+), 28 deletions(-) diff --git a/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserImport.cs b/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserImport.cs index 76bcf71d7..9a826112a 100644 --- a/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserImport.cs +++ b/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserImport.cs @@ -26,11 +26,7 @@ namespace NzbDrone.Core.ImportLists.Trakt.User public override IImportListRequestGenerator GetRequestGenerator() { - return new TraktUserRequestGenerator() - { - Settings = Settings, - ClientId = ClientId - }; + return new TraktUserRequestGenerator(Settings, ClientId); } } } diff --git a/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserListType.cs b/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserListType.cs index b90508d14..8a0720c23 100644 --- a/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserListType.cs +++ b/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserListType.cs @@ -1,14 +1,14 @@ -using System.Runtime.Serialization; +using NzbDrone.Core.Annotations; namespace NzbDrone.Core.ImportLists.Trakt.User { public enum TraktUserListType { - [EnumMember(Value = "User Watch List")] + [FieldOption(Label = "User Watch List")] UserWatchList = 0, - [EnumMember(Value = "User Watched List")] + [FieldOption(Label = "User Watched List")] UserWatchedList = 1, - [EnumMember(Value = "User Collection List")] + [FieldOption(Label = "User Collection List")] UserCollectionList = 2 } } diff --git a/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserRequestGenerator.cs b/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserRequestGenerator.cs index 8f7c0396d..4ed21a84c 100644 --- a/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserRequestGenerator.cs +++ b/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserRequestGenerator.cs @@ -6,9 +6,14 @@ namespace NzbDrone.Core.ImportLists.Trakt.User { public class TraktUserRequestGenerator : IImportListRequestGenerator { - public TraktUserSettings Settings { get; set; } + private readonly TraktUserSettings _settings; + private readonly string _clientId; - public string ClientId { get; set; } + public TraktUserRequestGenerator(TraktUserSettings settings, string clientId) + { + _settings = settings; + _clientId = clientId; + } public virtual ImportListPageableRequestChain GetListItems() { @@ -21,33 +26,49 @@ namespace NzbDrone.Core.ImportLists.Trakt.User private IEnumerable GetSeriesRequest() { - var link = Settings.BaseUrl.Trim(); - var userName = Settings.Username.IsNotNullOrWhiteSpace() ? Settings.Username.Trim() : Settings.AuthUser.Trim(); + var requestBuilder = new HttpRequestBuilder(_settings.BaseUrl.Trim()); - switch (Settings.TraktListType) + switch (_settings.TraktListType) { case (int)TraktUserListType.UserWatchList: - link += $"/users/{userName}/watchlist/shows?limit={Settings.Limit}"; + var watchSorting = _settings.TraktWatchSorting switch + { + (int)TraktUserWatchSorting.Added => "added", + (int)TraktUserWatchSorting.Title => "title", + (int)TraktUserWatchSorting.Released => "released", + _ => "rank" + }; + + requestBuilder + .Resource("/users/{userName}/watchlist/shows/{sorting}") + .SetSegment("sorting", watchSorting); break; case (int)TraktUserListType.UserWatchedList: - link += $"/users/{userName}/watched/shows?extended=full&limit={Settings.Limit}"; + requestBuilder + .Resource("/users/{userName}/watched/shows") + .AddQueryParam("extended", "full"); break; case (int)TraktUserListType.UserCollectionList: - link += $"/users/{userName}/collection/shows?limit={Settings.Limit}"; + requestBuilder.Resource("/users/{userName}/collection/shows"); break; } - var request = new ImportListRequest(link, HttpAccept.Json); + var userName = _settings.Username.IsNotNullOrWhiteSpace() ? _settings.Username.Trim() : _settings.AuthUser.Trim(); - request.HttpRequest.Headers.Add("trakt-api-version", "2"); - request.HttpRequest.Headers.Add("trakt-api-key", ClientId); + requestBuilder + .SetSegment("userName", userName) + .Accept(HttpAccept.Json) + .WithRateLimit(4) + .SetHeader("trakt-api-version", "2") + .SetHeader("trakt-api-key", _clientId) + .AddQueryParam("limit", _settings.Limit.ToString()); - if (Settings.AccessToken.IsNotNullOrWhiteSpace()) + if (_settings.AccessToken.IsNotNullOrWhiteSpace()) { - request.HttpRequest.Headers.Add("Authorization", "Bearer " + Settings.AccessToken); + requestBuilder.SetHeader("Authorization", $"Bearer {_settings.AccessToken}"); } - yield return request; + yield return new ImportListRequest(requestBuilder.Build()); } } } diff --git a/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserSettings.cs b/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserSettings.cs index 057f3edca..f6ccb7a9c 100644 --- a/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserSettings.cs +++ b/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserSettings.cs @@ -22,6 +22,7 @@ namespace NzbDrone.Core.ImportLists.Trakt.User { TraktListType = (int)TraktUserListType.UserWatchList; TraktWatchedListType = (int)TraktUserWatchedListType.All; + TraktWatchSorting = (int)TraktUserWatchSorting.Rank; } [FieldDefinition(1, Label = "List Type", Type = FieldType.Select, SelectOptions = typeof(TraktUserListType), HelpText = "Type of list you're seeking to import from")] @@ -30,7 +31,18 @@ namespace NzbDrone.Core.ImportLists.Trakt.User [FieldDefinition(2, Label = "Watched List Filter", Type = FieldType.Select, SelectOptions = typeof(TraktUserWatchedListType), HelpText = "If List Type is Watched. Series do you want to import from")] public int TraktWatchedListType { get; set; } - [FieldDefinition(3, Label = "Username", HelpText = "Username for the List to import from (empty to use Auth User)")] + [FieldDefinition(3, Label = "Watch List Sorting", Type = FieldType.Select, SelectOptions = typeof(TraktUserWatchSorting), HelpText = "If List Type is Watch")] + public int TraktWatchSorting { get; set; } + + [FieldDefinition(4, Label = "Username", HelpText = "Username for the List to import from (empty to use Auth User)")] public string Username { get; set; } } + + public enum TraktUserWatchSorting + { + Rank = 0, + Added = 1, + Title = 2, + Released = 3 + } } diff --git a/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserWatchedListType.cs b/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserWatchedListType.cs index 5b6526e6e..396ce5987 100644 --- a/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserWatchedListType.cs +++ b/src/NzbDrone.Core/ImportLists/Trakt/User/TraktUserWatchedListType.cs @@ -1,14 +1,14 @@ -using System.Runtime.Serialization; +using NzbDrone.Core.Annotations; namespace NzbDrone.Core.ImportLists.Trakt.User { public enum TraktUserWatchedListType { - [EnumMember(Value = "All")] + [FieldOption(Label = "All")] All = 0, - [EnumMember(Value = "In Progress")] + [FieldOption(Label = "In Progress")] InProgress = 1, - [EnumMember(Value = "100% Watched")] + [FieldOption(Label = "100% Watched")] CompletelyWatched = 2 } }