Last active
June 1, 2023 20:44
-
-
Save jesvs/bd0d00e736503fb3385f799dad1aeaac to your computer and use it in GitHub Desktop.
Shell script that uses last.fm API to add similar artists to MPD.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# depends on jq to parse json | |
# https://stedolan.github.io/jq/ | |
# | |
# This script will query the last.fm API for | |
# similar artists based on the currently playing track. | |
# It will crop the current playlist, and shuffle the API results. | |
# | |
# You can configure 3 parameters: | |
# similar_limit Sets the total similar artists to grab, a high number | |
# yields more diverse artists. | |
# per_artist_limit Limits how many songs per artist to consider. | |
# total_limit How many songs to add to the final playlist. | |
lastfm_api_key=LAST_FM_API_KEY # https://www.last.fm/api/account/create | |
similar_limit=20 # a higher number gives less similar artists | |
per_artist_limit=5 # how many tracks per artist to consider | |
total_limit=20 # how many tracks to add | |
artist=$(mpc current -f "%artist%") | |
if [[ -z $artist ]]; then | |
echo Not playing… | |
exit | |
fi | |
mpc crop | |
curl --silent --get \ | |
--data-urlencode "api_key=${lastfm_api_key}" \ | |
--data-urlencode "format=json" \ | |
--data-urlencode "limit=${similar_limit}" \ | |
--data-urlencode "method=artist.getsimilar" \ | |
--data-urlencode "artist=${artist}" \ | |
"http://ws.audioscrobbler.com/2.0" | | |
jq --raw-output '.similarartists.artist[]?.name' | sed "1 i\\${artist}" | while read line; do | |
mpc -q search artist "$line" | shuf -n $per_artist_limit | |
done | shuf -n $total_limit | while read track; do | |
mpc -q add "$track" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have tried the script and it works. Make sure that mpc is connected to mpd.
Just run
mpc current -f "%artist%"
in shell to make sure. If you getMPD error: Connection refused
you have to find out if mpd is reachable as socket or ip. change every line where mpc in use with 'mpc -h $MPDSOCKET or $IP'