diff --git a/src/main.rs b/src/main.rs index dc6d889..ae94d6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,8 +74,8 @@ async fn main() -> Result<(), Box> { let data_http = web::Data::clone(&data); // Fetch spotify current playing every second. - let mut interval = time::interval(Duration::from_secs(1)); if config.env != "dev" { + let mut interval = time::interval(Duration::from_secs(1)); tokio::spawn(async move { interval.tick().await; loop { diff --git a/src/modules/spotify.rs b/src/modules/spotify.rs index dde9e7a..4a6b227 100644 --- a/src/modules/spotify.rs +++ b/src/modules/spotify.rs @@ -47,34 +47,30 @@ pub(crate) async fn fetch_spotify_current(data: web::Data) { ArtistName { name } } - if json.is_playing { + if json.is_playing && json.item.is_some() { + let item = json.item.unwrap(); let mut image: String = String::from("none"); let mut artists: Vec = [].to_vec(); - if json.item.album.is_some() && json.item.artists.is_some() { - image = json.item.album.unwrap().images[0].url.to_string(); - artists = json - .item - .artists - .unwrap() - .into_iter() - .map(get_name) - .collect(); - } else if json.item.show.is_some() { - let show = json.item.show.unwrap(); + if item.album.is_some() && item.artists.is_some() { + image = item.album.unwrap().images[0].url.to_string(); + artists = + item.artists.unwrap().into_iter().map(get_name).collect(); + } else if item.show.is_some() { + let show = item.show.unwrap(); image = show.images[0].url.to_string(); artists = [ArtistName { name: show.name }].to_vec() } let current = CurrentPlaying { - id: Some(json.item.id), - name: Some(json.item.name), + id: Some(item.id), + name: Some(item.name), current_playing_type: Some( - json.item.item_type.unwrap_or_else(|| String::from("track")), + item.item_type.unwrap_or_else(|| String::from("track")), ), playing: json.is_playing, artists: Some(artists), - length: Some(json.item.duration_ms), + length: Some(item.duration_ms), progress: Some(json.progress_ms), image: Some(image), device: Some(DeviceRewrite { diff --git a/src/structs/spotify.rs b/src/structs/spotify.rs index 8cdfb8f..6783d2a 100644 --- a/src/structs/spotify.rs +++ b/src/structs/spotify.rs @@ -15,7 +15,7 @@ pub struct PlayerState { pub timestamp: i64, pub context: Option, pub progress_ms: i64, - pub item: Item, + pub item: Option, #[serde(rename = "type")] pub currently_playing_type: Option, pub is_playing: bool,