Skip to content

Instantly share code, notes, and snippets.

@rodolfo42
Last active January 19, 2018 09:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodolfo42/c4a04cb5757ffb4bac1a02db54a5fa07 to your computer and use it in GitHub Desktop.
Save rodolfo42/c4a04cb5757ffb4bac1a02db54a5fa07 to your computer and use it in GitHub Desktop.
Post current playing Spotify song to a txt online (MacOS)

How to use it

$ curl -s https://gist.githubusercontent.com/rodolfo42/c4a04cb5757ffb4bac1a02db54a5fa07/raw/f90e69d91f01f4096d7dc4556f810a3853efde27/spotifynowplaying.sh > ~/spotifynowplaying.sh

Change the URL

Change URL to some dontpad.com url (it's a simple persistent textarea in the cloud)

URL=http://dontpad.com/some-url
$ chmod ugo+x ~/spotifynowplaying.sh
$ ~/spotifynowplaying.sh

To run in the background

$ ~/spotifynowplaying.sh > /dev/null &
#!/bin/bash
URL=http://dontpad.com/some-url
last_song=""
get_current_song() {
# TODO Linux magic if the OS is Linux
SONG_NAME=$(osascript -e 'tell application "Spotify" to name of current track as string')
SONG_ARTIST=$(osascript -e 'tell application "Spotify" to artist of current track as string')
SONG_ALBUM=$(osascript -e 'tell application "Spotify" to album of current track as string')
echo -n "Song: $SONG_NAME | Artist: $SONG_ARTIST | Album: $SONG_ALBUM"
}
append_song() {
SONGTEXT=$1
TEXT=$(echo -e "$(date) | $SONGTEXT"'\n'"$(curl -s $URL.txt)")
curl -o /dev/null -s -X POST $URL --data-urlencode "text=$TEXT"
}
while true; do
SONG_STATUS=$(get_current_song)
# echo "$(date) now playing: $SONG_STATUS"
if [[ "$SONG_STATUS" != "$last_song" ]]; then
last_song=$SONG_STATUS
echo -e "$(date) changed now playing status to: $SONG_STATUS"
append_song "$SONG_STATUS"
echo "$(date) $SONG_STATUS" >> ~/spotify_status_history.txt
echo "$(date) status changed. polling..."
fi
sleep 3
done
@rodolfo42
Copy link
Author

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