vaporget 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2020 Matteo Savatteri and Matteo Zeccoli Marazzini.
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. # Global Variables
  18. WEBSITE="https://vaporwave.ivan.moe/list/"
  19. ###############################################################################
  20. # USEFUL FUNCTIONS #
  21. ###############################################################################
  22. # Download the html list of artists and urls
  23. # Arguments: $1 = music library website
  24. # Write to standard output a file made like this:
  25. # "[url]\n[artist]\noff\n"... (repeat for each artist)
  26. # The "off" at the end is needed by dialog.
  27. get_library()
  28. {
  29. wget -qO- "$1" 2>/dev/null \
  30. | grep "^<a href" \
  31. | sed -e 's/<a href="//' \
  32. -e 's/">/\n/' \
  33. -e 's/&gt;<\/a>.*/\noff/' \
  34. -e 's/\/<\/a>.*/\noff/'
  35. }
  36. # Let the user select the artists he likes using a dialog checklist
  37. # Arguments: $1 = music library website
  38. # Write to standard output a '\n' separated list of the url of the selected artists.
  39. # If the user selects cancel or presses ESC, send a SIGTERM to the script and all its subprocesses.
  40. select_artist_list()
  41. {
  42. { 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 -$$; } \
  43. | sed -e 's/ /\n/g' -e '$a\' \
  44. | sed -e 's,^,'"$1"','
  45. }
  46. # Prepare a list of song urls
  47. # Arguments: $1 = list of selected artist urls
  48. # Write to standard output and error the urls of all the songs by the selected artists.
  49. prepare_playlist()
  50. {
  51. wget --spider -r -l inf --no-parent --no-directories $1 2>&1 \
  52. | grep --line-buffered '^--' \
  53. | stdbuf -oL cut -d' ' -f4 \
  54. | grep --line-buffered '\.\(flac\|mp3\|wav\)$' \
  55. | tee /dev/stderr
  56. }
  57. # Plays the playlist it recieves via standard input
  58. play_music()
  59. {
  60. mpv --no-video --playlist=- $MPV_CMDLINE
  61. }
  62. # TERM signal handler
  63. handle_term()
  64. {
  65. clear
  66. exit 0
  67. }
  68. ###############################################################################
  69. # MAIN SECTION #
  70. ###############################################################################
  71. # Parse command line
  72. while getopts m: f; do
  73. case $f in
  74. m)
  75. MPV_CMDLINE=$(echo "${OPTARG}" | sed -e 's/^ */--/' -e 's/, */ --/g')
  76. ;;
  77. esac
  78. done
  79. shift `expr $OPTIND - 1`
  80. # Use our custom handler for the TERM signal
  81. trap handle_term TERM
  82. # Fetch the artist list from the website and let the user select from a menu
  83. CHOICES=$(get_library "${WEBSITE}" | select_artist_list "${WEBSITE}")
  84. clear
  85. if [ -z "${CHOICES}" ]; then # User didn't select any artist
  86. echo "Please, select at least one artist." >&2
  87. exit 1
  88. fi
  89. # Play the music
  90. prepare_playlist "${CHOICES}" | play_music