Skip to content

Instantly share code, notes, and snippets.

@cookandy
Last active February 15, 2019 00:29
Show Gist options
  • Save cookandy/a0ec054bf1d9cfb7be4293a5381deb27 to your computer and use it in GitHub Desktop.
Save cookandy/a0ec054bf1d9cfb7be4293a5381deb27 to your computer and use it in GitHub Desktop.
Spotfiy to Slack
#!/bin/bash
# this script updates your slack status with the current playing track in spotify in OS X
#########################################
##### to run this script at startup #####
#########################################
# create a com.user.spotifyToSlack.plist file containing the following xml
# make sure to update the /path/to/your/spotify-to-slack.sh in the file
# also update the xoxp-xxxxx to your real slack token
# <?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.spotifyToSlack</string>
# <key>ProgramArguments</key>
# <array>
# <string>/path/to/your/spotify-to-slack.sh</string>
# <string>xoxp-xxxxxxxxxxxx</string>
# </array>
# <key>RunAtLoad</key>
# <true/>
# </dict>
# </plist>
# save the file in ~/Library/LaunchAgents
# run this command: launchctl load ~/Library/LaunchAgents/com.user.spotifyToSlack.plist
[[ -z "$1" ]] && echo "no api key set. exiting" && echo "usage: $0 xoxp-xxxxxxxxxxxx" && exit 1
#####################
##### variables #####
#####################
# slack api key is provided when calling the script
slack_api_key="$1"
# enable logging to file
debug="false"
# if debug is enabled, the following log file will be used
logfile="`dirname "$0"`/spotify-to-slack.log"
# how often to check spotify for a new track (in sec), when spotify is running
poll_for_new_song="15"
# how often to check to see if spotify is running (in sec), when spotify is closed
poll_for_spotify_running="30"
# emoji to use when playing (without surrounding colons)
playing_emoji="plexamp-outline"
############################
##### start the script #####
############################
# create a simple logging function
function log(){
if [ $debug = true ]; then
echo "$(date) $1" >> $logfile
fi
}
# clear the log on startup
> $logfile
# start the script in an infinite loop
while true
do
# see if spotify is running
if [ $(osascript -e 'tell application "System Events" to (name of processes) contains "Spotify"') = true ]; then
log "spotify is running! getting status"
# get status of spotify (play/paused)
playing=$(osascript -e 'tell application "Spotify" to set playState to (player state as text)')
log "spotify status is $playing"
# get current status emoji
current_status_emoji=$(curl -C - -m 2 -s "https://slack.com/api/users.profile.get?token=$slack_api_key" | /usr/local/bin/jq -r .profile.status_emoji | sed 's/://g')
log "current emoji: $current_status_emoji"
if [ $playing == "playing" ]; then
# get artist and song
artist=$(osascript -e 'tell application "Spotify" to set currentArtist to artist of current track as string')
song=$(osascript -e 'tell application "Spotify" to set currentTrack to name of current track as string')
# convert it to a URL
url_song=$(echo "$artist - $song" | perl -MURI::Escape -ne 'chomp;print uri_escape($_),"\n"')
log "artist: $artist"
log "song: $song"
log "checking to see if $url_song is the same as $current_song"
# check to see if the current playing song is the same as the last time we checked
if [[ "$url_song" == "$current_song" && "$current_status_emoji" != "" ]]; then
log "same song is playing. no need to update slack status"
elif [[ "$current_status_emoji" != "" && "$current_status_emoji" != "$playing_emoji" ]]; then
log "slack status set by user, skipping update"
else
# if it's a new song, update slack with status
log "updating slack status with artist and song"
curl -s -m 2 "https://slack.com/api/users.profile.set?token="$slack_api_key"&profile=%7B%22status_text%22%3A%22"$url_song"%22%2C%22status_emoji%22%3A%22%3A$playing_emoji%3A%22%7D" > /dev/null
fi
# this is when spotify isn't playing
else
# if it's set to plexamp, we need to clear it
if [[ "$current_status_emoji" == "$playing_emoji" ]]; then
log "no track playing and emoji set to $playing_emoji. clearing slack status"
curl -s -m 2 "https://slack.com/api/users.profile.set?token="$slack_api_key"&profile=%7B%22status_text%22%3A%22""%22%2C%22status_emoji%22%3A%22%22%7D" > /dev/null
else
# otherwise, don't do anything
log "no track playing and emoji not set to $playing_emoji - not doing anything"
fi
fi
# check every xx seconds for a new track
log "sleeping for $poll_for_new_song seconds before getting playing track again..."
# if the last status was playing, set the track name so we can check for it again in the future
if [ $playing == "playing" ]; then
log "setting current_song = $url_song"
current_song="$url_song"
else
# othewise, clear it so when we start spotify again we'll get an updated status
current_song=""
log "setting current_song = EMPTY"
fi
# clear playing status since we'll get it again soon
playing=""
sleep $poll_for_new_song
# this is when spotify isn't even running
else
# sleep xx seconds before checking if spotify is running again
log "spotify not running, waiting $poll_for_spotify_running seconds before checking again..."
sleep $poll_for_spotify_running
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment