Add spotify to polybar; controls for bluetooth volume

This commit is contained in:
Tommy Harris 2020-12-31 10:24:10 -07:00
parent e88a5609df
commit 1e4186dca2
Signed by: tobraha
GPG Key ID: 93769A4F97AE33E9
3 changed files with 156 additions and 32 deletions

View File

@ -61,7 +61,7 @@ font-5 = Font Awesome 5 Brands:pixelsize=12;2
modules-left = bspwm
modules-center = xwindow pacman-updates
modules-right = filesystem pulseaudio-control memory cpu eth date powermenu
modules-right = filesystem spotify pulseaudio-control memory cpu eth date powermenu
separator = |
tray-position = none
@ -103,7 +103,7 @@ type = custom/script
interval = 1
format-prefix = " "
format = <label>
exec = python /home/ermanno/.config/scripts/spotify_status.py -f '{artist}: {song}'
exec = python ~/scripts/spotify_status.py -f '{artist}: {song}'
format-underline = #1db954
[module/pulseaudio-control]
@ -124,29 +124,14 @@ click-middle = pulseaudio-control --sink-blacklist "alsa_output.pci-0000_01_00.1
scroll-up = pulseaudio-control --volume-max 130 up
scroll-down = pulseaudio-control --volume-max 130 down
[module/xkeyboard]
type = internal/xkeyboard
blacklist-0 = num lock
format-prefix = " "
format-prefix-foreground = ${colors.foreground-alt}
format-prefix-underline = ${colors.secondary}
label-layout = %layout%
label-layout-underline = ${colors.secondary}
label-indicator-padding = 2
label-indicator-margin = 1
label-indicator-background = ${colors.secondary}
label-indicator-underline = ${colors.secondary}
[module/filesystem]
type = internal/fs
interval = 25
mount-0 = /
mount-1 = /home
label-mounted =  %free%
label-mounted =  %mountpoint% %free%
label-unmounted = %mountpoint% not mounted
label-unmounted-foreground = ${colors.foreground-alt}

141
scripts/spotify_status.py Normal file
View File

@ -0,0 +1,141 @@
#!/usr/bin/env python
import sys
import dbus
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
'-t',
'--trunclen',
type=int,
metavar='trunclen'
)
parser.add_argument(
'-f',
'--format',
type=str,
metavar='custom format',
dest='custom_format'
)
parser.add_argument(
'-p',
'--playpause',
type=str,
metavar='play-pause indicator',
dest='play_pause'
)
parser.add_argument(
'--font',
type=str,
metavar='the index of the font to use for the main label',
dest='font'
)
parser.add_argument(
'--playpause-font',
type=str,
metavar='the index of the font to use to display the playpause indicator',
dest='play_pause_font'
)
parser.add_argument(
'-q',
'--quiet',
action='store_true',
help="if set, don't show any output when the current song is paused",
dest='quiet',
)
args = parser.parse_args()
def fix_string(string):
# corrects encoding for the python version used
if sys.version_info.major == 3:
return string
else:
return string.encode('utf-8')
def truncate(name, trunclen):
if len(name) > trunclen:
name = name[:trunclen]
name += '...'
if ('(' in name) and (')' not in name):
name += ')'
return name
# Default parameters
output = fix_string(u'{play_pause} {artist}: {song}')
trunclen = 35
play_pause = fix_string(u'\u25B6,\u23F8') # first character is play, second is paused
label_with_font = '%{{T{font}}}{label}%{{T-}}'
font = args.font
play_pause_font = args.play_pause_font
quiet = args.quiet
# parameters can be overwritten by args
if args.trunclen is not None:
trunclen = args.trunclen
if args.custom_format is not None:
output = args.custom_format
if args.play_pause is not None:
play_pause = args.play_pause
try:
session_bus = dbus.SessionBus()
spotify_bus = session_bus.get_object(
'org.mpris.MediaPlayer2.spotify',
'/org/mpris/MediaPlayer2'
)
spotify_properties = dbus.Interface(
spotify_bus,
'org.freedesktop.DBus.Properties'
)
metadata = spotify_properties.Get('org.mpris.MediaPlayer2.Player', 'Metadata')
status = spotify_properties.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus')
# Handle play/pause label
play_pause = play_pause.split(',')
if status == 'Playing':
play_pause = play_pause[0]
elif status == 'Paused':
play_pause = play_pause[1]
else:
play_pause = str()
if play_pause_font:
play_pause = label_with_font.format(font=play_pause_font, label=play_pause)
# Handle main label
artist = fix_string(metadata['xesam:artist'][0]) if metadata['xesam:artist'] else ''
song = fix_string(metadata['xesam:title']) if metadata['xesam:title'] else ''
album = fix_string(metadata['xesam:album']) if metadata['xesam:album'] else ''
if (quiet and status == 'Paused') or (not artist and not song and not album):
print('')
else:
if font:
artist = label_with_font.format(font=font, label=artist)
song = label_with_font.format(font=font, label=song)
album = label_with_font.format(font=font, label=album)
# Add 4 to trunclen to account for status symbol, spaces, and other padding characters
print(truncate(output.format(artist=artist,
song=song,
play_pause=play_pause,
album=album), trunclen + 4))
except Exception as e:
if isinstance(e, dbus.exceptions.DBusException):
print('')
else:
print(e)

View File

@ -13,12 +13,12 @@ super + alt + b ; {t,c,a}
# config files; open with emacs
super + alt + c ; {b,s,e,p,x,z,n}
emacs { "~/dotfiles/.config/bspwm/bspwmrc", \
"~/dotfiles/.config/sxhkd/sxhkdrc", \
"~/dotfiles/.spacemacs", \
emacs { "~/.config/bspwm/bspwmrc", \
"~/.config/sxhkd/sxhkdrc", \
"~/.spacemacs", \
"~/sync/candle/candle.txt.gpg", \
"~/.xinitrc", \
"~/dotfiles/.zshrc", \
"~/.zshrc", \
"~/sync/ATG/tommy_notebook.org" }
# program launcher
@ -147,8 +147,6 @@ super + {Left,Down,Up,Right}
# Audio Controls
#
# volume control - Dell Soundbar
XF86AudioLowerVolume
pactl set-sink-volume 1 -5%
@ -159,15 +157,15 @@ XF86AudioRaiseVolume
XF86AudioMute
pactl set-sink-volume 1 0%
# volume control - Logitech Headset
super + XF86AudioLowerVolume
pactl set-sink-volume 0 -5%
# volume control - Logitech Headset & bluetooth headphones
super + {_,alt + } XF86AudioLowerVolume
pactl set-sink-volume {0,4} -5%
super + XF86AudioRaiseVolume
pactl set-sink-volume 0 +5%
super + {_,alt + } XF86AudioRaiseVolume
pactl set-sink-volume {0,4} +5%
super + XF86AudioMute
pactl set-sink-volume 0 0%
super + {_,alt + } XF86AudioMute
pactl set-sink-volume {0,4} 0%
#
# Misc