Skip to content

Commit

Permalink
Increased polling interval from default of 1s to 2s to prevent API ra…
Browse files Browse the repository at this point in the history
…te limiting. Track updates will happen between 0 and 2 seconds now. Additionally changed it so that if Snip does get rate limited that it won't create duplicate track entries in the history; it just skips that interval update altogether.
  • Loading branch information
David Rudie committed Oct 18, 2022
1 parent 7033e20 commit 9d00b40
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions Snip/Players/Spotify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ internal sealed class Spotify : MediaPlayer, IDisposable
private Timer updateSpotifyTrackInformation;

private double updateAuthorizationTokenDefaultInterval = 1000;
private double updateSpotifyTrackInformationDefaultInterval = 1000;
private double updateSpotifyTrackInformationDefaultInterval = 2000;

private bool rateLimited = false;

#endregion

Expand Down Expand Up @@ -349,11 +351,20 @@ private void UpdateSpotifyTrackInformation_Elapsed(object sender, ElapsedEventAr
this.updateSpotifyTrackInformation.Enabled = false;
this.updateSpotifyTrackInformation.Interval = updateSpotifyTrackInformationDefaultInterval;
this.updateSpotifyTrackInformation.Enabled = true;
this.rateLimited = false;
}
else
{
// If the downloaded JSON is null or empty it's likely because there's no player running
this.ResetSnipSinceSpotifyIsNotPlaying();
if (this.rateLimited)
{
// If we are rate limited let's just not update or do anything yet. Once there is a successful update
// then the information will update accordingly.
}
else
{
// If the downloaded JSON is null or empty it's likely because there's no player running
this.ResetSnipSinceSpotifyIsNotPlaying();
}
}
}

Expand Down Expand Up @@ -482,6 +493,8 @@ private string DownloadJson(string jsonAddress, SpotifyAddressContactType spotif
{
if (webHeaderCollection.GetKey(i).ToUpperInvariant() == "RETRY-AFTER")
{
this.rateLimited = true;

// Set the timer to the retry seconds. Plus 1 for safety.
this.updateSpotifyTrackInformation.Enabled = false;
this.updateSpotifyTrackInformation.Interval = (Double.Parse(webHeaderCollection.Get(i) + 1, CultureInfo.InvariantCulture)) * 1000;
Expand Down

0 comments on commit 9d00b40

Please sign in to comment.