parent
431a3f6f8f
commit
00f631c623
@ -0,0 +1,97 @@
|
||||
using System.Linq;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.MediaFiles;
|
||||
using NzbDrone.Core.MediaFiles.MediaInfo;
|
||||
using NzbDrone.Core.Movies;
|
||||
using NzbDrone.Core.Notifications;
|
||||
using NzbDrone.Core.Notifications.Trakt;
|
||||
using NzbDrone.Core.Notifications.Trakt.Resource;
|
||||
using NzbDrone.Core.Qualities;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
|
||||
namespace NzbDrone.Core.Test.NotificationTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class TraktServiceFixture : CoreTest<TraktService>
|
||||
{
|
||||
private DownloadMessage _downloadMessage;
|
||||
private TraktSettings _traktSettings;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_downloadMessage = new DownloadMessage
|
||||
{
|
||||
Movie = new Movie(),
|
||||
MovieFile = new MovieFile
|
||||
{
|
||||
MediaInfo = null,
|
||||
Quality = new QualityModel
|
||||
{
|
||||
Quality = Quality.Unknown
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
_traktSettings = new TraktSettings
|
||||
{
|
||||
AccessToken = "",
|
||||
RefreshToken = ""
|
||||
};
|
||||
}
|
||||
|
||||
private void GiventValidMediaInfo(Quality quality, string audioChannels, string audioFormat, string scanType)
|
||||
{
|
||||
_downloadMessage.MovieFile.MediaInfo = new MediaInfoModel
|
||||
{
|
||||
AudioChannelPositions = audioChannels,
|
||||
AudioFormat = audioFormat,
|
||||
ScanType = scanType
|
||||
};
|
||||
|
||||
_downloadMessage.MovieFile.Quality.Quality = quality;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_add_collection_movie_if_null_mediainfo()
|
||||
{
|
||||
Subject.AddMovieToCollection(_traktSettings, _downloadMessage.Movie, _downloadMessage.MovieFile);
|
||||
|
||||
Mocker.GetMock<ITraktProxy>()
|
||||
.Verify(v => v.AddToCollection(It.IsAny<TraktCollectMoviesResource>(), It.IsAny<string>()), Times.Once());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_add_collection_movie_if_valid_mediainfo()
|
||||
{
|
||||
GiventValidMediaInfo(Quality.Bluray1080p, "3/2/0.1", "DTS", "Interlaced");
|
||||
|
||||
Subject.AddMovieToCollection(_traktSettings, _downloadMessage.Movie, _downloadMessage.MovieFile);
|
||||
|
||||
Mocker.GetMock<ITraktProxy>()
|
||||
.Verify(v => v.AddToCollection(It.Is<TraktCollectMoviesResource>(t =>
|
||||
t.Movies.First().Audio == "dts" &&
|
||||
t.Movies.First().AudioChannels == "5.1" &&
|
||||
t.Movies.First().Resolution == "hd_1080i" &&
|
||||
t.Movies.First().MediaType == "bluray"),
|
||||
It.IsAny<string>()), Times.Once());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_format_audio_channels_to_one_decimal_when_adding_collection_movie()
|
||||
{
|
||||
GiventValidMediaInfo(Quality.Bluray1080p, "2/0/0", "DTS", "Interlaced");
|
||||
|
||||
Subject.AddMovieToCollection(_traktSettings, _downloadMessage.Movie, _downloadMessage.MovieFile);
|
||||
|
||||
Mocker.GetMock<ITraktProxy>()
|
||||
.Verify(v => v.AddToCollection(It.Is<TraktCollectMoviesResource>(t =>
|
||||
t.Movies.First().Audio == "dts" &&
|
||||
t.Movies.First().AudioChannels == "2.0" &&
|
||||
t.Movies.First().Resolution == "hd_1080i" &&
|
||||
t.Movies.First().MediaType == "bluray"),
|
||||
It.IsAny<string>()), Times.Once());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using FizzWare.NBuilder;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.Notifications.Trakt;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
using NzbDrone.Test.Common;
|
||||
|
||||
namespace NzbDrone.Core.Test.NotificationTests.TraktTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class TraktSettingsValidatorFixture : CoreTest<TraktSettingsValidator>
|
||||
{
|
||||
private TraktSettings _traktSettings;
|
||||
private TestValidator<TraktSettings> _validator;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_validator = new TestValidator<TraktSettings>
|
||||
{
|
||||
v => v.RuleFor(s => s).SetValidator(Subject)
|
||||
};
|
||||
|
||||
_traktSettings = Builder<TraktSettings>.CreateNew()
|
||||
.With(s => s.AccessToken = "sometoken")
|
||||
.With(s => s.RefreshToken = "sometoken")
|
||||
.With(s => s.Expires = DateTime.Now.AddDays(2))
|
||||
.Build();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_be_valid_if_all_settings_valid()
|
||||
{
|
||||
_validator.Validate(_traktSettings).IsValid.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_be_valid_if_port_is_out_of_range()
|
||||
{
|
||||
_traktSettings.AccessToken = "";
|
||||
|
||||
_validator.Validate(_traktSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_be_valid_if_server_is_empty()
|
||||
{
|
||||
_traktSettings.RefreshToken = "";
|
||||
|
||||
_validator.Validate(_traktSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_be_valid_if_from_is_empty()
|
||||
{
|
||||
_traktSettings.Expires = default(DateTime);
|
||||
|
||||
_validator.Validate(_traktSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
namespace NzbDrone.Core.NetImport.Trakt
|
||||
{
|
||||
public class TraktMovieIdsResource
|
||||
{
|
||||
public int Trakt { get; set; }
|
||||
public string Slug { get; set; }
|
||||
public string Imdb { get; set; }
|
||||
public int Tmdb { get; set; }
|
||||
}
|
||||
|
||||
public class TraktMovieResource
|
||||
{
|
||||
public string Title { get; set; }
|
||||
public int? Year { get; set; }
|
||||
public TraktMovieIdsResource Ids { get; set; }
|
||||
}
|
||||
|
||||
public class TraktResponse
|
||||
{
|
||||
public int? Rank { get; set; }
|
||||
public string Listed_at { get; set; }
|
||||
public string Type { get; set; }
|
||||
|
||||
public int? Watchers { get; set; }
|
||||
|
||||
public long? Revenue { get; set; }
|
||||
|
||||
public long? Watcher_count { get; set; }
|
||||
public long? Play_count { get; set; }
|
||||
public long? Collected_count { get; set; }
|
||||
|
||||
public TraktMovieResource Movie { get; set; }
|
||||
}
|
||||
|
||||
public class RefreshRequestResponse
|
||||
{
|
||||
public string Access_token { get; set; }
|
||||
public string Token_type { get; set; }
|
||||
public int Expires_in { get; set; }
|
||||
public string Refresh_token { get; set; }
|
||||
public string Scope { get; set; }
|
||||
}
|
||||
|
||||
public class UserSettingsResponse
|
||||
{
|
||||
public TraktUserResource User { get; set; }
|
||||
}
|
||||
|
||||
public class TraktUserResource
|
||||
{
|
||||
public string Username { get; set; }
|
||||
public TraktUserIdsResource Ids { get; set; }
|
||||
}
|
||||
|
||||
public class TraktUserIdsResource
|
||||
{
|
||||
public string Slug { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Trakt.Resource
|
||||
{
|
||||
public class TraktAuthRefreshResource
|
||||
{
|
||||
[JsonProperty(PropertyName = "access_token")]
|
||||
public string AccessToken { get; set; }
|
||||
[JsonProperty(PropertyName = "token_type")]
|
||||
public string TokenType { get; set; }
|
||||
[JsonProperty(PropertyName = "expires_in")]
|
||||
public int ExpiresIn { get; set; }
|
||||
[JsonProperty(PropertyName = "refresh_token")]
|
||||
public string RefreshToken { get; set; }
|
||||
public string Scope { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Trakt.Resource
|
||||
{
|
||||
public class TraktCollectMovie : TraktMovieResource
|
||||
{
|
||||
[JsonProperty(PropertyName = "collected_at")]
|
||||
public DateTime CollectedAt { get; set; }
|
||||
public string Resolution { get; set; }
|
||||
[JsonProperty(PropertyName = "audio_channels")]
|
||||
public string AudioChannels { get; set; }
|
||||
public string Audio { get; set; }
|
||||
[JsonProperty(PropertyName = "media_type")]
|
||||
public string MediaType { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Trakt.Resource
|
||||
{
|
||||
public class TraktCollectMoviesResource
|
||||
{
|
||||
public List<TraktCollectMovie> Movies { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Trakt.Resource
|
||||
{
|
||||
public class TraktListResource
|
||||
{
|
||||
public int? Rank { get; set; }
|
||||
[JsonProperty(PropertyName = "listed_at")]
|
||||
public string ListedAt { get; set; }
|
||||
public string Type { get; set; }
|
||||
|
||||
public int? Watchers { get; set; }
|
||||
|
||||
public long? Revenue { get; set; }
|
||||
[JsonProperty(PropertyName = "watcher_count")]
|
||||
public long? WatcherCount { get; set; }
|
||||
[JsonProperty(PropertyName = "play_count")]
|
||||
public long? PlayCount { get; set; }
|
||||
[JsonProperty(PropertyName = "collected_count")]
|
||||
public long? CollectedCount { get; set; }
|
||||
|
||||
public TraktMovieResource Movie { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
namespace NzbDrone.Core.Notifications.Trakt.Resource
|
||||
{
|
||||
public class TraktMovieIdsResource
|
||||
{
|
||||
public int Trakt { get; set; }
|
||||
public string Slug { get; set; }
|
||||
public string Imdb { get; set; }
|
||||
public int Tmdb { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
namespace NzbDrone.Core.Notifications.Trakt.Resource
|
||||
{
|
||||
public class TraktMovieResource
|
||||
{
|
||||
public string Title { get; set; }
|
||||
public int? Year { get; set; }
|
||||
public TraktMovieIdsResource Ids { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
namespace NzbDrone.Core.Notifications.Trakt.Resource
|
||||
{
|
||||
public class TraktUserIdsResource
|
||||
{
|
||||
public string Slug { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
using NzbDrone.Core.Notifications.Trakt.Resource;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Trakt
|
||||
{
|
||||
public class TraktUserResource
|
||||
{
|
||||
public string Username { get; set; }
|
||||
public TraktUserIdsResource Ids { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
namespace NzbDrone.Core.Notifications.Trakt.Resource
|
||||
{
|
||||
public class TraktUserSettingsResource
|
||||
{
|
||||
public TraktUserResource User { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using FluentValidation.Results;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Core.Validation;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Trakt
|
||||
{
|
||||
public class Trakt : NotificationBase<TraktSettings>
|
||||
{
|
||||
private readonly ITraktService _traktService;
|
||||
private readonly INotificationRepository _notificationRepository;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public Trakt(ITraktService traktService, INotificationRepository notificationRepository, Logger logger)
|
||||
{
|
||||
_traktService = traktService;
|
||||
_notificationRepository = notificationRepository;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public override string Link => "https://trakt.tv/";
|
||||
public override string Name => "Trakt";
|
||||
|
||||
public override void OnDownload(DownloadMessage message)
|
||||
{
|
||||
_traktService.AddMovieToCollection(Settings, message.Movie, message.MovieFile);
|
||||
}
|
||||
|
||||
public override ValidationResult Test()
|
||||
{
|
||||
var failures = new List<ValidationFailure>();
|
||||
|
||||
failures.AddIfNotNull(_traktService.Test(Settings));
|
||||
|
||||
return new ValidationResult(failures);
|
||||
}
|
||||
|
||||
public override object RequestAction(string action, IDictionary<string, string> query)
|
||||
{
|
||||
if (action == "startOAuth")
|
||||
{
|
||||
var request = _traktService.GetOAuthRequest(query["callbackUrl"]);
|
||||
|
||||
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 = _traktService.GetUserName(query["access_token"])
|
||||
};
|
||||
}
|
||||
|
||||
return new { };
|
||||
}
|
||||
|
||||
public void RefreshToken()
|
||||
{
|
||||
_logger.Trace("Refreshing Token");
|
||||
|
||||
Settings.Validate().Filter("RefreshToken").ThrowOnError();
|
||||
|
||||
try
|
||||
{
|
||||
var response = _traktService.RefreshAuthToken(Settings.RefreshToken);
|
||||
|
||||
if (response != null)
|
||||
{
|
||||
var token = response;
|
||||
Settings.AccessToken = token.AccessToken;
|
||||
Settings.Expires = DateTime.UtcNow.AddSeconds(token.ExpiresIn);
|
||||
Settings.RefreshToken = token.RefreshToken ?? Settings.RefreshToken;
|
||||
|
||||
if (Definition.Id > 0)
|
||||
{
|
||||
_notificationRepository.UpdateSettings((NotificationDefinition)Definition);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (HttpException)
|
||||
{
|
||||
_logger.Warn($"Error refreshing trakt access token");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using NzbDrone.Common.Exceptions;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Trakt
|
||||
{
|
||||
public class TraktException : NzbDroneException
|
||||
{
|
||||
public TraktException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public TraktException(string message, Exception innerException, params object[] args)
|
||||
: base(message, innerException, args)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using FluentValidation.Results;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Notifications.Trakt.Resource;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Trakt
|
||||
{
|
||||
public interface ITraktProxy
|
||||
{
|
||||
string GetUserName(string accessToken);
|
||||
HttpRequest GetOAuthRequest(string callbackUrl);
|
||||
TraktAuthRefreshResource RefreshAuthToken(string refreshToken);
|
||||
void AddToCollection(TraktCollectMoviesResource payload, string accessToken);
|
||||
HttpRequest BuildTraktRequest(string resource, HttpMethod method, string accessToken);
|
||||
}
|
||||
|
||||
public class TraktProxy : ITraktProxy
|
||||
{
|
||||
private const string URL = "https://api.trakt.tv";
|
||||
private const string OAuthUrl = "https://api.trakt.tv/oauth/authorize";
|
||||
private const string RedirectUri = "https://auth.servarr.com/v1/trakt/auth";
|
||||
private const string RenewUri = "https://auth.servarr.com/v1/trakt/renew";
|
||||
private const string ClientId = "64508a8bf370cee550dde4806469922fd7cd70afb2d5690e3ee7f75ae784b70e";
|
||||
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public TraktProxy(IHttpClient httpClient, Logger logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void AddToCollection(TraktCollectMoviesResource payload, string accessToken)
|
||||
{
|
||||
var request = BuildTraktRequest("sync/collection", HttpMethod.POST, accessToken);
|
||||
|
||||
request.Headers.ContentType = "application/json";
|
||||
request.SetContent(payload.ToJson());
|
||||
|
||||
try
|
||||
{
|
||||
_httpClient.Execute(request);
|
||||
}
|
||||
catch (HttpException ex)
|
||||
{
|
||||
_logger.Error(ex, "Unable to post payload {0}", payload);
|
||||
throw new TraktException("Unable to post payload", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public string GetUserName(string accessToken)
|
||||
{
|
||||
var request = BuildTraktRequest("users/settings", HttpMethod.GET, accessToken);
|
||||
|
||||
try
|
||||
{
|
||||
var response = _httpClient.Get<TraktUserSettingsResource>(request);
|
||||
|
||||
if (response != null && response.Resource != null)
|
||||
{
|
||||
return response.Resource.User.Ids.Slug;
|
||||
}
|
||||
}
|
||||
catch (HttpException)
|
||||
{
|
||||
_logger.Warn($"Error refreshing trakt access token");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public HttpRequest GetOAuthRequest(string callbackUrl)
|
||||
{
|
||||
return new HttpRequestBuilder(OAuthUrl)
|
||||
.AddQueryParam("client_id", ClientId)
|
||||
.AddQueryParam("response_type", "code")
|
||||
.AddQueryParam("redirect_uri", RedirectUri)
|
||||
.AddQueryParam("state", callbackUrl)
|
||||
.Build();
|
||||
}
|
||||
|
||||
public TraktAuthRefreshResource RefreshAuthToken(string refreshToken)
|
||||
{
|
||||
var request = new HttpRequestBuilder(RenewUri)
|
||||
.AddQueryParam("refresh_token", refreshToken)
|
||||
.Build();
|
||||
|
||||
return _httpClient.Get<TraktAuthRefreshResource>(request)?.Resource ?? null;
|
||||
}
|
||||
|
||||
public HttpRequest BuildTraktRequest(string resource, HttpMethod method, string accessToken)
|
||||
{
|
||||
var request = new HttpRequestBuilder(URL).Resource(resource).Build();
|
||||
|
||||
request.Headers.Accept = HttpAccept.Json.Value;
|
||||
request.Method = method;
|
||||
|
||||
request.Headers.Add("trakt-api-version", "2");
|
||||
request.Headers.Add("trakt-api-key", ClientId);
|
||||
|
||||
if (accessToken.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
request.Headers.Add("Authorization", "Bearer " + accessToken);
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,250 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using FluentValidation.Results;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Core.MediaFiles;
|
||||
using NzbDrone.Core.MediaFiles.MediaInfo;
|
||||
using NzbDrone.Core.Movies;
|
||||
using NzbDrone.Core.Notifications.Trakt.Resource;
|
||||
using NzbDrone.Core.Qualities;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Trakt
|
||||
{
|
||||
public interface ITraktService
|
||||
{
|
||||
HttpRequest GetOAuthRequest(string callbackUrl);
|
||||
TraktAuthRefreshResource RefreshAuthToken(string refreshToken);
|
||||
void AddMovieToCollection(TraktSettings settings, Movie movie, MovieFile movieFile);
|
||||
string GetUserName(string accessToken);
|
||||
ValidationFailure Test(TraktSettings settings);
|
||||
}
|
||||
|
||||
public class TraktService : ITraktService
|
||||
{
|
||||
private readonly ITraktProxy _proxy;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public TraktService(ITraktProxy proxy,
|
||||
Logger logger)
|
||||
{
|
||||
_proxy = proxy;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string GetUserName(string accessToken)
|
||||
{
|
||||
return _proxy.GetUserName(accessToken);
|
||||
}
|
||||
|
||||
public HttpRequest GetOAuthRequest(string callbackUrl)
|
||||
{
|
||||
return _proxy.GetOAuthRequest(callbackUrl);
|
||||
}
|
||||
|
||||
public TraktAuthRefreshResource RefreshAuthToken(string refreshToken)
|
||||
{
|
||||
return _proxy.RefreshAuthToken(refreshToken);
|
||||
}
|
||||
|
||||
public ValidationFailure Test(TraktSettings settings)
|
||||
{
|
||||
try
|
||||
{
|
||||
GetUserName(settings.AccessToken);
|
||||
return null;
|
||||
}
|
||||
catch (HttpException ex)
|
||||
{
|
||||
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
_logger.Error(ex, "Access Token is invalid: " + ex.Message);
|
||||
return new ValidationFailure("Token", "Access Token is invalid");
|
||||
}
|
||||
|
||||
_logger.Error(ex, "Unable to send test message: " + ex.Message);
|
||||
return new ValidationFailure("Token", "Unable to send test message");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error(ex, "Unable to send test message: " + ex.Message);
|
||||
return new ValidationFailure("", "Unable to send test message");
|
||||
}
|
||||
}
|
||||
|
||||
public void AddMovieToCollection(TraktSettings settings, Movie movie, MovieFile movieFile)
|
||||
{
|
||||
var payload = new TraktCollectMoviesResource
|
||||
{
|
||||
Movies = new List<TraktCollectMovie>()
|
||||
};
|
||||
|
||||
var traktResolution = MapResolution(movieFile.Quality.Quality.Resolution, movieFile.MediaInfo?.ScanType);
|
||||
var mediaType = MapMediaType(movieFile.Quality.Quality.Source);
|
||||
var audio = MapAudio(movieFile);
|
||||
var audioChannels = MapAudioChannels(movieFile, audio);
|
||||
|
||||
payload.Movies.Add(new TraktCollectMovie
|
||||
{
|
||||
Title = movie.Title,
|
||||
Year = movie.Year,
|
||||
CollectedAt = DateTime.Now,
|
||||
Resolution = traktResolution,
|
||||
MediaType = mediaType,
|
||||
AudioChannels = audioChannels,
|
||||
Audio = audio,
|
||||
Ids = new TraktMovieIdsResource
|
||||
{
|
||||
Tmdb = movie.TmdbId,
|
||||
Imdb = movie.ImdbId ?? "",
|
||||
}
|
||||
});
|
||||
|
||||
_proxy.AddToCollection(payload, settings.AccessToken);
|
||||
}
|
||||
|
||||
private string MapMediaType(Source source)
|
||||
{
|
||||
var traktSource = string.Empty;
|
||||
|
||||
switch (source)
|
||||
{
|
||||
case Source.BLURAY:
|
||||
traktSource = "bluray";
|
||||
break;
|
||||
case Source.WEBDL:
|
||||
traktSource = "digital";
|
||||
break;
|
||||
case Source.WEBRIP:
|
||||
traktSource = "digital";
|
||||
break;
|
||||
case Source.DVD:
|
||||
traktSource = "dvd";
|
||||
break;
|
||||
case Source.TV:
|
||||
traktSource = "dvd";
|
||||
break;
|
||||
}
|
||||
|
||||
return traktSource;
|
||||
}
|
||||
|
||||
private string MapResolution(int resolution, string scanType)
|
||||
{
|
||||
var traktResolution = string.Empty;
|
||||
var interlacedTypes = new string[] { "Interlaced", "MBAFF", "PAFF" };
|
||||
|
||||
var scanIdentifier = scanType.IsNotNullOrWhiteSpace() && interlacedTypes.Contains(scanType) ? "i" : "p";
|
||||
|
||||
switch (resolution)
|
||||
{
|
||||
case 2160:
|
||||
traktResolution = "uhd_4k";
|
||||
break;
|
||||
case 1080:
|
||||
traktResolution = string.Format("hd_1080{0}", scanIdentifier);
|
||||
break;
|
||||
case 720:
|
||||
traktResolution = "hd_720p";
|
||||
break;
|
||||
case 576:
|
||||
traktResolution = string.Format("sd_576{0}", scanIdentifier);
|
||||
break;
|
||||
case 480:
|
||||
traktResolution = string.Format("sd_480{0}", scanIdentifier);
|
||||
break;
|
||||
}
|
||||
|
||||
return traktResolution;
|
||||
}
|
||||
|
||||
private string MapAudio(MovieFile movieFile)
|
||||
{
|
||||
var traktAudioFormat = string.Empty;
|
||||
|
||||
var audioCodec = movieFile.MediaInfo != null ? MediaInfoFormatter.FormatAudioCodec(movieFile.MediaInfo, movieFile.SceneName) : string.Empty;
|
||||
|
||||
switch (audioCodec)
|
||||
{
|
||||
case "AC3":
|
||||
traktAudioFormat = "dolby_digital";
|
||||
break;
|
||||
case "EAC3":
|
||||
traktAudioFormat = "dolby_digital_plus";
|
||||
break;
|
||||
case "TrueHD":
|
||||
traktAudioFormat = "dolby_truehd";
|
||||
break;
|
||||
case "EAC3 Atmos":
|
||||
case "TrueHD Atmos":
|
||||
traktAudioFormat = "dolby_atmos";
|
||||
break;
|
||||
case "DTS":
|
||||
case "DTS-ES":
|
||||
traktAudioFormat = "dts";
|
||||
break;
|
||||
case "DTS-HD MA":
|
||||
traktAudioFormat = "dts_ma";
|
||||
break;
|
||||
case "DTS-HD HRA":
|
||||
traktAudioFormat = "dts_hr";
|
||||
break;
|
||||
case "DTS-X":
|
||||
traktAudioFormat = "dts_x";
|
||||
break;
|
||||
case "MP3":
|
||||
case "MP2":
|
||||
traktAudioFormat = "mp3";
|
||||
break;
|
||||
case "Vorbis":
|
||||
traktAudioFormat = "ogg";
|
||||
break;
|
||||
case "WMA":
|
||||
traktAudioFormat = "wma";
|
||||
break;
|
||||
case "AAC":
|
||||
traktAudioFormat = "aac";
|
||||
break;
|
||||
case "PCM":
|
||||
traktAudioFormat = "lpcm";
|
||||
break;
|
||||
case "FLAC":
|
||||
traktAudioFormat = "flac";
|
||||
break;
|
||||
case "Opus":
|
||||
traktAudioFormat = "ogg";
|
||||
break;
|
||||
}
|
||||
|
||||
return traktAudioFormat;
|
||||
}
|
||||
|
||||
private string MapAudioChannels(MovieFile movieFile, string audioFormat)
|
||||
{
|
||||
var audioChannels = movieFile.MediaInfo != null ? MediaInfoFormatter.FormatAudioChannels(movieFile.MediaInfo).ToString("0.0") : string.Empty;
|
||||
|
||||
// Map cases where Radarr doesn't handle MI correctly, can purge once mediainfo handling is improved
|
||||
if (audioChannels == "8.0")
|
||||
{
|
||||
audioChannels = "7.1";
|
||||
}
|
||||
else if (audioChannels == "6.0" && audioFormat == "dts_ma")
|
||||
{
|
||||
audioChannels = "7.1";
|
||||
}
|
||||
else if (audioChannels == "6.0" && audioFormat != "dts_ma")
|
||||
{
|
||||
audioChannels = "5.1";
|
||||
}
|
||||
else if (audioChannels == "0.0")
|
||||
{
|
||||
audioChannels = string.Empty;
|
||||
}
|
||||
|
||||
return audioChannels;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using FluentValidation;
|
||||
using NzbDrone.Core.Annotations;
|
||||
using NzbDrone.Core.ThingiProvider;
|
||||
using NzbDrone.Core.Validation;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Trakt
|
||||
{
|
||||
public class TraktSettingsValidator : AbstractValidator<TraktSettings>
|
||||
{
|
||||
public TraktSettingsValidator()
|
||||
{
|
||||
RuleFor(c => c.AccessToken).NotEmpty();
|
||||
RuleFor(c => c.RefreshToken).NotEmpty();
|
||||
RuleFor(c => c.Expires).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class TraktSettings : IProviderConfig
|
||||
{
|
||||
private static readonly TraktSettingsValidator Validator = new TraktSettingsValidator();
|
||||
|
||||
public TraktSettings()
|
||||
{
|
||||
SignIn = "startOAuth";
|
||||
}
|
||||
|
||||
[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 Trakt", Type = FieldType.OAuth)]
|
||||
public string SignIn { get; set; }
|
||||
|
||||
public NzbDroneValidationResult Validate()
|
||||
{
|
||||
return new NzbDroneValidationResult(Validator.Validate(this));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue