From 83d7b9c7ce20f16681afacf99ed3ab47427f1ded Mon Sep 17 00:00:00 2001 From: Peter Son Struschka Date: Sun, 28 Feb 2021 17:58:30 +0800 Subject: all: fixes and new modules --- polybar/.local/bin/spotifycl | 74 ++++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 24 deletions(-) (limited to 'polybar/.local/bin/spotifycl') diff --git a/polybar/.local/bin/spotifycl b/polybar/.local/bin/spotifycl index ffee9f0..f7ced52 100755 --- a/polybar/.local/bin/spotifycl +++ b/polybar/.local/bin/spotifycl @@ -22,32 +22,34 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +"""Spotify DBus listener""" + import os import socket -import sys -import time -import traceback +import logging from concurrent.futures import ThreadPoolExecutor import click -import dbus -import dbus.mainloop.glib -import spotipy -import spotipy.util as util +import dbus # type: ignore +import dbus.mainloop.glib # type: ignore from dbus.mainloop.glib import DBusGMainLoop -from gi.repository import GLib -from spotipy import SpotifyException -from spotipy.oauth2 import SpotifyClientCredentials +from gi.repository import GLib # type: ignore +#from spotipy import SpotifyException +#from spotipy.oauth2 import SpotifyClientCredentials + +INACTIVE_COLOR = '%{F#6E6E6E}' +ACTIVE_COLOR = '%{F#CECECE}' +DEFAULT_COLOR = '%{F-}' -inactive_color = '%{F#6E6E6E}' -active_color = '%{F#CECECE}' -default_color = '%{F-}' +SERVER_ADDRESS = '/tmp/spotifycl-socket' -server_address = '/tmp/spotifycl-socket' class Spotify: + """Spotify DBus Listener""" + + # pylint: disable=too-many-instance-attributes SPOTIFY_BUS = 'org.mpris.MediaPlayer2.spotify' SPOTIFYD_BUS = 'org.mpris.MediaPlayer2.spotifyd' @@ -58,6 +60,9 @@ class Spotify: SAVE_REMOVE = b'save' + logging.basicConfig(filename="/tmp/spotifycl.log", level=logging.DEBUG) + logger = logging.getLogger("spotifycl") + def __init__(self): DBusGMainLoop(set_as_default=True) self.session_bus = dbus.SessionBus() @@ -70,8 +75,13 @@ class Spotify: self.saved_track = False # Whether to ignore the update self.ignore = False + # DBus session object + self.freedesktop = None + self.spotify = None def monitor(self): + """ Monitor """ + self.logger.info("monitor") self.setup_properties_changed() self.freedesktop = self.session_bus.get_object( "org.freedesktop.DBus", @@ -87,22 +97,26 @@ class Spotify: executor.submit(self._start_glib_loop) executor.submit(self._start_server) - def _start_glib_loop(self): + @staticmethod + def _start_glib_loop(): + """ Start Glib loop """ loop = GLib.MainLoop() loop.run() - def _start_server(self): + @staticmethod + def _start_server(): try: - os.unlink(server_address) + os.unlink(SERVER_ADDRESS) except OSError: - if os.path.exists(server_address): + if os.path.exists(SERVER_ADDRESS): raise sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - sock.bind(server_address) + sock.bind(SERVER_ADDRESS) sock.listen(5) @property def metadata_status(self): + """ Get song status """ spotify_properties = dbus.Interface( self.spotify, dbus_interface=Spotify.PROPERTIES_INTERFACE @@ -118,6 +132,7 @@ class Spotify: return metadata, playback_status def output(self, line): + """ Output for polybar """ if not line: self.empty_output = True if line != self.last_output: @@ -125,15 +140,21 @@ class Spotify: self.last_output = line def setup_spotify(self): + """ Setup spotify DBus session object """ + self.logger.info("setup spotify") try: self.spotify = self.session_bus.get_object( Spotify.SPOTIFY_BUS, Spotify.SPOTIFY_OBJECT_PATH ) - except dbus.DBusException: - self.spotify = self.session_bus.get_object(Spotify.SPOTIFYD_BUS, Spotify.SPOTIFY_OBJECT_PATH) + except dbus.DBusException as error: + self.logger.warning("DbusException: %s", error) + self.spotify = self.session_bus.get_object( + Spotify.SPOTIFYD_BUS, + Spotify.SPOTIFY_OBJECT_PATH) def setup_properties_changed(self): + """ Setup propertise changed """ try: self.setup_spotify() self.spotify.connect_to_signal( @@ -151,9 +172,11 @@ class Spotify: ) except dbus.DBusException: + self.logger.warning("Exception") self.output('') def output_playback_status(self, data, retry=False): + """ output current song """ if self.ignore: return @@ -169,17 +192,21 @@ class Spotify: playback_status = data['PlaybackStatus'] same_song = title == self.last_title - color = active_color if playback_status == 'Playing' else inactive_color + color = ACTIVE_COLOR if playback_status == 'Playing' else INACTIVE_COLOR # divider = '+' if same_song and self.saved_track else '-' - self.output(f'{color}{artist} - {title}{default_color}') + self.output(f'{color}{artist} - {title}{DEFAULT_COLOR}') if not same_song: self.last_title = title def on_properties_changed(self, interface, data, *args, **kwargs): + """On name properties changed event""" + del interface, args, kwargs self.output_playback_status(data) def on_name_owner_changed(self, name, old_owner, new_owner): + """On name owner changed event""" + del old_owner if name == self.SPOTIFY_BUS: if new_owner: # Spotify was opened. @@ -193,7 +220,6 @@ class Spotify: @click.group() def cli(): """Script for listening to Spotify over dbus and adding tracks to your library.""" - pass @cli.command() -- cgit v1.2.3