Skip to content

Instantly share code, notes, and snippets.

@vreon
Last active April 24, 2017 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vreon/a366fc74daff3c6f1e5ce5bd562b71b9 to your computer and use it in GitHub Desktop.
Save vreon/a366fc74daff3c6f1e5ce5bd562b71b9 to your computer and use it in GitHub Desktop.
Sync currently-playing mpd track to Slack
#!/bin/bash
# Sync currently-playing mpd track to Slack
# Requires mpc, jq, curl
# Contains SLACK_TOKEN
source ~/.zshrc_secrets
prev_playing=''
while true; do
if ! mpc > /dev/null; then
# wait for something mpd-compatible to be available
sleep 30s
else
now_playing=$(mpc current -f "[%artist% - ][%album% - ][%title%|%file%]")
payload=$(jq -n '{status_emoji: ":notes:", status_text: $np}' --arg np "${now_playing}")
# mpc unblocks for play/pause events
# be nice to Slack and don't POST unless the track actually changed
if [ "${now_playing}" != "${prev_playing}" ]; then
prev_playing="${now_playing}"
echo "${now_playing}"
curl -s -X POST 'https://slack.com/api/users.profile.set' \
--data-urlencode "token=${SLACK_TOKEN}" \
--data-urlencode "profile=${payload}" \
> /dev/null
fi
# wait until track change
mpc idle > /dev/null
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment