Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work on CheckForTraktTokenUpdate #1098

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions Shoko.Server/Import/Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1438,49 +1438,41 @@ public static void CheckForTraktTokenUpdate(bool forceRefresh)
try
{
var settings = Utils.SettingsProvider.GetSettings();
if (!settings.TraktTv.Enabled)
if (!settings.TraktTv.Enabled || string.IsNullOrEmpty(settings.TraktTv.TokenExpirationDate))
{
return;
}

var traktHelper = Utils.ServiceContainer.GetRequiredService<TraktTVHelper>();
// by updating the Trakt token regularly, the user won't need to authorize again
var freqHours = 24; // we need to update this daily

var sched =
RepoFactory.ScheduledUpdate.GetByUpdateType((int)ScheduledUpdateType.TraktToken);
if (sched != null)
// Convert the Unix timestamp to DateTime directly
var expirationDate = DateTimeOffset.FromUnixTimeSeconds(long.Parse(settings.TraktTv.TokenExpirationDate)).DateTime;

// Check if force refresh is requested or the token is expired
if (forceRefresh || DateTime.Now.Add(TimeSpan.FromDays(45)) >= expirationDate)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 45 days?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://discord.com/channels/96234011612958720/1148092356592160768/1195127627397214331

Initially I had it set to 30 days, but half would be 45 days so I set this. We can set it to whatever is fine

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Half of what

Copy link
Contributor Author

@krbrs krbrs Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Token lifetime - it's 90 days

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Looks good then

{
// if we have run this in the last xxx hours and are not forcing it, then exit
var tsLastRun = DateTime.Now - sched.LastUpdate;
Logger.Trace("Last Trakt Token Update: {0} minutes ago", tsLastRun.TotalMinutes);
if (tsLastRun.TotalHours < freqHours)
{
if (!forceRefresh)
{
return;
}
}
}
traktHelper.RefreshAuthToken();

traktHelper.RefreshAuthToken();
if (sched == null)
// Update the last token refresh timestamp
var sched = RepoFactory.ScheduledUpdate.GetByUpdateType((int)ScheduledUpdateType.TraktToken)
?? new ScheduledUpdate { UpdateType = (int)ScheduledUpdateType.TraktToken, UpdateDetails = string.Empty };

sched.LastUpdate = DateTime.Now;
RepoFactory.ScheduledUpdate.Save(sched);

Logger.Info("Trakt token refreshed successfully. Expiry date: {0}", expirationDate);
}
else
{
sched = new ScheduledUpdate
{
UpdateType = (int)ScheduledUpdateType.TraktToken, UpdateDetails = string.Empty
};
Logger.Info("Trakt token is still valid. Expiry date: {0}", expirationDate);
}

sched.LastUpdate = DateTime.Now;
RepoFactory.ScheduledUpdate.Save(sched);
}
catch (Exception ex)
{
Logger.Error(ex, "Error in CheckForTraktTokenUpdate: " + ex);
}
}

public static void CheckForAniDBFileUpdate(bool forceRefresh)
{
var settings = Utils.SettingsProvider.GetSettings();
Expand Down