New: Simkl List Support

(cherry picked from commit 4a740acb801a04bc2ead45d272d493f4ec46f7e8)
pull/8794/head
Qstick 12 months ago
parent bc004b3b5b
commit 916d43d70d

@ -6,6 +6,7 @@ namespace NzbDrone.Core.ImportLists
TMDB,
Trakt,
Plex,
Simkl,
Other,
Advanced
}

@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace NzbDrone.Core.ImportLists.Simkl
{
public class SimklMovieIdsResource
{
public int Simkl { get; set; }
public string Imdb { get; set; }
public string Tmdb { get; set; }
}
public class SimklMoviePropsResource
{
public string Title { get; set; }
public int? Year { get; set; }
public SimklMovieIdsResource Ids { get; set; }
}
public class SimklMovieResource
{
public SimklMoviePropsResource Movie { get; set; }
}
public class SimklResponse
{
public List<SimklMovieResource> Movies { get; set; }
}
public class RefreshRequestResponse
{
[JsonProperty("access_token")]
public string AccessToken { get; set; }
[JsonProperty("expires_in")]
public int ExpiresIn { get; set; }
[JsonProperty("refresh_token")]
public string RefreshToken { get; set; }
}
public class UserSettingsResponse
{
public SimklUserResource User { get; set; }
public SimklUserAccountResource Account { get; set; }
}
public class SimklUserResource
{
public string Name { get; set; }
}
public class SimklUserAccountResource
{
public int Id { get; set; }
}
public class SimklSyncActivityResource
{
public SimklMoviesSyncActivityResource Movies { get; set; }
}
public class SimklMoviesSyncActivityResource
{
public DateTime All { get; set; }
}
}

@ -0,0 +1,182 @@
using System;
using System.Collections.Generic;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.ImportLists.Simkl
{
public abstract class SimklImportBase<TSettings> : HttpImportListBase<TSettings>
where TSettings : SimklSettingsBase<TSettings>, new()
{
public override ImportListType ListType => ImportListType.Simkl;
public override TimeSpan MinRefreshInterval => TimeSpan.FromHours(6);
public const string OAuthUrl = "https://simkl.com/oauth/authorize";
public const string RedirectUri = "https://auth.servarr.com/v1/simkl/auth";
public const string RenewUri = "https://auth.servarr.com/v1/simkl/renew";
public const string ClientId = "0d6182f377fb3a6feca551649c203f8af34661125e4c143c7db069e24872cf58";
private IImportListRepository _importListRepository;
protected SimklImportBase(IImportListRepository netImportRepository,
IHttpClient httpClient,
IImportListStatusService importListStatusService,
IConfigService configService,
IParsingService parsingService,
Logger logger)
: base(httpClient, importListStatusService, configService, parsingService, logger)
{
_importListRepository = netImportRepository;
}
public override ImportListFetchResult Fetch()
{
Settings.Validate().Filter("AccessToken", "RefreshToken").ThrowOnError();
_logger.Trace($"Access token expires at {Settings.Expires}");
// Simkl doesn't currently expire access tokens, but if they start lets be prepared
if (Settings.RefreshToken.IsNotNullOrWhiteSpace() && Settings.Expires < DateTime.UtcNow.AddMinutes(5))
{
RefreshToken();
}
var lastFetch = _importListStatusService.GetLastSyncListInfo(Definition.Id);
var lastActivity = GetLastActivity();
// Check to see if user has any activity since last sync, if not return empty to avoid work
if (lastFetch.HasValue && lastActivity < lastFetch.Value.AddHours(-2))
{
return new ImportListFetchResult();
}
var generator = GetRequestGenerator();
return FetchMovies(generator.GetMovies());
}
public override IParseImportListResponse GetParser()
{
return new SimklParser();
}
public override object RequestAction(string action, IDictionary<string, string> query)
{
if (action == "startOAuth")
{
var request = new HttpRequestBuilder(OAuthUrl)
.AddQueryParam("client_id", ClientId)
.AddQueryParam("response_type", "code")
.AddQueryParam("redirect_uri", RedirectUri)
.AddQueryParam("state", query["callbackUrl"])
.Build();
return new
{
OauthUrl = request.Url.ToString()
};
}
else if (action == "getOAuthToken")
{
return new
{
accessToken = query["access_token"],
expires = DateTime.UtcNow.AddSeconds(int.Parse(query["expires_in"])),
refreshToken = query["refresh_token"],
authUser = GetUserId(query["access_token"])
};
}
return new { };
}
private DateTime GetLastActivity()
{
var request = new HttpRequestBuilder(string.Format("{0}/sync/activities", Settings.BaseUrl)).Build();
request.Headers.Add("simkl-api-key", ClientId);
request.Headers.Add("Authorization", "Bearer " + Settings.AccessToken);
try
{
var response = _httpClient.Get<SimklSyncActivityResource>(request);
if (response?.Resource != null)
{
return response.Resource.Movies.All;
}
}
catch (HttpException)
{
_logger.Warn($"Error fetching user activity");
}
return DateTime.UtcNow;
}
private string GetUserId(string accessToken)
{
var request = new HttpRequestBuilder(string.Format("{0}/users/settings", Settings.BaseUrl))
.Build();
request.Headers.Add("simkl-api-key", ClientId);
if (accessToken.IsNotNullOrWhiteSpace())
{
request.Headers.Add("Authorization", "Bearer " + accessToken);
}
try
{
var response = _httpClient.Get<UserSettingsResponse>(request);
if (response?.Resource != null)
{
return response.Resource.Account.Id.ToString();
}
}
catch (HttpException)
{
_logger.Warn($"Error refreshing simkl access token");
}
return null;
}
private void RefreshToken()
{
_logger.Trace("Refreshing Token");
Settings.Validate().Filter("RefreshToken").ThrowOnError();
var request = new HttpRequestBuilder(RenewUri)
.AddQueryParam("refresh_token", Settings.RefreshToken)
.Build();
try
{
var response = _httpClient.Get<RefreshRequestResponse>(request);
if (response?.Resource != null)
{
var token = response.Resource;
Settings.AccessToken = token.AccessToken;
Settings.Expires = DateTime.UtcNow.AddSeconds(token.ExpiresIn);
Settings.RefreshToken = token.RefreshToken ?? Settings.RefreshToken;
if (Definition.Id > 0)
{
_importListRepository.UpdateSettings((ImportListDefinition)Definition);
}
}
}
catch (HttpException)
{
_logger.Warn($"Error refreshing simkl access token");
}
}
}
}

@ -0,0 +1,66 @@
using System.Collections.Generic;
using System.Net;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.ImportLists.Exceptions;
using NzbDrone.Core.ImportLists.ImportListMovies;
namespace NzbDrone.Core.ImportLists.Simkl
{
public class SimklParser : IParseImportListResponse
{
private ImportListResponse _importResponse;
public SimklParser()
{
}
public virtual IList<ImportListMovie> ParseResponse(ImportListResponse importResponse)
{
_importResponse = importResponse;
var movie = new List<ImportListMovie>();
if (!PreProcess(_importResponse))
{
return movie;
}
var jsonResponse = STJson.Deserialize<SimklResponse>(_importResponse.Content);
// no shows were return
if (jsonResponse == null)
{
return movie;
}
foreach (var show in jsonResponse.Movies)
{
movie.AddIfNotNull(new ImportListMovie()
{
Title = show.Movie.Title,
TmdbId = int.TryParse(show.Movie.Ids.Tmdb, out var tmdbId) ? tmdbId : 0,
ImdbId = show.Movie.Ids.Imdb
});
}
return movie;
}
protected virtual bool PreProcess(ImportListResponse netImportResponse)
{
if (netImportResponse.HttpResponse.StatusCode != HttpStatusCode.OK)
{
throw new ImportListException(netImportResponse, "Simkl API call resulted in an unexpected StatusCode [{0}]", netImportResponse.HttpResponse.StatusCode);
}
if (netImportResponse.HttpResponse.Headers.ContentType != null && netImportResponse.HttpResponse.Headers.ContentType.Contains("text/json") &&
netImportResponse.HttpRequest.Headers.Accept != null && !netImportResponse.HttpRequest.Headers.Accept.Contains("text/json"))
{
throw new ImportListException(netImportResponse, "Simkl API responded with html content. Site is likely blocked or unavailable.");
}
return true;
}
}
}

@ -0,0 +1,61 @@
using System;
using FluentValidation;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.ImportLists.Simkl
{
public class SimklSettingsBaseValidator<TSettings> : AbstractValidator<TSettings>
where TSettings : SimklSettingsBase<TSettings>
{
public SimklSettingsBaseValidator()
{
RuleFor(c => c.BaseUrl).ValidRootUrl();
RuleFor(c => c.AccessToken).NotEmpty()
.OverridePropertyName("SignIn")
.WithMessage("Must authenticate with Simkl");
RuleFor(c => c.Expires).NotEmpty()
.OverridePropertyName("SignIn")
.WithMessage("Must authenticate with Simkl")
.When(c => c.AccessToken.IsNotNullOrWhiteSpace() && c.RefreshToken.IsNotNullOrWhiteSpace());
}
}
public class SimklSettingsBase<TSettings> : IProviderConfig
where TSettings : SimklSettingsBase<TSettings>
{
protected virtual AbstractValidator<TSettings> Validator => new SimklSettingsBaseValidator<TSettings>();
public SimklSettingsBase()
{
BaseUrl = "https://api.simkl.com";
SignIn = "startOAuth";
}
public string BaseUrl { get; set; }
[FieldDefinition(0, Label = "Access Token", Type = FieldType.Textbox, Hidden = HiddenType.Hidden)]
public string AccessToken { get; set; }
[FieldDefinition(0, Label = "Refresh Token", Type = FieldType.Textbox, Hidden = HiddenType.Hidden)]
public string RefreshToken { get; set; }
[FieldDefinition(0, Label = "Expires", Type = FieldType.Textbox, Hidden = HiddenType.Hidden)]
public DateTime Expires { get; set; }
[FieldDefinition(0, Label = "Auth User", Type = FieldType.Textbox, Hidden = HiddenType.Hidden)]
public string AuthUser { get; set; }
[FieldDefinition(99, Label = "Authenticate with Simkl", Type = FieldType.OAuth)]
public string SignIn { get; set; }
public NzbDroneValidationResult Validate()
{
return new NzbDroneValidationResult(Validator.Validate((TSettings)this));
}
}
}

@ -0,0 +1,33 @@
using NLog;
using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Parser;
namespace NzbDrone.Core.ImportLists.Simkl.User
{
public class SimklUserImport : SimklImportBase<SimklUserSettings>
{
public SimklUserImport(IImportListRepository netImportRepository,
IHttpClient httpClient,
IImportListStatusService netImportStatusService,
IConfigService configService,
IParsingService parsingService,
Logger logger)
: base(netImportRepository, httpClient, netImportStatusService, configService, parsingService, logger)
{
}
public override string Name => "Simkl User Watchlist";
public override bool Enabled => true;
public override bool EnableAuto => false;
public override IImportListRequestGenerator GetRequestGenerator()
{
return new SimklUserRequestGenerator()
{
Settings = Settings,
ClientId = ClientId
};
}
}
}

@ -0,0 +1,18 @@
using System.Runtime.Serialization;
namespace NzbDrone.Core.ImportLists.Simkl.User
{
public enum SimklUserListType
{
[EnumMember(Value = "Watching")]
Watching = 0,
[EnumMember(Value = "Plan To Watch")]
PlanToWatch = 1,
[EnumMember(Value = "Hold")]
Hold = 2,
[EnumMember(Value = "Completed")]
Completed = 3,
[EnumMember(Value = "Dropped")]
Dropped = 4
}
}

@ -0,0 +1,42 @@
using System.Collections.Generic;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
namespace NzbDrone.Core.ImportLists.Simkl.User
{
public class SimklUserRequestGenerator : IImportListRequestGenerator
{
public SimklUserSettings Settings { get; set; }
public string ClientId { get; set; }
public SimklUserRequestGenerator()
{
}
public virtual ImportListPageableRequestChain GetMovies()
{
var pageableRequests = new ImportListPageableRequestChain();
pageableRequests.Add(GetSeriesRequest());
return pageableRequests;
}
private IEnumerable<ImportListRequest> GetSeriesRequest()
{
var link = $"{Settings.BaseUrl.Trim()}/sync/all-items/movies/{((SimklUserListType)Settings.ListType).ToString().ToLowerInvariant()}";
var request = new ImportListRequest(link, HttpAccept.Json);
request.HttpRequest.Headers.Add("simkl-api-key", ClientId);
if (Settings.AccessToken.IsNotNullOrWhiteSpace())
{
request.HttpRequest.Headers.Add("Authorization", "Bearer " + Settings.AccessToken);
}
yield return request;
}
}
}

@ -0,0 +1,27 @@
using FluentValidation;
using NzbDrone.Core.Annotations;
namespace NzbDrone.Core.ImportLists.Simkl.User
{
public class SimklUserSettingsValidator : SimklSettingsBaseValidator<SimklUserSettings>
{
public SimklUserSettingsValidator()
: base()
{
RuleFor(c => c.ListType).NotNull();
}
}
public class SimklUserSettings : SimklSettingsBase<SimklUserSettings>
{
protected override AbstractValidator<SimklUserSettings> Validator => new SimklUserSettingsValidator();
public SimklUserSettings()
{
ListType = (int)SimklUserListType.Watching;
}
[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; }
}
}
Loading…
Cancel
Save