Spotify dbus events no longer always include Metadata and Playback
authorJacob Casper <dev@jacobcasper.com>
Thu, 17 Aug 2023 17:21:08 +0000 (12:21 -0500)
committerJacob Casper <dev@jacobcasper.com>
Thu, 17 Aug 2023 17:21:08 +0000 (12:21 -0500)
Where these could previously be referenced unconditionally, the code now
panics due to spotify not always sending these in the dbus properties
changed when songs change.

Add more match clauses when pulling them from the hash map.

src/main.rs

index 800cf54534953f41411b65342c1a4825565e835c..d6beac7ba3346ed73d4af4f776ecc6d03e22a5ef 100644 (file)
@@ -95,41 +95,49 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
     let spotify_match_map = arc_locked_xset_map.clone();
     let _ = spotify_proxy.match_signal(
         move |pc: PropertiesPropertiesChanged, _: &Connection, _: &Message| {
-            pc.changed_properties["Metadata"]
-                .0
-                .as_iter()
-                .and_then(|mut iter| {
-                    // Iterating over a RefArg goes key, value, key, value... Insane honestly.
-                    while let Some(key) = iter.next() {
-                        let key_str = key.as_str()?;
-                        let value = iter.next();
+            match pc.changed_properties.get("Metadata") {
+                Some(variant) => {
+                    variant.0.as_iter().and_then(|mut iter| {
+                        // Iterating over a RefArg goes key, value, key, value... Insane honestly.
+                        while let Some(key) = iter.next() {
+                            let key_str = key.as_str()?;
+                            let value = iter.next();
 
-                        match key_str {
-                            "xesam:artist" => {
-                                // Variant holding a variant that should just be a Vec<&str> I
-                                // believe. This is _the recommended_ way to do this by the author.
-                                let inner_value = value?.as_iter()?.next()?.as_iter()?.next()?;
-                                let artist = inner_value.as_str()?;
-                                update_map(spotify_match_map.clone(), "artist", artist);
+                            match key_str {
+                                "xesam:artist" => {
+                                    // Variant holding a variant that should just be a Vec<&str> I
+                                    // believe. This is _the recommended_ way to do this by the author.
+                                    let inner_value =
+                                        value?.as_iter()?.next()?.as_iter()?.next()?;
+                                    let artist = inner_value.as_str()?;
+                                    update_map(spotify_match_map.clone(), "artist", artist);
+                                }
+                                "xesam:title" => {
+                                    let title = value?.as_iter()?.next()?.as_str()?;
+                                    update_map(spotify_match_map.clone(), "title", title);
+                                }
+                                _ => (),
                             }
-                            "xesam:title" => {
-                                let title = value?.as_iter()?.next()?.as_str()?;
-                                update_map(spotify_match_map.clone(), "title", title);
-                            }
-                            _ => (),
                         }
-                    }
-                    Some(())
-                });
-            let playback_status = &pc.changed_properties["PlaybackStatus"].0;
-            update_map(
-                spotify_match_map.clone(),
-                "playback_status",
-                match &*playback_status.as_str().unwrap_or("") {
-                    "Playing" => "🔊",
-                    _ => "🔈",
-                },
-            );
+                        Some(())
+                    });
+                }
+                None => (),
+            }
+            match &pc.changed_properties.get("PlaybackStatus") {
+                Some(variant) => {
+                    let playback_status = &variant.0;
+                    update_map(
+                        spotify_match_map.clone(),
+                        "playback_status",
+                        match &*playback_status.as_str().unwrap_or("") {
+                            "Playing" => "🔊",
+                            _ => "🔈",
+                        },
+                    );
+                }
+                None => (),
+            }
             true
         },
     );