Update playlist every hour

This commit is contained in:
2025-07-30 18:55:56 -04:00
parent 13fcf08dd6
commit 0b3534348f

View File

@ -40,6 +40,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let url = spotify.get_authorize_url(false)?; let url = spotify.get_authorize_url(false)?;
spotify.prompt_for_token(&url).await?; spotify.prompt_for_token(&url).await?;
loop {
println!("Updating playlist...");
top_songs_playlist(&spotify, &playlist_name, track_limit).await?;
// Wait 1 hour before updating again
tokio::time::sleep(std::time::Duration::from_secs(60 * 60)).await;
}
}
async fn top_songs_playlist<'a>(spotify: &AuthCodeSpotify, playlist_name: &str, track_limit: usize) -> Result<(), ClientError> {
let mut top_tracks = spotify.current_user_top_tracks(Some(TimeRange::ShortTerm)); let mut top_tracks = spotify.current_user_top_tracks(Some(TimeRange::ShortTerm));
let mut top_track_ids = Vec::new(); let mut top_track_ids = Vec::new();
@ -52,7 +62,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
println!("Adding top tracks to playlist: {}", playlist_name); println!("Adding top tracks to playlist: {}", playlist_name);
let playlist_id = find_or_create_playlist(&spotify, &playlist_name, spotify.me().await?.id).await?; let playlist_id = find_or_create_playlist(&spotify, playlist_name, spotify.me().await?.id).await?;
spotify.playlist_replace_items(playlist_id, top_track_ids.into_iter().map(|track| PlayableId::Track(track))).await?; spotify.playlist_replace_items(playlist_id, top_track_ids.into_iter().map(|track| PlayableId::Track(track))).await?;
Ok(()) Ok(())
} }