From c82f19c5296a85f79086b40cc00b75853cd90e4d Mon Sep 17 00:00:00 2001 From: Pierre Guilbert Date: Thu, 26 Mar 2015 18:03:42 +0100 Subject: adding module to display current-song --- py3status/modules/spotify.py | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 py3status/modules/spotify.py (limited to 'py3status/modules/spotify.py') diff --git a/py3status/modules/spotify.py b/py3status/modules/spotify.py new file mode 100644 index 0000000..78321c1 --- /dev/null +++ b/py3status/modules/spotify.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +""" +This module displays the current "artist - title" playing in Spotify. + +Last modified: 2015-03-26 +Author: Pierre Guilbert LASSERRE +License: GNU GPL http://www.gnu.org/licenses/gpl.html +""" + +from time import time +from subprocess import check_output +import dbus, dbus.proxies + +class Py3status: + + # available configuration parameters + cache_timeout = 0 + + def getText(self): + """ + Get the current song metadatas (artist - title) + """ + + bus = dbus.SessionBus() + + try: + self.__bus = bus.get_object('com.spotify.qt', '/') + self.player = dbus.Interface(self.__bus, 'org.freedesktop.MediaPlayer2') + + title = self.player.GetMetadata().get('xesam:title') + artist = self.player.GetMetadata().get('xesam:artist')[0] + album = self.player.GetMetadata().get('xesam:album') + return title + " by " + artist + except Exception: + return "Spotify not running" + + def spotify(self, i3s_output_list, i3s_config): + """ + Get the current "artist - title" and return it. + """ + response = {'full_text': ''} + + response['cached_until'] = time() + self.cache_timeout + response['full_text'] = self.getText() or 'py3status module not working as expected' + + return response + +if __name__ == "__main__": + """ + Test this module by calling it directly. + """ + from time import sleep + x = Py3status() + config = { + 'color_good': '#00FF00', + 'color_bad': '#FF0000', + } + while True: + print(x.spotify([], config)) + sleep(1) -- cgit v1.3 From 53050c95eaa08b58c2726b49c7435893324c6388 Mon Sep 17 00:00:00 2001 From: Pierre Guilbert Date: Thu, 26 Mar 2015 18:05:44 +0100 Subject: fixing dbus and header --- py3status/modules/spotify.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'py3status/modules/spotify.py') diff --git a/py3status/modules/spotify.py b/py3status/modules/spotify.py index 78321c1..a797bb3 100644 --- a/py3status/modules/spotify.py +++ b/py3status/modules/spotify.py @@ -3,12 +3,11 @@ This module displays the current "artist - title" playing in Spotify. Last modified: 2015-03-26 -Author: Pierre Guilbert LASSERRE +Author: Pierre Guilbert License: GNU GPL http://www.gnu.org/licenses/gpl.html """ from time import time -from subprocess import check_output import dbus, dbus.proxies class Py3status: -- cgit v1.3 From 1ac61292ef0b4a25283eb53661df96dceee274c2 Mon Sep 17 00:00:00 2001 From: Pierre Guilbert Date: Tue, 31 Mar 2015 11:43:13 +0200 Subject: adding support for formating output of spotify module --- py3status/modules/spotify.py | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) (limited to 'py3status/modules/spotify.py') diff --git a/py3status/modules/spotify.py b/py3status/modules/spotify.py index a797bb3..f70afae 100644 --- a/py3status/modules/spotify.py +++ b/py3status/modules/spotify.py @@ -5,31 +5,58 @@ This module displays the current "artist - title" playing in Spotify. Last modified: 2015-03-26 Author: Pierre Guilbert License: GNU GPL http://www.gnu.org/licenses/gpl.html + +You could conf it in your i3status.conf like so: + + + +spotify { + format = "{title} by {artist} -> {time}" +} + + """ from time import time -import dbus, dbus.proxies +import dbus + class Py3status: + """ + Confiuration parameters: + cache_timeout + Format of status string placeholders: + title - name of the song + artist - artiste name (first one) + album - album name + time - time duration of the song + """ # available configuration parameters cache_timeout = 0 + format = "{title}: {album} : {artist} : {time}" def getText(self): """ Get the current song metadatas (artist - title) """ - + bus = dbus.SessionBus() try: - self.__bus = bus.get_object('com.spotify.qt', '/') - self.player = dbus.Interface(self.__bus, 'org.freedesktop.MediaPlayer2') + self.__bus = bus.get_object('com.spotify.qt', '/') + self.player = dbus.Interface( + self.__bus, 'org.freedesktop.MediaPlayer2') title = self.player.GetMetadata().get('xesam:title') artist = self.player.GetMetadata().get('xesam:artist')[0] album = self.player.GetMetadata().get('xesam:album') - return title + " by " + artist + from datetime import timedelta + microtime = self.player.GetMetadata().get('mpris:length') + time = str(timedelta(microseconds=microtime)) + + return self.format.format(title=title, + artist=artist, album=album, time=time) except Exception: return "Spotify not running" @@ -40,7 +67,8 @@ class Py3status: response = {'full_text': ''} response['cached_until'] = time() + self.cache_timeout - response['full_text'] = self.getText() or 'py3status module not working as expected' + response['full_text'] = self.getText( + ) or 'py3status module not working as expected' return response -- cgit v1.3 From 2c4bdb3120df98953c52ed087f6a692f385be231 Mon Sep 17 00:00:00 2001 From: Pierre Guilbert Date: Tue, 31 Mar 2015 12:11:51 +0200 Subject: removing licence gpl: replacing default format by a simpler one --- py3status/modules/spotify.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'py3status/modules/spotify.py') diff --git a/py3status/modules/spotify.py b/py3status/modules/spotify.py index f70afae..8f51c2c 100644 --- a/py3status/modules/spotify.py +++ b/py3status/modules/spotify.py @@ -2,13 +2,10 @@ """ This module displays the current "artist - title" playing in Spotify. -Last modified: 2015-03-26 +Last modified: 2015-03-31 Author: Pierre Guilbert -License: GNU GPL http://www.gnu.org/licenses/gpl.html - -You could conf it in your i3status.conf like so: - +i3status.conf example: spotify { format = "{title} by {artist} -> {time}" @@ -34,7 +31,7 @@ class Py3status: """ # available configuration parameters cache_timeout = 0 - format = "{title}: {album} : {artist} : {time}" + format = "{artist} : {title}" def getText(self): """ -- cgit v1.3