Last active
April 24, 2017 20:21
-
-
Save vreon/a366fc74daff3c6f1e5ce5bd562b71b9 to your computer and use it in GitHub Desktop.
Sync currently-playing mpd track to Slack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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