Browse Source

Add command for current MPRIS track info

It reads the title, and if available, the artist(s).
Clara Hobbs 6 years ago
parent
commit
0429a21a17
1 changed files with 34 additions and 1 deletions
  1. 34
    1
      kayleevc/plugins/mpris.py

+ 34
- 1
kayleevc/plugins/mpris.py View File

@@ -44,7 +44,8 @@ class Plugin(PluginBase):
44 44
             'play music': MPRISPlayHandler,
45 45
             'play video': MPRISPlayHandler,
46 46
             'previous song': MPRISPreviousHandler,
47
-            'previous video': MPRISPreviousHandler
47
+            'previous video': MPRISPreviousHandler,
48
+            'whats playing': MPRISTrackInfoHandler
48 49
         }
49 50
 
50 51
         self.corpus_strings.update(self.commands)
@@ -122,3 +123,35 @@ class MPRISPreviousHandler(MPRISHandler):
122 123
             self.proxy.Previous()
123 124
         except GLib.Error:
124 125
             raise HandlerFailure()
126
+
127
+
128
+class MPRISTrackInfoHandler(MPRISHandler):
129
+    """Handler for getting info about the currently playing track from MPRIS"""
130
+
131
+    def __call__(self, tts):
132
+        """Get and speak info about the currently playing track"""
133
+        # Get metadata about the player
134
+        try:
135
+            metadata = self.proxy.Metadata
136
+        except GLib.Error:
137
+            raise HandlerFailure()
138
+
139
+        # Get the title
140
+        try:
141
+            title = metadata['xesam:title']
142
+        except KeyError:
143
+            raise HandlerFailure()
144
+
145
+        # Get the artist list (optional)
146
+        try:
147
+            artist_list = metadata['xesam:artist']
148
+        except KeyError:
149
+            artist_text = ''
150
+        else:
151
+            # Get the artists in a speakable format
152
+            artist_text = ' by ' + ', '.join(
153
+                    artist_list[:-2] + [', and '.join(artist_list[-2:])])
154
+
155
+
156
+        # Speak the information
157
+        tts(f"{title}{artist_text}")

Loading…
Cancel
Save