bin/pronounce: New script

I wrote this some time ago, but cleaned it up a bit so that others hopefully
can find this useful.  I'll be sure to use this as an example in my upcoming
LibrePlanet 2019 talk.

* bin/pronounce: New script.
master
Mike Gerwitz 2019-02-12 01:22:46 -05:00
parent 14a0f86797
commit 91919325a8
Signed by: mikegerwitz
GPG Key ID: 8C917B7F5DC51BA2
1 changed files with 86 additions and 0 deletions

86
bin/pronounce 100755
View File

@ -0,0 +1,86 @@
#!/bin/bash
# Pronounce a word using English audio from dictionary.cambridge.org
#
# Copyright (C) 2019 Mike Gerwitz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This script requires wget for scraping and mpv for playback. Please
# consider your privacy---if you do not want to leak the words you are
# looking up, prefix this command with `torify' to proxy through Tor.
#
# A suggestd improvement to this script to anyone who may want to do so:
# derive region and language from $LANG environment variables.
##
which mpv &>/dev/null || {
echo 'missing mpv' >&2
exit 1
}
declare -r name=$( basename "$0" )
declare -r www=${PRONOUNCE_WWW:-https://dictionary.cambridge.org/}
declare -r region=${PRONOUNCE_REGION:-us}
declare -r lang=${PRONOUNCE_LANG:-english}
usage()
{
cat <<EOU
Usage: $name word
Pronounce WORD using audio from an online dictionary
Ex: $name night
PRIVACY WARNING: This connects to PRONOUNCE_WWW (current value:
$www).
Consider prefixing this command with \`torify' to proxy through Tor.
You may also set PRONOUNCE_REGION (current value \`$region') and
PRONOUNCE_LANG (current value \`$lang').
Copyright (C) 2019 Mike Gerwitz
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; see the GNU GPLv3+ for details at
<https://www.gnu.org/licenses/>.
EOU
exit 64 # EX_USAGE
}
main()
{
test $# -gt 0 || usage
local -r word=$1
local -r ogg_url=$(
wget -O- "$www/$region/pronunciation/$lang/$word" \
| grep -o 'data-src-ogg="[^"]\+"' \
| grep "$region"_pron_ogg \
| head -n1 \
| cut -d\" -f2
)
test -n "$ogg_url" || {
echo "US OGG not found for \`$word'" >&2
exit 1
}
mpv -vo null "$www/$ogg_url"
}
main "$@"