From 9623579cb6e231805ca9ec9c245955a926e01cd8 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Mon, 29 May 2023 23:05:43 +0000 Subject: feat: initial release --- src/lib/chapter_selection.sh | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/lib/chapter_selection.sh (limited to 'src/lib/chapter_selection.sh') diff --git a/src/lib/chapter_selection.sh b/src/lib/chapter_selection.sh new file mode 100644 index 0000000..c3534a5 --- /dev/null +++ b/src/lib/chapter_selection.sh @@ -0,0 +1,38 @@ +chapter_selection() { + # Novel page HTML + novel_page="$(gum spin --spinner line --show-output -- \ + curl --silent --get --data-urlencode "k=${args[title]}" "${1}")" + + # Chapter data + IFS=$'\n' read -r -d '\n' -a chapter_locations <<<"$(echo "${novel_page}" | + pup 'div.list-chapter a.list-group-item attr{href}')" || true + IFS=$'\n' read -r -d '\n' -a chapter_names <<<"$(echo "${novel_page}" | + pup 'div.list-chapter span.chapter-name text{}')" || true + + # Chapter collection for Gum + chapters="" + + # Fix output format for Gum + for ((i = 0; i < "${#chapter_locations[@]}"; i++)); do + chapters+="$(printf "\nChapter ${i}:%s" "${chapter_names[${i}]}")" + done + + # Remove leading new-line + chapters="$(echo "${chapters}" | sed '/./,$!d')" + + # Prompt for user's novel choice + chapter_choice="$(echo "${chapters}" | gum filter)" + + # Remove chapter discriminator from chapter choice + chapter_choice="${chapter_choice#Chapter [0-9]*: }" + + # Locate chapter choice location + for ((i = 0; i < "${#chapter_locations[@]}"; i++)); do + if [[ "${chapter_names[${i}]}" = " ${chapter_choice}" ]]; then + chapter_location="https://www.honeyfeed.fm${chapter_locations[${i}]}" + fi + done + + # Return chapter location + echo "${chapter_location}" +} -- cgit v1.2.3