69 lines
1.3 KiB
Bash
69 lines
1.3 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
PLAYLIST="$1"
|
|
COUNT="$2"
|
|
|
|
set -e
|
|
|
|
. "$HOME/.local/share/music/common.sh"
|
|
|
|
PLAYLIST_DIR="$MUSIC_DIR/Playlists"
|
|
|
|
function rand_num() {
|
|
cat /dev/urandom | tr -dc '0-9' | head -c 10 | sed -e 's/^0*//'
|
|
}
|
|
|
|
function pick_song() {
|
|
playlist="$1"
|
|
|
|
playlist_path="$PLAYLIST_DIR/$playlist.pls"
|
|
if [[ ! -f "$playlist_path" ]]
|
|
then
|
|
echo "ERROR: playlist $playlist not found" >&2
|
|
exit 1
|
|
fi
|
|
|
|
songs="$(cat "$playlist_path" | grep '^file' | sed -e 's|^[^=]*=\.\./||')"
|
|
|
|
num=$(( 1 + $(rand_num) % $(echo "$songs" | wc -l) ))
|
|
song="$(echo "$songs" | sed "${num}q;d")"
|
|
echo "$MUSIC_DIR/$song"
|
|
}
|
|
|
|
function shuffle() {
|
|
playlist="$1"
|
|
|
|
song="$(pick_song "$playlist")"
|
|
song="$(music_get_song_info_from_file "$song")"
|
|
song="$(music_process_genre "$song")"
|
|
like=$(echo "$song" | jq '.likeLevel')
|
|
mpd_path="$(echo "$song" | jq -rc '.path.mpd')"
|
|
like_min=$(( -1 + $(rand_num) % 3 ))
|
|
|
|
if [[ $like > $like_min ]]
|
|
then
|
|
echo "$like $mpd_path"
|
|
mpc add "$mpd_path"
|
|
else
|
|
shuffle "$playlist"
|
|
fi
|
|
}
|
|
|
|
if [[ -z "$PLAYLIST" ]]
|
|
then
|
|
PLAYLIST=all
|
|
fi
|
|
|
|
if [[ -z "$COUNT" ]]
|
|
then
|
|
echo "add one song to que from $PLAYLIST"
|
|
echo "$(shuffle $PLAYLIST)"
|
|
else
|
|
echo "add $COUNT songs to que from $PLAYLIST"
|
|
for i in $(seq 1 $COUNT)
|
|
do
|
|
echo "$(shuffle $PLAYLIST)"
|
|
# echo $i
|
|
done
|
|
fi
|