Skip to content

Instantly share code, notes, and snippets.

@bloodf
Last active May 1, 2020 15:55
Show Gist options
  • Save bloodf/983895d2a5066f4753803c706b5af9bf to your computer and use it in GitHub Desktop.
Save bloodf/983895d2a5066f4753803c706b5af9bf to your computer and use it in GitHub Desktop.
Share Spotify Song on Slack
#!/bin/bash
# From Here https://api.slack.com/custom-integrations/legacy-tokens
SLACKTOKEN=""
trap onexit INT
function reset() {
echo 'Resetting Slack Status'
curl -s -d "payload=$json" "https://slack.com/api/users.profile.set?token="$SLACKTOKEN"&profile=%7B%22status_text%22%3A%22%22%2C%22status_emoji%22%3A%22%22%7D" > /dev/null
}
function onexit() {
echo 'Exitting'
reset
exit
}
date
while true; do
state=$(osascript -e 'tell application "Spotify" to player state')
if [[ "$state" != "playing" ]]; then
if [[ "$PAUSE" = "0" ]]; then
reset
PAUSE=1
fi
else
PAUSE=0
SONG=$(osascript -e 'tell application "Spotify" to artist of current track & " - " & name of current track')
URLSONG=$(echo "$SONG" | perl -MURI::Escape -ne 'chomp;print uri_escape($_),"\n"')
if [[ "$OLDSONG" != "$SONG" ]]; then
if [[ -n "$SONG" ]]; then
echo "Spotify"
echo "$state: "$SONG
echo "Last Song: "$OLDSONG
echo '------'
OLDSONG=$SONG
echo 'Updating Slack Status'
curl -s -d "payload=$json" "https://slack.com/api/users.profile.set?token="$SLACKTOKEN"&profile=%7B%22status_text%22%3A%22"$URLSONG"%20%22%2C%22status_emoji%22%3A%22%3Aheadphones%3A%22%7D" > /dev/null
fi
fi
fi
sleep 1
done
@bloodf
Copy link
Author

bloodf commented Mar 26, 2020

  • perl -MCPAN -e shell
  • yes - (for the "...configure automatically" question)
  • install URI::Escape
  • ...wait for it to finish...
  • exit

@bloodf
Copy link
Author

bloodf commented Mar 26, 2020

You can have this run automatically on your mac using the native launchd system:

Creating the startup process

Drop the following PLIST into ~/Library/LaunchAgents/com.user.slack-spotify.plist:

The filename must match the string under Label, replace /PATH/TO/SCRIPT.sh with an actual path to the .sh script above.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.user.slack-spotify</string>
        <key>ProgramArguments</key>
        <array><string>/PATH/TO/shareSpotifySongOnSlack.sh</string></array>
        <key>RunAtLoad</key>
        <true/>
        <key>StartInterval</key>
        <integer>60</integer>
    </dict>
</plist>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment