Skip to content

Commit

Permalink
Prepare for shuffling local tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
byBlurr committed Feb 12, 2021
1 parent d4dff2c commit 8023efe
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions SpotifyShuffle/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ private static void Main(string[] args)
private static SpotifyClient client;
private static EmbedIOAuthServer server;

private static int Tracks = 0;
private static int Locals = 0;

/// <summary>
/// Creates the server and handles the authorisation request
/// </summary>
Expand Down Expand Up @@ -96,12 +99,12 @@ private async Task OnImplicitGrantReceivedAsync(object sender, ImplictGrantRespo

// get all the tracks from the playlist and populate the lists
await GetAllTracks(playlistUri, allTracks, loops);
int tracks = PopulateSongLists(allTracks, songs, songsToRemove);
PopulateSongLists(allTracks, songs, songsToRemove);

// recalculate the loops and remainder of the playlist, some of the tracks may have been invalid
loops = tracks / 100;
remainder = tracks % 100;
Log(LogType.Info, "Shuffle", $"Tracks: {tracks}, Loops: {loops}, Remainder: {remainder}");
loops = Tracks / 100;
remainder = Tracks % 100;
Log(LogType.Info, "Shuffle", $"Tracks: {Tracks}, Loops: {loops}, Remainder: {remainder}, Local tracks: {Locals}");

// do the actual shuffle
List<string> shuffled = Shuffle(songs);
Expand All @@ -112,6 +115,8 @@ private async Task OnImplicitGrantReceivedAsync(object sender, ImplictGrantRespo
await Task.Delay(100);
await AddSongsToPlaylist(playlistUri, shuffled, loops);

// TODO: Shuffle local tracks

Log(LogType.Info, "Shuffle", "Playlist shuffle complete.");
}
else
Expand Down Expand Up @@ -224,10 +229,11 @@ private void ListPlaylists(string user, Paging<SimplePlaylist> playlists)
/// <param name="songs">The playlists uri list</param>
/// <param name="songsToRemove">The list of songs to remove</param>
/// <returns></returns>
private int PopulateSongLists(List<PlaylistTrack<IPlayableItem>> allTracks, List<Item> songs, List<Item> songsToRemove)
private void PopulateSongLists(List<PlaylistTrack<IPlayableItem>> allTracks, List<Item> songs, List<Item> songsToRemove)
{
Log(LogType.Info, "Shuffle", "Populating lists...");
int tracks = 0;
int locals = 0;
for (int i = allTracks.Count - 1; i >= 0; i--)
{
PlaylistTrack<IPlayableItem> track = allTracks[i];
Expand Down Expand Up @@ -257,13 +263,15 @@ private int PopulateSongLists(List<PlaylistTrack<IPlayableItem>> allTracks, List
}
else
{
locals++;
Log(LogType.Warning, "Shuffle", "Found a local song. Skipping...");
}
}
else Log(LogType.Warning, "Shuffle", "Found an unavailable song. Skipping...");
}

return tracks;
Tracks = tracks;
Locals = locals;
}

/// <summary>
Expand Down

0 comments on commit 8023efe

Please sign in to comment.