|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# commands used by this script |
| 4 | +DEPS=("grep" \ |
| 5 | + "sed" \ |
| 6 | + "curl" \ |
| 7 | + "perl" \ |
| 8 | + "youtube-dl") |
| 9 | + |
| 10 | +# website to scrape listings from |
| 11 | +DOMAIN="https://www.khanacademy.org" |
| 12 | + |
| 13 | +# check for the existence of dependencies |
| 14 | +check_commands() { |
| 15 | + for COMMAND in "$@"; do |
| 16 | + if ! command -v "$COMMAND" > /dev/null 2>&1; then |
| 17 | + echo "command not found: $COMMAND" |
| 18 | + exit 1 |
| 19 | + fi |
| 20 | + done |
| 21 | +} |
| 22 | + |
| 23 | +# show a general-purpose menu with numbered entries |
| 24 | +menu_entry() { |
| 25 | + for i in $(seq 1 $#); do |
| 26 | + # parse html escapes, such as '"' and '&' as '"' and '&' respectively |
| 27 | + echo " [$i]" "$(echo "${!i}" | perl -MHTML::Entities -pe 'decode_entities($_);')" |
| 28 | + done |
| 29 | + |
| 30 | + read -p "Enter number to select: " SELECTION |
| 31 | + SELECTION=$(( $SELECTION - 1 )) |
| 32 | +} |
| 33 | + |
| 34 | +# apply regex patterns to extract the list of topics |
| 35 | +extract_topics() { |
| 36 | + local IFS=$'\n' |
| 37 | + |
| 38 | + REGEX_MATCH=$(curl -s "$DOMAIN" | grep -Po '>Courses</h3><ul>.*?</ul>' | grep -Po 'href="/[^/]*?"[^>]*?>[^<]+?<') |
| 39 | + TOPIC_TITLES=( $(echo "$REGEX_MATCH" | grep -Po '>[^<]+?<' | sed 's/[><]//g') ) |
| 40 | + TOPICS=( $(echo "$REGEX_MATCH" | grep -Po 'href=".*?"' | sed 's/href="\///;s/"//') ) |
| 41 | +} |
| 42 | + |
| 43 | +# apply regex patterns to extract the list of subtopics |
| 44 | +extract_subtopics() { |
| 45 | + local IFS=$'\n' |
| 46 | + TOPIC="$1" |
| 47 | + |
| 48 | + REGEX_MATCH=$(curl -s "$DOMAIN/$TOPIC" | grep -Po 'href="/'"$TOPIC"'/[^/"]*?">[^<]+?<') |
| 49 | + SUBTOPIC_TITLES=( $(echo "$REGEX_MATCH" | grep -Po '>[^<]+?<' | sed 's/[><]//g') ) |
| 50 | + SUBTOPICS=( $(echo "$REGEX_MATCH" | grep -Po 'href=".*?"' | sed 's/href="\///;s/"//') ) |
| 51 | +} |
| 52 | + |
| 53 | +# apply regex patterns to extract the list of chapters |
| 54 | +extract_chapters() { |
| 55 | + local IFS=$'\n' |
| 56 | + SUBTOPIC="$1" |
| 57 | + |
| 58 | + REGEX_MATCH=$(curl -s "$DOMAIN/$SUBTOPIC" | grep -Po 'href="/'"$SUBTOPIC"'/[^/#]*?"[^>]*?><h3.*?</h3>') |
| 59 | + CHAPTER_TITLES=( $(echo "$REGEX_MATCH" | grep -Po '>[^<]+?<' | sed 's/[><]//g') ) |
| 60 | + CHAPTERS=( $(echo "$REGEX_MATCH" | grep -Po 'href=".*?"' | sed 's/href="\///;s/"//') ) |
| 61 | +} |
| 62 | + |
| 63 | +# invoke youtube-dl to list the videos under the discovered playlist |
| 64 | +extract_videos() { |
| 65 | + local IFS=$'\n' |
| 66 | + CHAPTER="$1" |
| 67 | + |
| 68 | + VIDEO_TITLES=( $(youtube-dl --flat-playlist --get-title "$DOMAIN/$CHAPTER") ) |
| 69 | + VIDEOS=( $(youtube-dl --get-id "$DOMAIN/$CHAPTER") ) |
| 70 | +} |
| 71 | + |
| 72 | +check_commands "${DEPS[@]}" |
| 73 | + |
| 74 | +echo -e "\nFetching course topics..." |
| 75 | +extract_topics |
| 76 | +echo -e "Select topic:" |
| 77 | +menu_entry "${TOPIC_TITLES[@]}" |
| 78 | + |
| 79 | +echo -e "\nFetching subtopics under \"${TOPIC_TITLES[$SELECTION]}\"..." |
| 80 | +extract_subtopics "${TOPICS[$SELECTION]}" |
| 81 | +echo -e "Select subtopic:" |
| 82 | +menu_entry "${SUBTOPIC_TITLES[@]}" |
| 83 | + |
| 84 | +echo -e "\nFetching chapters under \"${SUBTOPIC_TITLES[$SELECTION]}\"..." |
| 85 | +extract_chapters "${SUBTOPICS[$SELECTION]}" |
| 86 | +echo -e "Select chapter:" |
| 87 | +menu_entry "${CHAPTER_TITLES[@]}" |
| 88 | + |
| 89 | +echo -e "\nFetching videos under \"${CHAPTER_TITLES[$SELECTION]}\"..." |
| 90 | +extract_videos "${CHAPTERS[$SELECTION]}" |
| 91 | +echo -e "Select video:" |
| 92 | + |
| 93 | +# some chapters have only text materials and no videos |
| 94 | +if [[ "${#VIDEO_TITLES[@]}" = "0" ]]; then |
| 95 | + echo -e "No videos found!" |
| 96 | +else |
| 97 | + menu_entry "${VIDEO_TITLES[@]}" |
| 98 | + |
| 99 | + VIDEO_URL="https://youtube.com/watch?v=${VIDEOS[$SELECTION]}" |
| 100 | + echo -e "\nURL of selected video: $VIDEO_URL" |
| 101 | + read -p "Download video (y/n)? " SAVE |
| 102 | + if [[ "$SAVE" = 'y' || "$SAVE" = 'Y' ]]; then |
| 103 | + youtube-dl "$VIDEO_URL" |
| 104 | + else |
| 105 | + echo -e "Aborting..." |
| 106 | + fi |
| 107 | +fi |
0 commit comments