#!/bin/sh
#
# Copyright (C) 2020 Matteo Savatteri and Matteo Zeccoli Marazzini.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
# Global Variables
WEBSITE="https://vaporwave.ivan.moe/list/"
###############################################################################
# USEFUL FUNCTIONS #
###############################################################################
# Download the html list of artists and urls
# Arguments: $1 = music library website
# Write to standard output a file made like this:
# "[url]\n[artist]\noff\n"... (repeat for each artist)
# The "off" at the end is needed by dialog.
get_library()
{
wget -qO- "$1" 2>/dev/null \
| grep "^/\n/' \
-e 's/><\/a>.*/\noff/' \
-e 's/\/<\/a>.*/\noff/'
}
# Let the user select the artists he likes using a dialog checklist
# Arguments: $1 = music library website
# Write to standard output a '\n' separated list of the url of the selected artists.
# If the user selects cancel or presses ESC, send a SIGTERM to the script and all its subprocesses.
select_artist_list()
{
{ xargs -d '\n' dialog --title "VAPORGET" --no-tags --checklist "Please, select an artist:" 0 0 0 3>&1 1>&2 2>&3 3>&- || kill -TERM -$$; } \
| sed -e 's/ /\n/g' -e '$a\' \
| sed -e 's,^,'"$1"','
}
# Prepare a list of song urls
# Arguments: $1 = list of selected artist urls
# Write to standard output and error the urls of all the songs by the selected artists.
prepare_playlist()
{
wget --spider -r -l inf --no-parent --no-directories $1 2>&1 \
| grep --line-buffered '^--' \
| stdbuf -oL cut -d' ' -f4 \
| grep --line-buffered '\.\(flac\|mp3\|wav\)$' \
| tee /dev/stderr
}
# Plays the playlist it recieves via standard input
play_music()
{
mpv --no-video --playlist=- $MPV_CMDLINE
}
# TERM signal handler
handle_term()
{
clear
exit 0
}
###############################################################################
# MAIN SECTION #
###############################################################################
# Parse command line
while getopts m: f; do
case $f in
m)
MPV_CMDLINE=$(echo "${OPTARG}" | sed -e 's/^ */--/' -e 's/, */ --/g')
;;
esac
done
shift `expr $OPTIND - 1`
# Use our custom handler for the TERM signal
trap handle_term TERM
# Fetch the artist list from the website and let the user select from a menu
CHOICES=$(get_library "${WEBSITE}" | select_artist_list "${WEBSITE}")
clear
if [ -z "${CHOICES}" ]; then # User didn't select any artist
echo "Please, select at least one artist." >&2
exit 1
fi
# Play the music
prepare_playlist "${CHOICES}" | play_music