Polybar: Add spotify status script, adjust default bar size

Thanks Jvanrhijn for the script
This commit is contained in:
Salt 2018-08-04 22:06:32 -05:00
parent a7d14dc5e2
commit 3b56f906cc
2 changed files with 80 additions and 2 deletions

View File

@ -19,6 +19,7 @@ icon-date = ${res/colors.primary}
icon-dropbox = ${res/colors.primary}
icon-fs = ${xrdb:color11}
icon-memory = ${xrdb:color13}
icon-spotify = ${xrdb:color2}
icon-tor = ${xrdb:color13}
icon-volume = ${res/colors.primary}
icon-network = ${xrdb:color14}
@ -35,7 +36,7 @@ bar-empty = ${self.border}
[res/config]
padding = 4
bar-width = ${env:PB_MODULE_BAR_WIDTH:50}
bar-width = ${env:PB_MODULE_BAR_WIDTH:45}
bar-indicator =
bar-fill = |
@ -96,7 +97,7 @@ tray-position = center
modules-left = ${env:PB_BAR_PRIMARY_MODULES_LEFT:battery cpu memory}
modules-center = ${env:PB_BAR_PRIMARY_MODULES_CENTER: }
modules-right = ${env:PB_BAR_PRIMARY_MODULES_RIGHT:xbacklight volume | dropbox date}
modules-right = ${env:PB_BAR_PRIMARY_MODULES_RIGHT:xbacklight volume | spotify dropbox date}
[bar/primary-2]
monitor = ${env:PB_MONITOR:eDP-1}
@ -305,6 +306,16 @@ type = custom/text
content = ${env:PB_MONITOR:eDP-1}
content-foreground = ${res/colors.background-alt}
[module/spotify]
type = custom/script
format = <label>
format-prefix = "Spotify: "
format-prefix-foreground = ${res/colors.icon-spotify}
interval = 3
exec = python $HOME/.config/polybar/scripts/spotify.py
exec-if = pgrep spotify
[module/swap]
type = internal/memory
format = <label>

View File

@ -0,0 +1,67 @@
#!/bin/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'
)
args = parser.parse_args()
# Default parameters
output = '{artist}: {song}'
trunclen = 25
# 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
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')
artist = metadata['xesam:artist'][0]
song = metadata['xesam:title']
if len(song) > trunclen:
song = song[0:trunclen]
song += '...'
if ('(' in song) and (')' not in song):
song += ')'
# Python3 uses UTF-8 by default.
if sys.version_info.major == 3:
print(output.format(artist=artist, song=song))
else:
print(output.format(artist=artist, song=song).encode('UTF-8'))
except Exception as e:
if isinstance(e, dbus.exceptions.DBusException):
print('')
else:
print(e)