diff options
author | ThanosApollo <[email protected]> | 2022-10-11 10:09:52 +0300 |
---|---|---|
committer | ThanosApollo <[email protected]> | 2022-10-11 10:09:52 +0300 |
commit | 33402c07042ae695e9ba4a8dec33d384575e18df (patch) | |
tree | 61980d52078d2c0a43f9a8d1a817ba25a8f1602a /.config/qtile | |
parent | 1b5354a6dd6f856831a01f7b157d235778aafb07 (diff) |
Add spotify_widget
Diffstat (limited to '.config/qtile')
-rw-r--r-- | .config/qtile/spotify_widget.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/.config/qtile/spotify_widget.py b/.config/qtile/spotify_widget.py new file mode 100644 index 0000000..a34e962 --- /dev/null +++ b/.config/qtile/spotify_widget.py @@ -0,0 +1,68 @@ +# Copyright (C) 2022, ThanosApollo +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import dbus +import dbus_next +import subprocess +from functools import partial +from libqtile.widget import base + + +class Spotify(base.ThreadPoolText): + """A simple Spotify widget. + Show the song and artist of now listening song and allow basic mouse + control from the bar using spotify-control: + - toggle pause (or play if stopped) on left click; + - skip forward in playlist on scroll up; + - skip backward in playlist on scroll down. + """ + + defaults = [ + ("play_color", "00ff00", "Text colour when playing."), + ("noplay_color", "cecece", "Text colour when not playing."), + ("update_interval", 0.5, "Update Time in seconds."), + ] + + def __init__(self, **config): + base.ThreadPoolText.__init__(self, "", **config) + self.add_defaults(Spotify.defaults) + self.status = "PLAY" + self.local = None + self.add_callbacks( + { + "Button1": partial(subprocess.Popen, ["spotify-control", "play-pause"]), + "Button2": partial(subprocess.Popen, ["spotify-control", "previous"]), + "Button3": partial(subprocess.Popen, ["spotify-control", "next"]), + } + ) + + def now_playing(self): + """Return current song""" + session_bus = dbus.SessionBus() + bus_data = ("org.mpris.MediaPlayer2.spotify", "/org/mpris/MediaPlayer2") + spotify_bus = session_bus.get_object(*bus_data) + interface = dbus.Interface(spotify_bus, "org.freedesktop.DBus.Properties") + metadata = interface.Get("org.mpris.MediaPlayer2.Player", "Metadata") + data = { + "artist" : next(iter(metadata.get("xesam:albumArtist"))), + "song" : metadata.get("xesam:title") + } + song = data["song"] + " | " + data["artist"] + self.layout.colour = self.play_color + return song + + def poll(self): + """Poll content for the text box.""" + return self.now_playing() |