New: Watch list sorting and rate limit for Trakt Import Lists

This commit is contained in:
Bogdan 2023-12-18 03:13:37 +02:00 committed by Mark McDowall
parent 84c95f5a9d
commit 0673374e97
5 changed files with 57 additions and 28 deletions

View File

@ -26,11 +26,7 @@ namespace NzbDrone.Core.ImportLists.Trakt.User
public override IImportListRequestGenerator GetRequestGenerator() public override IImportListRequestGenerator GetRequestGenerator()
{ {
return new TraktUserRequestGenerator() return new TraktUserRequestGenerator(Settings, ClientId);
{
Settings = Settings,
ClientId = ClientId
};
} }
} }
} }

View File

@ -1,14 +1,14 @@
using System.Runtime.Serialization; using NzbDrone.Core.Annotations;
namespace NzbDrone.Core.ImportLists.Trakt.User namespace NzbDrone.Core.ImportLists.Trakt.User
{ {
public enum TraktUserListType public enum TraktUserListType
{ {
[EnumMember(Value = "User Watch List")] [FieldOption(Label = "User Watch List")]
UserWatchList = 0, UserWatchList = 0,
[EnumMember(Value = "User Watched List")] [FieldOption(Label = "User Watched List")]
UserWatchedList = 1, UserWatchedList = 1,
[EnumMember(Value = "User Collection List")] [FieldOption(Label = "User Collection List")]
UserCollectionList = 2 UserCollectionList = 2
} }
} }

View File

@ -6,9 +6,14 @@ namespace NzbDrone.Core.ImportLists.Trakt.User
{ {
public class TraktUserRequestGenerator : IImportListRequestGenerator 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() public virtual ImportListPageableRequestChain GetListItems()
{ {
@ -21,33 +26,49 @@ namespace NzbDrone.Core.ImportLists.Trakt.User
private IEnumerable<ImportListRequest> GetSeriesRequest() private IEnumerable<ImportListRequest> GetSeriesRequest()
{ {
var link = Settings.BaseUrl.Trim(); var requestBuilder = new HttpRequestBuilder(_settings.BaseUrl.Trim());
var userName = Settings.Username.IsNotNullOrWhiteSpace() ? Settings.Username.Trim() : Settings.AuthUser.Trim();
switch (Settings.TraktListType) switch (_settings.TraktListType)
{ {
case (int)TraktUserListType.UserWatchList: 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; break;
case (int)TraktUserListType.UserWatchedList: case (int)TraktUserListType.UserWatchedList:
link += $"/users/{userName}/watched/shows?extended=full&limit={Settings.Limit}"; requestBuilder
.Resource("/users/{userName}/watched/shows")
.AddQueryParam("extended", "full");
break; break;
case (int)TraktUserListType.UserCollectionList: case (int)TraktUserListType.UserCollectionList:
link += $"/users/{userName}/collection/shows?limit={Settings.Limit}"; requestBuilder.Resource("/users/{userName}/collection/shows");
break; 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"); requestBuilder
request.HttpRequest.Headers.Add("trakt-api-key", ClientId); .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());
} }
} }
} }

View File

@ -22,6 +22,7 @@ namespace NzbDrone.Core.ImportLists.Trakt.User
{ {
TraktListType = (int)TraktUserListType.UserWatchList; TraktListType = (int)TraktUserListType.UserWatchList;
TraktWatchedListType = (int)TraktUserWatchedListType.All; 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")] [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")] [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; } 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 string Username { get; set; }
} }
public enum TraktUserWatchSorting
{
Rank = 0,
Added = 1,
Title = 2,
Released = 3
}
} }

View File

@ -1,14 +1,14 @@
using System.Runtime.Serialization; using NzbDrone.Core.Annotations;
namespace NzbDrone.Core.ImportLists.Trakt.User namespace NzbDrone.Core.ImportLists.Trakt.User
{ {
public enum TraktUserWatchedListType public enum TraktUserWatchedListType
{ {
[EnumMember(Value = "All")] [FieldOption(Label = "All")]
All = 0, All = 0,
[EnumMember(Value = "In Progress")] [FieldOption(Label = "In Progress")]
InProgress = 1, InProgress = 1,
[EnumMember(Value = "100% Watched")] [FieldOption(Label = "100% Watched")]
CompletelyWatched = 2 CompletelyWatched = 2
} }
} }