Trying to follow simkl's way of importing
This commit is contained in:
parent
d2d5196c28
commit
a24f76bd6a
|
@ -9,14 +9,16 @@ using NzbDrone.Common.Serializer;
|
||||||
using NzbDrone.Core.Configuration;
|
using NzbDrone.Core.Configuration;
|
||||||
using NzbDrone.Core.Parser;
|
using NzbDrone.Core.Parser;
|
||||||
using NzbDrone.Core.Parser.Model;
|
using NzbDrone.Core.Parser.Model;
|
||||||
|
using NzbDrone.Core.Validation;
|
||||||
|
|
||||||
namespace NzbDrone.Core.ImportLists.MyAnimeList
|
namespace NzbDrone.Core.ImportLists.MyAnimeList
|
||||||
{
|
{
|
||||||
public class MalImport : HttpImportListBase<MalListSettings>
|
public class MalImport : HttpImportListBase<MalListSettings>
|
||||||
{
|
{
|
||||||
public const string OAuthUrl = "https://myanimelist.net/v1/oauth2/authorize";
|
public const string OAuthUrl = "https://myanimelist.net/v1/oauth2/authorize";
|
||||||
public const string OAuthTokenUrl = "https://myanimelist.net/v1/oauth2/token";
|
public const string RedirectUri = "https://auth.servarr.com/v1/mal_sonarr/auth";
|
||||||
public const string RedirectUri = "http://localhost:8989/oauth.html";
|
public const string RenewUri = "https://auth.servarr.com/v1/mal_sonarr/renew";
|
||||||
|
public string ClientId = "402de1a90f8eb545f625739fa69ddb98";
|
||||||
|
|
||||||
public static Dictionary<int, int> MalTvdbIds = new Dictionary<int, int>();
|
public static Dictionary<int, int> MalTvdbIds = new Dictionary<int, int>();
|
||||||
|
|
||||||
|
@ -68,7 +70,7 @@ namespace NzbDrone.Core.ImportLists.MyAnimeList
|
||||||
_Codechallenge = GenCodeChallenge();
|
_Codechallenge = GenCodeChallenge();
|
||||||
var request = new HttpRequestBuilder(OAuthUrl)
|
var request = new HttpRequestBuilder(OAuthUrl)
|
||||||
.AddQueryParam("response_type", "code")
|
.AddQueryParam("response_type", "code")
|
||||||
.AddQueryParam("client_id", Settings.ClientId)
|
.AddQueryParam("client_id", ClientId)
|
||||||
.AddQueryParam("code_challenge", _Codechallenge)
|
.AddQueryParam("code_challenge", _Codechallenge)
|
||||||
.AddQueryParam("state", query["callbackUrl"])
|
.AddQueryParam("state", query["callbackUrl"])
|
||||||
.AddQueryParam("redirect_uri", RedirectUri)
|
.AddQueryParam("redirect_uri", RedirectUri)
|
||||||
|
@ -81,13 +83,11 @@ namespace NzbDrone.Core.ImportLists.MyAnimeList
|
||||||
}
|
}
|
||||||
else if (action == "getOAuthToken")
|
else if (action == "getOAuthToken")
|
||||||
{
|
{
|
||||||
var jsonResult = Json.Deserialize<MalAuthToken>(GetAuthToken(query["code"]));
|
|
||||||
return new
|
return new
|
||||||
{
|
{
|
||||||
accessToken = jsonResult.AccessToken,
|
accessToken = query["access_token"],
|
||||||
refreshToken = jsonResult.RefreshToken,
|
expires = DateTime.UtcNow.AddSeconds(int.Parse(query["expires_in"])),
|
||||||
expires = DateTime.UtcNow.AddSeconds(int.Parse(jsonResult.ExpiresIn))
|
refreshToken = query["refresh_token"]
|
||||||
//expires = DateTime.UtcNow.AddSeconds(5)
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,7 +122,7 @@ namespace NzbDrone.Core.ImportLists.MyAnimeList
|
||||||
public string TokenType { get; set; }
|
public string TokenType { get; set; }
|
||||||
|
|
||||||
[JsonProperty("expires_in")]
|
[JsonProperty("expires_in")]
|
||||||
public string ExpiresIn { get; set; }
|
public int ExpiresIn { get; set; }
|
||||||
|
|
||||||
[JsonProperty("access_token")]
|
[JsonProperty("access_token")]
|
||||||
public string AccessToken { get; set; }
|
public string AccessToken { get; set; }
|
||||||
|
@ -173,49 +173,30 @@ namespace NzbDrone.Core.ImportLists.MyAnimeList
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetAuthToken(string authcode)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var req = new HttpRequestBuilder(OAuthTokenUrl).Post()
|
|
||||||
.AddFormParameter("client_id", Settings.ClientId)
|
|
||||||
.AddFormParameter("client_secret", Settings.ClientSecret)
|
|
||||||
.AddFormParameter("code", authcode)
|
|
||||||
.AddFormParameter("code_verifier", _Codechallenge)
|
|
||||||
.AddFormParameter("grant_type", "authorization_code")
|
|
||||||
.AddFormParameter("redirect_uri", "http://localhost:8989/oauth.html")
|
|
||||||
.Build();
|
|
||||||
var response = _httpClient.Post(req);
|
|
||||||
return response.Content;
|
|
||||||
}
|
|
||||||
catch (HttpRequestException ex)
|
|
||||||
{
|
|
||||||
_logger.Error(ex.Message);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void RefreshToken()
|
private void RefreshToken()
|
||||||
{
|
{
|
||||||
var httpReq = new HttpRequestBuilder(OAuthTokenUrl)
|
_logger.Trace("Refreshing Token");
|
||||||
.Post()
|
|
||||||
.AddFormParameter("client_id", Settings.ClientId)
|
Settings.Validate().Filter("RefreshToken").ThrowOnError();
|
||||||
.AddFormParameter("client_secret", Settings.ClientSecret)
|
|
||||||
.AddFormParameter("grant_type", "refresh_token")
|
var httpReq = new HttpRequestBuilder(RenewUri)
|
||||||
.AddFormParameter("refresh_token", Settings.RefreshToken)
|
.AddQueryParam("refresh_token", Settings.RefreshToken)
|
||||||
.Build();
|
.Build();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var httpResp = _httpClient.Post(httpReq);
|
var httpResp = _httpClient.Get<MalAuthToken>(httpReq);
|
||||||
var jsonResp = Json.Deserialize<MalAuthToken>(httpResp.Content);
|
|
||||||
Settings.AccessToken = jsonResp.AccessToken;
|
|
||||||
Settings.RefreshToken = jsonResp.RefreshToken;
|
|
||||||
Settings.Expires = DateTime.UtcNow.AddSeconds(int.Parse(jsonResp.ExpiresIn));
|
|
||||||
//Settings.Expires = DateTime.UtcNow.AddSeconds(5);
|
|
||||||
|
|
||||||
if (Definition.Id > 0)
|
if (httpResp?.Resource != null)
|
||||||
{
|
{
|
||||||
_importListRepository.UpdateSettings((ImportListDefinition)Definition);
|
var token = httpResp.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 (HttpRequestException)
|
catch (HttpRequestException)
|
||||||
|
|
|
@ -38,19 +38,13 @@ namespace NzbDrone.Core.ImportLists.MyAnimeList
|
||||||
[FieldDefinition(0, Label = "List Status", Type = FieldType.Select, SelectOptions = typeof(MalAnimeStatus), HelpText = "Type of list status you're seeking to import from")]
|
[FieldDefinition(0, Label = "List Status", Type = FieldType.Select, SelectOptions = typeof(MalAnimeStatus), HelpText = "Type of list status you're seeking to import from")]
|
||||||
public MalAnimeStatus ListStatus { get; set; }
|
public MalAnimeStatus ListStatus { get; set; }
|
||||||
|
|
||||||
[FieldDefinition(0, Label = "Client ID", Type = FieldType.Textbox)]
|
[FieldDefinition(0, Label = "Access Token", Type = FieldType.Textbox, Hidden = HiddenType.Hidden)]
|
||||||
public string ClientId { get; set; }
|
|
||||||
|
|
||||||
[FieldDefinition(0, Label = "Client Secret", Type = FieldType.Textbox)]
|
|
||||||
public string ClientSecret { get; set; }
|
|
||||||
|
|
||||||
[FieldDefinition(0, Label = "Access Token", Type = FieldType.Textbox, Hidden = HiddenType.Visible)]
|
|
||||||
public string AccessToken { get; set; }
|
public string AccessToken { get; set; }
|
||||||
|
|
||||||
[FieldDefinition(0, Label = "Refresh Token", Type = FieldType.Textbox, Hidden = HiddenType.Visible)]
|
[FieldDefinition(0, Label = "Refresh Token", Type = FieldType.Textbox, Hidden = HiddenType.Hidden)]
|
||||||
public string RefreshToken { get; set; }
|
public string RefreshToken { get; set; }
|
||||||
|
|
||||||
[FieldDefinition(0, Label = "Expires", Type = FieldType.Textbox, Hidden = HiddenType.Visible)]
|
[FieldDefinition(0, Label = "Expires", Type = FieldType.Textbox, Hidden = HiddenType.Hidden)]
|
||||||
public DateTime Expires { get; set; }
|
public DateTime Expires { get; set; }
|
||||||
|
|
||||||
[FieldDefinition(99, Label = "Authenticate With MyAnimeList", Type = FieldType.OAuth, HelpTextWarning = "Client ID and Client Secret must be filled out before authenticating with MAL")]
|
[FieldDefinition(99, Label = "Authenticate With MyAnimeList", Type = FieldType.OAuth, HelpTextWarning = "Client ID and Client Secret must be filled out before authenticating with MAL")]
|
||||||
|
|
Loading…
Reference in New Issue