Added ability to refresh token MAL

The func does not update the settings however
This commit is contained in:
iceypotato 2023-07-28 12:09:53 -07:00
parent 6d6c88a283
commit d2d5196c28
2 changed files with 55 additions and 4 deletions

View File

@ -22,24 +22,35 @@ namespace NzbDrone.Core.ImportLists.MyAnimeList
private static string _Codechallenge = ""; private static string _Codechallenge = "";
private IImportListRepository _importListRepository;
public override string Name => "MyAnimeList"; public override string Name => "MyAnimeList";
public override ImportListType ListType => ImportListType.Other; public override ImportListType ListType => ImportListType.Other;
public override TimeSpan MinRefreshInterval => TimeSpan.FromSeconds(10); // change this later public override TimeSpan MinRefreshInterval => TimeSpan.FromSeconds(10); // change this later
// This constructor the first thing that is called when sonarr creates a button // This constructor the first thing that is called when sonarr creates a button
public MalImport(IHttpClient httpClient, IImportListStatusService importListStatusService, IConfigService configService, IParsingService parsingService, Logger logger) public MalImport(IImportListRepository netImportRepository, IHttpClient httpClient, IImportListStatusService importListStatusService, IConfigService configService, IParsingService parsingService, Logger logger)
: base(httpClient, importListStatusService, configService, parsingService, logger) : base(httpClient, importListStatusService, configService, parsingService, logger)
{ {
if (MalTvdbIds.Count == 0) if (MalTvdbIds.Count == 0)
{ {
MalTvdbIds = GetMalToTvdbIds(); MalTvdbIds = GetMalToTvdbIds();
} }
_importListRepository = netImportRepository;
} }
// This method should refresh (dunno what that means) the token // This method should refresh (dunno what that means) the token
// This method also fetches the anime from mal? // This method also fetches the anime from mal?
public override IList<ImportListItemInfo> Fetch() public override IList<ImportListItemInfo> Fetch()
{ {
if (Settings.Expires < DateTime.UtcNow.AddMinutes(5))
{
_logger.Info($"current token: {Settings.AccessToken}");
RefreshToken();
_logger.Info($"new token: {Settings.AccessToken}");
}
//_importListStatusService; //_importListStatusService;
return FetchItems(g => g.GetListItems()); return FetchItems(g => g.GetListItems());
} }
@ -76,6 +87,7 @@ namespace NzbDrone.Core.ImportLists.MyAnimeList
accessToken = jsonResult.AccessToken, accessToken = jsonResult.AccessToken,
refreshToken = jsonResult.RefreshToken, refreshToken = jsonResult.RefreshToken,
expires = DateTime.UtcNow.AddSeconds(int.Parse(jsonResult.ExpiresIn)) expires = DateTime.UtcNow.AddSeconds(int.Parse(jsonResult.ExpiresIn))
//expires = DateTime.UtcNow.AddSeconds(5)
}; };
} }
@ -182,5 +194,34 @@ namespace NzbDrone.Core.ImportLists.MyAnimeList
return null; return null;
} }
} }
private void RefreshToken()
{
var httpReq = new HttpRequestBuilder(OAuthTokenUrl)
.Post()
.AddFormParameter("client_id", Settings.ClientId)
.AddFormParameter("client_secret", Settings.ClientSecret)
.AddFormParameter("grant_type", "refresh_token")
.AddFormParameter("refresh_token", Settings.RefreshToken)
.Build();
try
{
var httpResp = _httpClient.Post(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)
{
_importListRepository.UpdateSettings((ImportListDefinition)Definition);
}
}
catch (HttpRequestException)
{
_logger.Error("Error trying to refresh MAL access token.");
}
}
} }
} }

View File

@ -9,6 +9,16 @@ namespace NzbDrone.Core.ImportLists.MyAnimeList
{ {
public MalSettingsValidator() public MalSettingsValidator()
{ {
RuleFor(c => c.BaseUrl).ValidRootUrl();
RuleFor(c => c.ClientId).NotEmpty()
.OverridePropertyName("ClientId")
.WithMessage("Must input Client ID from MAL, created in the API section of your account");
RuleFor(c => c.ClientSecret).NotEmpty()
.OverridePropertyName("ClientSecret")
.WithMessage("Must input Client Secret from MAL, created in the API section of your account");
RuleFor(c => c.AccessToken).NotEmpty()
.OverridePropertyName("SignIn")
.WithMessage("Must authenticate with MyAnimeList");
} }
} }
@ -34,13 +44,13 @@ namespace NzbDrone.Core.ImportLists.MyAnimeList
[FieldDefinition(0, Label = "Client Secret", Type = FieldType.Textbox)] [FieldDefinition(0, Label = "Client Secret", Type = FieldType.Textbox)]
public string ClientSecret { get; set; } public string ClientSecret { get; set; }
[FieldDefinition(0, Label = "Access Token", Type = FieldType.Textbox, Hidden = HiddenType.Hidden)] [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.Hidden)] [FieldDefinition(0, Label = "Refresh Token", Type = FieldType.Textbox, Hidden = HiddenType.Visible)]
public string RefreshToken { get; set; } public string RefreshToken { get; set; }
[FieldDefinition(0, Label = "Expires", Type = FieldType.Textbox, Hidden = HiddenType.Hidden)] [FieldDefinition(0, Label = "Expires", Type = FieldType.Textbox, Hidden = HiddenType.Visible)]
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")]