Skip to content

Instantly share code, notes, and snippets.

@bonelifer
Created January 10, 2025 20:44
Show Gist options
  • Save bonelifer/18dfc17df599b933fc3ef8a45efc7538 to your computer and use it in GitHub Desktop.
Save bonelifer/18dfc17df599b933fc3ef8a45efc7538 to your computer and use it in GitHub Desktop.
Script description: Fetches similar artist tracks based on the currently playing track or all tracks in the queue.
#!/usr/bin/bash
# Script description: Fetches similar artist tracks based on the currently playing track or all tracks in the queue.
# Accepts arguments:
# -c, --current: Retrieves similar artist tracks for the currently playing track.
# -a, --all: Retrieves similar artist tracks for each track in the current playlist.
# Configurable parameters:
# lastfm_api_key: Last.fm API key (replace LAST_FM_API_KEY).
# similar_limit: Limit for similar artists fetched.
# per_artist_limit: Maximum tracks per similar artist.
# current_total_limit: Total tracks to add to the playlist for -c/--current.
# per_track_limit: Total tracks per individual track in the queue for -a/--all.
# Default values
lastfm_api_key="LastFMAPIKey" # Replace with your Last.fm API key
similar_limit=20
per_artist_limit=5
current_total_limit=20
per_track_limit=10
# Required applications
required_apps=("mpc" "curl" "jq")
# Function to check and install required applications
check_and_install_apps() {
for app in "${required_apps[@]}"; do
if ! command -v "$app" &>/dev/null; then
echo "Required application '$app' is not installed. Installing..."
sudo apt update && sudo apt install -y "$app"
if [[ $? -ne 0 ]]; then
echo "Failed to install '$app'. Please install it manually."
exit 1
fi
fi
done
}
# Run the application check
check_and_install_apps
# Display usage help
display_help() {
echo "Usage: $0 [OPTIONS]"
echo
echo "Options:"
echo " -c, --current Retrieves similar artist tracks for the currently playing track."
echo " -a, --all Retrieves similar artist tracks for each track in the current playlist."
echo
echo "If no options are provided, this help message will be displayed."
exit 0
}
# Parse script arguments
if [[ $# -eq 0 ]]; then
display_help
fi
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-c|--current)
current_track=true
shift
;;
-a|--all)
all_tracks=true
shift
;;
*)
echo "Unknown option: $1"
display_help
;;
esac
done
if [[ $current_track == true ]]; then
artist=$(mpc current -f "%artist%")
if [[ -z $artist ]]; then
echo "Not playing..."
exit 1
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 $current_total_limit | while read track; do
mpc -q add "$track"
done
fi
if [[ $all_tracks == true ]]; then
# Capture the original queue at the start
original_queue=$(mpc playlist -f "%artist% - %title%\n")
# Iterate only over the original queue
while IFS= read -r line; do
artist=$(echo "$line" | cut -d'-' -f1 | sed 's/ *$//')
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 similar_artist; do
mpc -q search artist "$similar_artist" | shuf -n $per_artist_limit
done | shuf -n $per_track_limit | while read track; do
mpc -q add "$track"
done
done <<< "$original_queue"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment