Browse Source

Create functions to better organize code

Since the test for the cancel button is now executed in a subprocess,
instead of calling exit send a TERM signal to the process group. The
script has a signal handler.
Matteo Zeccoli Marazzini 4 years ago
parent
commit
3461d6c510
1 changed files with 62 additions and 15 deletions
  1. 62 15
      vaporget

+ 62 - 15
vaporget

@@ -15,21 +15,75 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
+
 WEBSITE="https://vaporwave.ivan.moe/list/"
 
-# Fetch the artist list from the website and let the user select from a menu
-CHOICES=$(wget -qO- $WEBSITE 2>/dev/null \
+
+###############################################################################
+# 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 "^<a href" \
 	| sed -e 's/<a href="//' \
 	      -e 's/">/\n/' \
 	      -e 's/&gt;<\/a>.*/\noff/' \
-	      -e 's/\/<\/a>.*/\noff/' \
-	| xargs -d '\n' dialog --title "VAPORGET" --checklist "Please, select an artist:" 0 0 0 3>&1 1>&2 2>&3 3>&-)
+	      -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" --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=- "$@"
+}
 
-if [ $? != 0 ]; then	# User selected cancel, therefore we exit
+# TERM signal handler
+handle_term()
+{
 	clear
 	exit 0
-fi
+}
+
+
+
+###############################################################################
+# MAIN SECTION                                                                #
+###############################################################################
+
+trap handle_term TERM	# Use our custom handler for the TERM signal
+
+# 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
@@ -37,12 +91,5 @@ if [ -z "${CHOICES}" ]; then	# User didn't select any artist
 	exit 1
 fi
 
-echo "${CHOICES}" \
-	| sed -e 's/ /\n/g' -e '$a\' \
-	| sed -e 's,^,'"${WEBSITE}"',' \
-	| wget -i - --spider -r -l inf --no-parent --no-directories 2>&1 \
-	| grep --line-buffered '^--' \
-	| stdbuf -oL cut -d' ' -f4 \
-	| grep --line-buffered '\.\(flac\|mp3\|wav\)$' \
-	| tee /dev/stderr \
-	| mpv --no-video --playlist=- "$@"
+# Play the music
+prepare_playlist "${CHOICES}" | play_music