Skip to content

Commit cf48cdb

Browse files
committed
fix: Ensure we place songs on the correct yearly playlist based on the year they were liked
1 parent 205c87d commit cf48cdb

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/workflows/spotify_yearly_playlist.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,29 @@ impl Job for SpotifyYearlyPlaylistWorkflow {
2828
let new_tracks = collector.list(&services).await?;
2929

3030
if !new_tracks.is_empty() {
31-
crate::publishers::SpotifyAddToPlaylist::dispatch(
32-
crate::publishers::SpotifyAddToPlaylistPayload {
33-
account_id: user.id.clone(),
34-
name: format!("{} Liked Songs", chrono::Utc::now().year()),
35-
description: Some("A yearly playlist of all my liked songs.".to_string()),
36-
access_token: token.clone(),
37-
track_uris: new_tracks.iter().map(|t| t.track.uri.clone()).collect(),
31+
let year_groups = new_tracks.iter().fold(
32+
std::collections::HashMap::<i32, Vec<&crate::publishers::spotify::SpotifySavedTrack>>::new(),
33+
|mut acc, track| {
34+
let year = track.added_at.year();
35+
acc.entry(year).or_default().push(track);
36+
acc
3837
},
39-
None,
40-
&services,
41-
)
42-
.await?;
38+
);
39+
40+
for (year, tracks) in year_groups {
41+
crate::publishers::SpotifyAddToPlaylist::dispatch(
42+
crate::publishers::SpotifyAddToPlaylistPayload {
43+
account_id: user.id.clone(),
44+
name: format!("{} Liked Songs", year),
45+
description: Some(format!("A yearly playlist of all my liked songs from {}.", year)),
46+
access_token: token.clone(),
47+
track_uris: tracks.iter().map(|t| t.track.uri.clone()).collect(),
48+
},
49+
None,
50+
&services,
51+
)
52+
.await?;
53+
}
4354
}
4455

4556
Self::dispatch_delayed(token, Some(user.id.into()), TimeDelta::hours(1), &services).await?;

0 commit comments

Comments
 (0)