dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

forvo (983B)


      1 #!/bin/sh
      2 # Play the top pronunciation from forvo.com for a phrase & language
      3 die() { printf '%s\n' "$1" >&2 && exit 1; }
      4 checkdeps() {
      5   for com in "$@"; do
      6     command -v "$com" >/dev/null 2>&1 \
      7       || { printf '%s required but not found.\n' "$com" >&2 && exit 1; }
      8   done
      9 }
     10 checkdeps curl pup awk base64 mpv
     11 
     12 [ $# -eq 2 ] || die "Usage: forvo PHRASE ISO_LANG_CODE"
     13 phrase_encoded="$(printf '%s' "$1" | sed 's/ /%20/g')"
     14 lang="$2"
     15 search_url="https://forvo.com/search/$phrase_encoded/$lang"
     16 search_result="$(curl -sL "$search_url")"
     17 audio_path="$(printf '%s' "$search_result" \
     18   | pup '.results_match li div.play:first-of-type attr{onclick}' \
     19   | awk -F ''' '{print $2 }' \
     20   | base64 -d)"
     21 if [ -z "$audio_path" ]; then
     22   if [ "$(printf '%s' "$search_result" | pup '.results_match li span.lang_rec text{}')" = "Pending pronunciation" ]; then
     23     die "Not yet recorded."
     24   fi
     25   die "Not found."
     26 fi
     27 audio_url="https://audio.forvo.com/mp3/$audio_path"
     28 mpv -loop "$audio_url"