Skip to content

Instantly share code, notes, and snippets.

@MacsInSpace
Last active July 20, 2021 01:02
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 MacsInSpace/2c46a2a6c44922fb5d8f65a8e122d0ba to your computer and use it in GitHub Desktop.
Save MacsInSpace/2c46a2a6c44922fb5d8f65a8e122d0ba to your computer and use it in GitHub Desktop.
Monitors Last.fm and plays last.fm "now playing" track on spotify-tui spt with spotifyd
#!/usr/bin/env zsh
# now playing
# requires the last.fm API key
export API_KEY="fghjfghfghfghfghjfghjfjgh"
# requires LastFM user to follow
USER="hairball1975"
URL="http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks"
URL+="&user=$USER&api_key=$API_KEY&format=json&limit=1&nowplaying=true"
########################################################
function levenshtein {
if (( $# != 2 )); then
echo "Usage: $0 word1 word2" >&2
elif (( ${#1} < ${#2} )); then
levenshtein "$2" "$1"
else
local str1len=${#1}
local str2len=${#2}
local d
for (( i = 0; i <= (str1len+1)*(str2len+1); i++ )); do
d[i]=0
done
for (( i = 0; i <= str1len; i++ )); do
d[i+0*str1len]=$i
done
for (( j = 0; j <= str2len; j++ )); do
d[0+j*(str1len+1)]=$j
done
for (( j = 1; j <= str2len; j++ )); do
for (( i = 1; i <= str1len; i++ )); do
[ "${1:i-1:1}" = "${2:j-1:1}" ] && local cost=0 || local cost=1
del=$(( d[(i-1)+str1len*j]+1 ))
ins=$(( d[i+str1len*(j-1)]+1 ))
alt=$(( d[(i-1)+str1len*(j-1)]+cost ))
d[i+str1len*j]=$( echo "$del\n$ins\n$alt" | sort -n | head -1 )
done
done
echo ${d[str1len+str1len*(str2len)]}
fi
}
########################################################
function pagecheck {
date
echo "checking page for now playing..."
RES=$(curl -s $URL)
NOWPLAYING=$(jq '.recenttracks.track[0]."@attr".nowplaying' <<< "$RES" | tr -d '"')
TRACK=$(jq '.recenttracks.track[0].name' <<< "$RES" | tr -d '"')
ARTIST=$(jq '.recenttracks.track[0].artist."#text"' <<< "$RES" | tr -d '"')
UNIXDATE=$(jq '.recenttracks.track[0].date."uts"' <<< "$RES" | tr -d '"')
if [[ "$NOWPLAYING" = "true" ]]
then
echo "calling now playing"
nowplaying
else
echo "calling not playing"
notplaying
fi
}
########################################################
function servicecheck {
echo "Checking spotifyd service..."
STATUS=$(brew services list | grep spotifyd | awk '{ print $2 }')
if [[ "$STATUS" != "started" ]]
then
brew services restart spotifyd
echo "spotifyd service is now running."
else
echo "spotifyd service already is running."
fi
}
########################################################
function nowplaying {
CURRENTPLAYINGLOCALTRACK=$(spt playback -f %t)
CURRENTPLAYINGLOCALARTIST=$(spt playback -f %a)
CURRENTTRACK=$(echo "$CURRENTPLAYINGLOCALTRACK""$CURRENTPLAYINGLOCALARTIST" | sed 's/[^a-zA-Z0-9]//g' | tr '[:upper:]' '[:lower:]')
CURRENTTRACK=${CURRENTTRACK:-default}
TRACK=$(jq '.recenttracks.track[0].name' <<< "$RES" | tr -d '"')
ARTIST=$(jq '.recenttracks.track[0].artist."#text"' <<< "$RES" | tr -d '"')
REMOTETRACK=$(echo "$TRACK""$ARTIST" | sed 's/[^a-zA-Z0-9]//g' | tr '[:upper:]' '[:lower:]')
echo CURRENTTRACK is $CURRENTTRACK
echo REMOTETRACK is $REMOTETRACK
if [[ $CURRENTTRACK = $REMOTETRACK ]] || [[ $CURRENTTRACK =~ .*$REMOTETRACK.* ]] || [[ $REMOTETRACK =~ .*$CURRENTTRACK.* ]]
then
LEV="1"
echo "this contains that levenshtein is $LEV"
else
echo "calculating levenshtein distance..."
LEV=$(levenshtein $REMOTETRACK $CURRENTTRACK)
echo "levenshtein is $LEV"
fi
if [[ "$LEV" -lt "4" ]] && [[ -n "$CURRENTTRACK" ]]
then
echo "still playing..."
sleep 15
elif [[ "$LEV" -gt "4" ]] || [[ -z "$LEV" ]]
then
echo "Current Locally playing track is $CURRENTPLAYINGLOCALTRACK by $CURRENTPLAYINGLOCALARTIST"
echo "Current Last.FM track is $TRACK by $ARTIST at $DATE"
echo "Do you wish to play this Last.FM track? [Timed default 'yes']"
timelimit=2
reply="y"
read -n1 -t $timelimit reply
#read -t $timelimit reply <&1
# for bash versions bellow 3.x
if [[ $reply = "N" ]] || [[ $reply = "n" ]] || [[ $reply = "q" ]] || [[ $reply = "Q" ]]
then
exit 0
elif [[ $reply = "Y" ]] || [[ $reply = "y" ]]
then
echo ""
echo "yes seleted. Playing last track..."
open https://www.last.fm/user/$USER
spt play --name "$ARTIST $TRACK" --track
spt playback --transfer spotifyd
else
echo ""
echo "continuing checking.."
fi
fi
pagecheck
}
#####################################################
function notplaying {
echo "Nothing Playing"
NOWPLAYING=$(jq '.recenttracks.track[0]."@attr".nowplaying' <<< "$RES" | tr -d '"')
TRACK=$(jq '.recenttracks.track[0].name' <<< "$RES" | tr -d '"')
ARTIST=$(jq '.recenttracks.track[0].artist."#text"' <<< "$RES" | tr -d '"')
UNIXDATE=$(jq '.recenttracks.track[0].date."uts"' <<< "$RES" | tr -d '"')
DATE=$(date -r $UNIXDATE)
echo "Previous was $TRACK by $ARTIST at $DATE"
timelimit=30
echo "Do you wish to play this track? [Timed default 'no']"
reply=""
read -n1 -t $timelimit reply
#read -t $timelimit reply <&1
# for bash versions bellow 3.x
if [[ $reply = "Y" ]] || [[ $reply = "y" ]]
then
echo ""
echo "yes seleted. Playing last track..."
spt playback --transfer spotifyd
spt play --name "$ARTIST $TRACK" --track
elif [[ $reply = "N" ]] || [[ $reply = "n" ]] || [[ $reply = "q" ]] || [[ $reply = "Q" ]]
then
exit 0
else
echo ""
echo "continuing checking.."
fi
pagecheck
}
#####################################################3
#servicecheck
brew services restart spotifyd
sleep 2
pagecheck
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment