From a26efaac95be3a0678c3bd02be84c0443a4ef82c Mon Sep 17 00:00:00 2001 From: cganas Date: Mon, 28 Apr 2014 02:23:27 -0400 Subject: [PATCH] added spotify support. --- i3pystatus/spotify.py | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 i3pystatus/spotify.py diff --git a/i3pystatus/spotify.py b/i3pystatus/spotify.py new file mode 100644 index 0000000..9ac3455 --- /dev/null +++ b/i3pystatus/spotify.py @@ -0,0 +1,71 @@ +from i3pystatus import IntervalModule + +import threading, math +from gi.repository import Playerctl, GLib + +class Spotify(IntervalModule): + + """ + This class shows information from Spotify. + + Left click will toggle pause/play of the current song. + Right click will skip the song. + + Dependent on Playerctl and GLib + """ + + format = "{artist} - {title}" + color = "#ffffff" + + settings = ( + ("format", "Format string. {artist}, {title}, {album}, {volume}, and {length} are available for output."), + ("color", "color of the output"), + ) + + def main_loop(self): + """ Mainloop blocks so we thread it.""" + self.player = Playerctl.Player() + self.player.on('metadata', self.on_track_change) + main = GLib.MainLoop() + main.run() + + def init(self): + try: + t = threading.Thread(target=self.main_loop) + t.daemon = True + t.start() + except Exception as e: + self.output = { + "full_text": "Error creating new thread!", + "color" : "#FF0000" + } + + def on_track_change(self, player, e): + artist = player.get_artist() + title = player.get_title() + album = player.get_album() + volume = player.props.volume + + time = e["mpris:length"] / 60.0e6 + minutes = math.floor(time) + seconds = round(time % 1 * 60) + if seconds < 10: seconds = "0" + str(seconds) + length = "{}:{}".format(minutes, seconds) + + self.output = { + "full_text": self.format.format( + artist=artist, title=title, + album=album, length=length, + volume=volume), + "color": self.color + } + + def on_leftclick(self): + self.player.play_pause() + + def on_rightclick(self): + self.player.next() + + def run(self): + pass +