commit bc525ea530233ea9608b2bfdbe2943c7537c32fe parent ffd9cda1c6c437a9a1a8fa3acb0b7ddbe70501c4 Author: Alex Balgavy <alex@balgavy.eu> Date: Wed, 22 Jun 2022 18:27:30 +0200 forvo: play the top forvo.com pronunciation of a phrase in a language Diffstat:
A | scripts/forvo | | | 22 | ++++++++++++++++++++++ |
1 file changed, 22 insertions(+), 0 deletions(-)
diff --git a/scripts/forvo b/scripts/forvo @@ -0,0 +1,22 @@ +#!/bin/sh +# Play the top pronunciation from forvo.com for a phrase & language +die() { printf '%s\n' "$1" >&2 && exit 1; } +checkdeps() { + for com in "$@"; do + command -v "$com" >/dev/null 2>&1 \ + || { printf '%s required but not found.\n' "$com" >&2 && exit 1; } + done +} +checkdeps curl pup awk base64 mpv + +[ $# -eq 2 ] || die "Usage: forvo PHRASE ISO_LANG_CODE" +phrase_encoded="$(printf '%s' "$1" | sed 's/ /%20/g')" +lang="$2" +search_url="https://forvo.com/search/$phrase_encoded/$lang" +audio_path="$(curl -sL "$search_url" \ + | pup '.results_match li span.play:first-of-type attr{onclick}' \ + | awk -F ''' '{print $2 }' \ + | base64 -d)" +[ -z "$audio_path" ] && die "Not found." +audio_url="https://audio.forvo.com/mp3/$audio_path" +mpv -loop "$audio_url"