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
  • 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
<?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>
</dict>
</plist>
{
"defined": {
"emoji": "",
"text": ""
},
"playing_emoji": ":headphones:"
}
#!/usr//bin/env bash
set -eu
APIKEY="From Here https://api.slack.com/custom-integrations/legacy-tokens"
DATA="$(dirname $0)"/data.json
function post_status() {
local payload="$(jq -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 -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 -r '.playing_emoji' ${DATA})"
local status=$(current_slack_status)
local emoji="$(echo "${status}" | jq -r '.emoji')"
local text="$(echo "${status}" | jq -r '.text')"
if [[ "${playing}" != "${emoji}" ]]; then
local new=$(jq -r --arg text "${text}" --arg emoji "${emoji}" '.defined.text = $text | .defined.emoji = $emoji' "${DATA}")
echo new data: "${new}"
echo "${new}" > "${DATA}"
fi
}
function main() {
date
echo "Spotify is "$(echo_state)". Current song is: '"$(echo_song)"'."
maybe_save_current_status
if [[ "$(echo_state)" != "playing" ]]; then
post_status "$(jq -r '.defined.emoji' ${DATA})" "$(jq -r '.defined.text' ${DATA})"
else
post_status "$(jq -r '.playing_emoji' ${DATA})" "$(echo_song)"
fi
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment