Merge pull request #256 from SibrenVasse/spotify-json

Convert spotify module to generic json version
pull/265/head
Alex 2019-03-31 16:30:16 +02:00 committed by GitHub
commit 0ad9a03d3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 23 deletions

View File

@ -4,7 +4,7 @@
// "height": 30, // Waybar height // "height": 30, // Waybar height
// "width": 1280, // Waybar width // "width": 1280, // Waybar width
// Choose the order of the modules // Choose the order of the modules
"modules-left": ["sway/workspaces", "sway/mode", "custom/spotify"], "modules-left": ["sway/workspaces", "sway/mode", "custom/media"],
"modules-center": ["sway/window"], "modules-center": ["sway/window"],
"modules-right": ["idle_inhibitor", "pulseaudio", "network", "cpu", "memory", "temperature", "backlight", "battery", "battery#bat2", "clock", "tray"], "modules-right": ["idle_inhibitor", "pulseaudio", "network", "cpu", "memory", "temperature", "backlight", "battery", "battery#bat2", "clock", "tray"],
// Modules configuration // Modules configuration
@ -95,9 +95,14 @@
}, },
"on-click": "pavucontrol" "on-click": "pavucontrol"
}, },
"custom/spotify": { "custom/media": {
"format": " {}", "format": "{icon} {}",
"return-type": "json",
"max-length": 40, "max-length": 40,
"format-icons": {
"spotify": "",
"default": "🎜"
},
"escape": true, "escape": true,
"exec": "$HOME/.config/waybar/mediaplayer.py 2> /dev/null" // Script in resources folder "exec": "$HOME/.config/waybar/mediaplayer.py 2> /dev/null" // Script in resources folder
} }

View File

@ -4,19 +4,31 @@ import logging
import sys import sys
import signal import signal
import gi import gi
import json
gi.require_version('Playerctl', '2.0') gi.require_version('Playerctl', '2.0')
from gi.repository import Playerctl, GLib from gi.repository import Playerctl, GLib
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def write_output(text, player):
logger.info('Writing output')
output = {'text': text,
'class': 'custom-' + player.props.player_name,
'alt': player.props.player_name}
sys.stdout.write(json.dumps(output) + '\n')
sys.stdout.flush()
def on_play(player, status, manager): def on_play(player, status, manager):
logger.info('Received new playback status') logger.info('Received new playback status')
on_metadata(player, player.props.metadata, manager) on_metadata(player, player.props.metadata, manager)
def on_metadata(player, metadata, manager): def on_metadata(player, metadata, manager):
logger.info("Received new metadata") logger.info('Received new metadata')
track_info = '' track_info = ''
if player.props.player_name == 'spotify' and \ if player.props.player_name == 'spotify' and \
@ -26,33 +38,27 @@ def on_metadata(player, metadata, manager):
elif player.get_artist() != '' and player.get_title() != '': elif player.get_artist() != '' and player.get_title() != '':
track_info = '{artist} - {title}'.format(artist=player.get_artist(), track_info = '{artist} - {title}'.format(artist=player.get_artist(),
title=player.get_title()) title=player.get_title())
else:
sys.stdout.write('\n')
sys.stdout.flush()
return
if player.props.status == 'Playing': if player.props.status != 'Playing':
sys.stdout.write(track_info + '\n') track_info = '' + track_info
else: write_output(track_info, player)
sys.stdout.write('' + track_info + '\n')
sys.stdout.flush()
def on_player_appeared(manager, player, selected_player=None): def on_player_appeared(manager, player, selected_player=None):
if player is not None and player.name == selected_player: if player is not None and player.name == selected_player:
init_player(manager, player) init_player(manager, player)
else: else:
logger.debug('New player appeared, but it\'s not the selected player, skipping') logger.debug("New player appeared, but it's not the selected player, skipping")
def on_player_vanished(manager, player): def on_player_vanished(manager, player):
logger.info("Player has vanished") logger.info('Player has vanished')
sys.stdout.write("\n") sys.stdout.write('\n')
sys.stdout.flush() sys.stdout.flush()
def init_player(manager, name): def init_player(manager, name):
logger.debug("Initialize player: {player}".format(player=name.name)) logger.debug('Initialize player: {player}'.format(player=name.name))
player = Playerctl.Player.new_from_name(name) player = Playerctl.Player.new_from_name(name)
player.connect('playback-status', on_play, manager) player.connect('playback-status', on_play, manager)
player.connect('metadata', on_metadata, manager) player.connect('metadata', on_metadata, manager)
@ -62,7 +68,7 @@ def init_player(manager, name):
def signal_handler(sig, frame): def signal_handler(sig, frame):
logger.debug('Received signal to stop, exiting') logger.debug('Received signal to stop, exiting')
sys.stdout.write("\n") sys.stdout.write('\n')
sys.stdout.flush() sys.stdout.flush()
# loop.quit() # loop.quit()
sys.exit(0) sys.exit(0)
@ -72,7 +78,7 @@ def parse_arguments():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
# Increase verbosity with every occurance of -v # Increase verbosity with every occurance of -v
parser.add_argument('-v', '--verbose', action="count", default=0) parser.add_argument('-v', '--verbose', action='count', default=0)
# Define for which player we're listening # Define for which player we're listening
parser.add_argument('--player') parser.add_argument('--player')
@ -88,7 +94,7 @@ def main():
format='%(name)s %(levelname)s %(message)s') format='%(name)s %(levelname)s %(message)s')
# Logging is set by default to WARN and higher. # Logging is set by default to WARN and higher.
# With every occurance of -v it's lowered by one # With every occurrence of -v it's lowered by one
logger.setLevel(max((3 - arguments.verbose) * 10, 0)) logger.setLevel(max((3 - arguments.verbose) * 10, 0))
# Log the sent command line arguments # Log the sent command line arguments
@ -115,6 +121,6 @@ def main():
loop.run() loop.run()
if __name__ == "__main__": if __name__ == '__main__':
main() main()

View File

@ -30,7 +30,7 @@ window#waybar {
border-bottom: 3px solid #ffffff; border-bottom: 3px solid #ffffff;
} }
#clock, #battery, #cpu, #memory, #temperature, #backlight, #network, #pulseaudio, #custom-spotify, #tray, #mode, #idle_inhibitor { #clock, #battery, #cpu, #memory, #temperature, #backlight, #network, #pulseaudio, #custom-media, #tray, #mode, #idle_inhibitor {
padding: 0 10px; padding: 0 10px;
margin: 0 5px; margin: 0 5px;
} }
@ -97,11 +97,19 @@ window#waybar {
color: #2a5c45; color: #2a5c45;
} }
#custom-spotify { #custom-media {
background: #66cc99; background: #66cc99;
color: #2a5c45; color: #2a5c45;
} }
.custom-spotify {
background: #66cc99;
}
.custom-vlc {
background: #ffa000;
}
#temperature { #temperature {
background: #f0932b; background: #f0932b;
} }