Skip to content

Instantly share code, notes, and snippets.

@sammerry
Last active December 29, 2023 16:54
Show Gist options
  • Select an option

  • Save sammerry/8d7d3cf130d5b025269945e5b1dd2c7d to your computer and use it in GitHub Desktop.

Select an option

Save sammerry/8d7d3cf130d5b025269945e5b1dd2c7d to your computer and use it in GitHub Desktop.
Mopidy + Polybar

Mopidy Polybar

This takes the currently playing track and displays it in polybar.

In /etc/pulse/default.pa you'll want to allow anonymous on local unix sockets

load-module module-native-protocol-unix auth-anonymous=1

Alias mopidy and ncmpcpp in your ~/.bashrc or ~/.bash_aliases.

# Mopidy music server
alias mopidy='docker run -d --rm --name mopidy \
  -v /run/user/$UID/pulse/native:/tmp/pulse/native \
  -e "PULSE_SERVER=unix:/tmp/pulse/native" \
  -p 6600:6600/tcp -p 6680:6680/tcp -p 5555:5555/udp \
  sammerry/mopidy mopidy\
  -o spotify/username=[redacted] -o spotify/password=[redacted] \
  -o spotify/client_id=[redacted] \
  -o spotify/client_secret=[redacted]

# Ncmpcpp music client
alias ncmpcpp='docker run --rm -it \
  -v $HOME/.config/ncmpcpp:/home/ncmpcpp/.ncmpcpp --name ncmpcpp \
  --net container:mopidy sammerry/ncmpcpp ncmpcpp'

check that you can play music through mopidy and hear sound. This may need some tweaking for your system. Mopidy needs a little time to pull down your spotify, pandora, youtube... etc playlists so give it a moment.

mopidy
sleep 10
ncmpcpp

If you can see playlists in ncmpcpp and hear music through mopidy. You should be good to go. Place mopidy.sh in ~/.config/polybar/scripts/ and add the text in config to ~/.config/polybar/config then run chmod +x mopidy.sh

once you start streaming music you should see the track in polybar.

modules-center = mopidy
################################################################################
[module/mopidy]
type = custom/script
exec = ~/.config/polybar/scripts/mopidy.sh
interval = 2
;format = <label>
format-foreground = ${colors.foreground}
format-background = ${colors.background}
format-padding = 2
format-prefix-foreground = #0f0
label = %output:0:150%
################################################################################
#!/bin/sh
# Queries the mopidy API for artist and title
#
# requires curl and jq
# place this script in `~/.config/polybar/scripts/mopidy.sh
#
#
set -e
set -o pipefail
MOPIDY_SERVER=${MOPIDY_SERVER:-"http://localhost:6680"}
main() {
RETURN=$(curl --silent -f -XPOST $MOPIDY_SERVER/mopidy/rpc)
POSTDATA='{"jsonrpc": "2.0", "id": 1, "method": "core.playback.get_current_track"}'
DATA=$(curl --silent -d "${POSTDATA}" $MOPIDY_SERVER/mopidy/rpc)
ARTIST=$(echo $DATA | jq '.result.artists[0].name')
ALBUM=$(echo $DATA | jq '.result.album.name')
TITLE=$(echo $DATA | jq '.result.name')
echo "$ARTIST - $TITLE" | sed s/\"//g | sed s/null\\s-.*//
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment