3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-05 11:39:34 +01:00
pbot/applets/jisho.sh
Pragmatic Software 3d97dc2c33 Rename "modules" to "applets"
"Applet" is a much better name for the external command-line
scripts and programs that can be loaded as PBot commands. They
will no longer be confused with Perl modules.

https://en.wikipedia.org/wiki/Applet
2021-11-19 18:05:50 -08:00

35 lines
947 B
Bash
Executable File
Vendored

#!/bin/bash
url=https://jisho.org/api/v1/search/words
n=1 w=()
while (( $# )); do
case $1 in
-n) shift; n=$1;;
-n*) n=${1#-n};;
-all) n=0;;
*) w+=("$1");;
esac
shift
done
w=${w[*]}
if [[ $w ]]; then
curl -fsSLG "$url" --data-urlencode "keyword=$w" |
jq -r --arg w "$w" --argjson n "$n" '
.data |
length as $N |
if $N == 0 then
"\($w): word not found"
else if $n > $N then
"\($w): only \($N) definition\(if $N > 1 then "s" else "" end) available"
else
(if $n == 0 then range($N) else $n - 1 end) as $i |
.[$i] |
.japanese[0].reading as $r |
.slug +
if $r != .slug then " (\($r))" else "" end +
" [\($i + 1)/\($N)]: " +
(.senses | map(.english_definitions | join(", ")) | join("; "))
end end'
else
echo "usage: jisho [-n INDEX] WORD"
fi