Skip to content

Instantly share code, notes, and snippets.

@cookandy
Last active March 3, 2022 17:02
Show Gist options
  • Save cookandy/636a3f00768bf1e3a0a8b18630aac62c to your computer and use it in GitHub Desktop.
Save cookandy/636a3f00768bf1e3a0a8b18630aac62c to your computer and use it in GitHub Desktop.
last.fm to Slack
#!/bin/bash
# this script updates your slack status with the current playing track from last.fm
# requires URI::Escape perl module (cpan URI::Escape) and jq
# make sure to update slack_api_key, last_fm_api_key, and last_fm_username
#########################################
##### to run this script at startup #####
#########################################
# save this script to /Users/<your_user>/Library/scripts/lastfm-to-slack.sh
# create a com.user.lastfmToSlack.plist file containing the following xml
# <?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.lastfmToSlack</string>
# <key>ProgramArguments</key>
# <array>
# <string>/Users/<your_user>/Library/scripts/lastfm-to-slack.sh</string>
# </array>
# <key>RunAtLoad</key>
# <true/>
# </dict>
# </plist>
# save the file in ~/Library/LaunchAgents
# run this command: launchctl load ~/Library/LaunchAgents/com.user.lastfmToSlack.plist
#####################
##### variables #####
#####################
# slack api key
slack_api_key="xxxxxxxxxxxxxxx"
# last.fm api key
last_fm_api_key="xxxxxxxxxxxxxxx"
# last.fm username
last_fm_username="xxxxxxxxxxxxxxx"
# enable logging to file
debug="false"
# if debug is enabled, the following log file will be used
logfile="$(dirname "$0")/lastfm-to-slack.log"
# how often to check last.fm for a new track (in sec)
poll_for_new_song="10"
# emoji to use when playing (without surrounding colons)
playing_emoji="headphones"
############################
##### start the script #####
############################
# create a simple logging function
function log(){
if [ $debug = true ]; then
echo "$(date) $1" >> "$logfile"
fi
}
# clear the log on startup
true > "$logfile"
# start the script in an infinite loop
while true
do
# reset update_slack var
update_slack=""
# set current time
now=$(date +%s)
# check to see if it's safe to update slack status (only update if no emoji or using our custom 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 [[ "$current_status_emoji" == "" || "$current_status_emoji" == "$playing_emoji" ]]; then
log "it's safe to update slack status. proceeding"
update_slack="true"
else
log "it's not safe to update slack status"
fi
# if slack can be updated, go ahead and call lastfm and update it
if [ $update_slack == "true" ]; then
# get the raw json from last.fm
raw_json=$(curl -s "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=$last_fm_username&api_key=$last_fm_api_key&format=json&limit=1&from=$now")
log "raw json: $raw_json"
# see if there's a valid response
if ! echo "$raw_json" | /usr/local/bin/jq '.recenttracks.track.artist' >/dev/null 2>&1 ; then
# nothing playing
log "nothing playing."
if [ "$now_playing" != "none" ]; then
log "clearing slack status"
# clear 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
encoded_track="none"
else
log "no need to update slack"
fi
else
# get artist
artist=$(echo "$raw_json" | /usr/local/bin/jq -r '.recenttracks.track.artist."#text"')
# get album
track=$(echo "$raw_json" | /usr/local/bin/jq -r '.recenttracks.track.name')
# convert it to a url
encoded_track=$(echo "$artist - $track" | perl -MURI::Escape -ne 'chomp;print uri_escape($_),"\n"')
log "artist: $artist"
log "track: $track"
log "encoded_track: $encoded_track"
# update slack status
if [ "$now_playing" != "$encoded_track" ]; then
if [[ "$track" != "" && "$track" != "null" ]]; then
log "updating slack with new track info"
curl -s -m 2 "https://slack.com/api/users.profile.set?token=$slack_api_key&profile=%7B%22status_text%22%3A%22$encoded_track%22%2C%22status_emoji%22%3A%22%3A$playing_emoji%3A%22%7D" > /dev/null
fi
else
log "the same song is playing from the last check. not updating slack"
fi
fi
fi
# set now playing to current song
now_playing="$encoded_track"
# sleep
log "sleeping for $poll_for_new_song seconds"
sleep $poll_for_new_song
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment