diff --git a/src/NzbDrone.Core/ImportLists/ImportListType.cs b/src/NzbDrone.Core/ImportLists/ImportListType.cs index 68f865308..e24ac28d7 100644 --- a/src/NzbDrone.Core/ImportLists/ImportListType.cs +++ b/src/NzbDrone.Core/ImportLists/ImportListType.cs @@ -6,6 +6,7 @@ namespace NzbDrone.Core.ImportLists TMDB, Trakt, Plex, + Simkl, Other, Advanced } diff --git a/src/NzbDrone.Core/ImportLists/Simkl/SimklAPI.cs b/src/NzbDrone.Core/ImportLists/Simkl/SimklAPI.cs new file mode 100644 index 000000000..baa4d189b --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Simkl/SimklAPI.cs @@ -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 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; } + } +} diff --git a/src/NzbDrone.Core/ImportLists/Simkl/SimklImportBase.cs b/src/NzbDrone.Core/ImportLists/Simkl/SimklImportBase.cs new file mode 100644 index 000000000..f780e2556 --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Simkl/SimklImportBase.cs @@ -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 : HttpImportListBase + where TSettings : SimklSettingsBase, 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 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(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(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(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"); + } + } + } +} diff --git a/src/NzbDrone.Core/ImportLists/Simkl/SimklParser.cs b/src/NzbDrone.Core/ImportLists/Simkl/SimklParser.cs new file mode 100644 index 000000000..494b2693e --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Simkl/SimklParser.cs @@ -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 ParseResponse(ImportListResponse importResponse) + { + _importResponse = importResponse; + + var movie = new List(); + + if (!PreProcess(_importResponse)) + { + return movie; + } + + var jsonResponse = STJson.Deserialize(_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; + } + } +} diff --git a/src/NzbDrone.Core/ImportLists/Simkl/SimklSettingsBase.cs b/src/NzbDrone.Core/ImportLists/Simkl/SimklSettingsBase.cs new file mode 100644 index 000000000..5fc21a6d5 --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Simkl/SimklSettingsBase.cs @@ -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 : AbstractValidator + where TSettings : SimklSettingsBase + { + 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 : IProviderConfig + where TSettings : SimklSettingsBase + { + protected virtual AbstractValidator Validator => new SimklSettingsBaseValidator(); + + 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)); + } + } +} diff --git a/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserImport.cs b/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserImport.cs new file mode 100644 index 000000000..fc06e875a --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserImport.cs @@ -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 + { + 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 + }; + } + } +} diff --git a/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserListType.cs b/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserListType.cs new file mode 100644 index 000000000..e00bc60ac --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserListType.cs @@ -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 + } +} diff --git a/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserRequestGenerator.cs b/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserRequestGenerator.cs new file mode 100644 index 000000000..9b88c49a0 --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserRequestGenerator.cs @@ -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 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; + } + } +} diff --git a/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserSettings.cs b/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserSettings.cs new file mode 100644 index 000000000..61cc48129 --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Simkl/User/SimklUserSettings.cs @@ -0,0 +1,27 @@ +using FluentValidation; +using NzbDrone.Core.Annotations; + +namespace NzbDrone.Core.ImportLists.Simkl.User +{ + public class SimklUserSettingsValidator : SimklSettingsBaseValidator + { + public SimklUserSettingsValidator() + : base() + { + RuleFor(c => c.ListType).NotNull(); + } + } + + public class SimklUserSettings : SimklSettingsBase + { + protected override AbstractValidator 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; } + } +}