Skip to content

Commit

Permalink
Fix crash from spotify module when playing DJ
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinrouillard committed Dec 1, 2024
1 parent db3c736 commit e30fd13
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
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 {
Expand Down
28 changes: 12 additions & 16 deletions src/modules/spotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,30 @@ pub(crate) async fn fetch_spotify_current(data: web::Data<ServerState>) {
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<ArtistName> = [].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 {
Expand Down
2 changes: 1 addition & 1 deletion src/structs/spotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct PlayerState {
pub timestamp: i64,
pub context: Option<Context>,
pub progress_ms: i64,
pub item: Item,
pub item: Option<Item>,
#[serde(rename = "type")]
pub currently_playing_type: Option<String>,
pub is_playing: bool,
Expand Down

0 comments on commit e30fd13

Please sign in to comment.