summaryrefslogtreecommitdiff
path: root/bin/extra/trl
blob: 957d0fb4b0428fbb9c29e0f27e9023489abec923 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/sh

# Translate words using http://context.reverso.net and scraping the answers
# with 'pup' and 'curl'.

# returns available languages
languages () {
	printf 'arabic\nchinese\nczech\ndanish\ndutch\nenglish\nfrench\ngerman\ngreek\nhebrew\nhindi\nhungarian\nitalian\njapanese\nkorean\npersian\npolish\nportuguese\nromanian\nslovak\nspanish\nswedish\nthai\nturkish\nukrainian\n'
}

if [ -z "${word:=$1}" ]
then
    >&2 printf '>'
    word="$(head -n1)"
fi
[ "$word" ] || exit 1

primary="$(languages | fzf --prompt="from:")"
[ "$primary" ] || exit 1

secondary="$(languages | fzf --prompt="to:")"
[ "$secondary" ] || exit 1

# url="https://www.reverso.net/text-translation#sl=$primary&tl=$secondary&text=$word"
url="https://context.reverso.net/translation/$primary-$secondary/$word"
curl -s "$url"  \
    --compressed \
    -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0' \
    -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8' \
    -H 'Accept-Encoding: gzip, deflate, br' |
    pup 'a.link_highlighted em text{}' |
    sed 's/.*/\L&/' |
    sort | uniq | sed 's/.*/  &/'