Skip to content

Instantly share code, notes, and snippets.

@olebedev
Forked from jgamblin/slackspotify.sh
Last active April 19, 2020 22:38
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 olebedev/298b456897d04ec85b683b88f11ff820 to your computer and use it in GitHub Desktop.
Save olebedev/298b456897d04ec85b683b88f11ff820 to your computer and use it in GitHub Desktop.
A Script To Set Current Spotify Song As Slack Status

Usage

  1. Get API key here and put it into ./slackspotify.sh as an APIKEY value.

  2. Copy the com.user.slack-spotify.plist file into ~/Library/LaunchAgents/com.user.slack-spotify.plist

$ cp com.user.slack-spotify.plist ~/Library/LaunchAgents/com.user.slack-spotify.plist
  1. Modify ~/Library/LaunchAgents/com.user.slack-spotify.plist accordinally -- set path to your ./slackspotify.sh script.

  2. Register this daemon with launchd by running

$ launchctl load ~/Library/LaunchAgents/com.user.slack-spotify.plist
  1. Start it by either logging out and back in or running
$ launchctl start com.user.slack-spotify
  1. (Optional) Modify your 'playing' emoji in ./data.json.
<?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/SCRIPT.sh</string></array>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>60</integer>
<!-- <key>StandardOutPath</key>
<string>/PATH/TO/slackspotify-output.log</string>
<key>StandardErrorPath</key>
<string>/PATH/TO//slackspotify-errors.log</string> -->
</dict>
</plist>
{
"defined": {
"text": "",
"emoji": ":finn:"
},
"playing_emoji": ":spotify:"
}
#!/usr//bin/env bash
set -eu
# Get your api key here https://api.slack.com/custom-integrations/legacy-tokens and puth it into .apikey file.
SCRIPT_DIR="$(dirname $0)"
APIKEY=$(cat "${SCRIPT_DIR}"/.apikey)
DATA="${SCRIPT_DIR}"/data.json
JQ_BIN=/usr/local/bin/jq
function post_status() {
local payload="$(${JQ_BIN} -n -r --arg emoji "${1}" --arg text "${2}" '{"profile":{"status_text":$text,"status_emoji":$emoji}}')"
curl -s \
-H "Content-type: application/json; charset=utf-8" \
-H "Authorization: Bearer ${APIKEY}" \
-d "${payload}" \
https://slack.com/api/users.profile.set >/dev/null
}
function current_slack_status() {
curl -s "https://slack.com/api/users.profile.get?token=${APIKEY}" | ${JQ_BIN} -r '{ text: .profile.status_text, emoji: .profile.status_emoji }'
}
function echo_state() {
osascript -e 'tell application "Spotify" to player state'
}
function echo_song() {
osascript -e 'tell application "Spotify" to artist of current track & " - " & name of current track'
}
function maybe_save_current_status() {
local playing="$(${JQ_BIN} -r '.playing_emoji' ${DATA})"
local status=$(current_slack_status)
local emoji="$(echo "${status}" | ${JQ_BIN} -r '.emoji')"
local text="$(echo "${status}" | ${JQ_BIN} -r '.text')"
if [[ "${playing}" != "${emoji}" ]]; then
local new=$(${JQ_BIN} -n -r --arg text "${text}" --arg emoji "${emoji}" --arg playing "${playing}" '.defined.text = $text | .defined.emoji = $emoji | .playing_emoji = $playing')
echo "${new}" > "${DATA}"
fi
}
is_spotify_running() {
ps aux | grep -v grep | grep -v slackspotify | grep -c spotify >/dev/null
}
function main() {
date
maybe_save_current_status
if ! is_spotify_running; then
echo "Spotify is not running on this machine."
exit 0
else
echo "Spotify is "$(echo_state)"."
fi
if [[ "$(echo_state)" != "playing" ]]; then
post_status "$(${JQ_BIN} -r '.defined.emoji //empty' ${DATA})" "$(${JQ_BIN} -r '.defined.text //empty' ${DATA})"
else
post_status "$(${JQ_BIN} -r '.playing_emoji //empty' ${DATA})" "$(echo_song)"
fi
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment