-
Notifications
You must be signed in to change notification settings - Fork 3
/
kamite-deeplx.sh
executable file
·66 lines (57 loc) · 1.99 KB
/
kamite-deeplx.sh
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env bash
#
# Kamite DeepLX: fetch DeepL translation of given JP text through DeepLX and send it to Kamite
#
# Made to be executed as a Kamite custom command, e.g.:
#
# deeplx {
# symbol: DEP
# name: DeepL lookup
# command: ["/path/to/kamite-deeplx.sh", "EN", "{effectiveText}"]
# }
#
# (`EN` is the target language code)
#
# Executing the above command from Kamite passes the current effective chunk text to this script,
# which then sends back its translation to Kamite to be displayed in the chunk translation
# component.
#
# The script can also be specified as a Kamite `chunk-add` event handler (currently an
# experimental, undocumented funtionality) to automatically fetch a translation each time a new
# chunk is added:
#
# events.handlers: [
# { on: "chunk-add", exec: ["/path/to/kamite-deeplx.sh", "EN", "--event", "{eventData}"] }
# ]
#
# Note that such intensive use makes it more likely for your IP to be blocked by DeepL.
#
# Dependencies:
# jq, curl, libnotify, DeeplX (must be running!)
#
# DeepLX download: https://github.com/OwO-Network/DeepLX/releases
#
DEEPLX_ENDPOINT='localhost:1188/translate'
KAMITE_ENDPOINT='localhost:4110/cmd/chunk/show-translation'
NAME='kamite-deeplx'
if [ $# -lt 2 ]; then
echo "USAGE: $NAME <target-language> <japanese-text> OR kamite-deeplx <target-language> --event <kamite-chunk-add-event-data>"
notify-send "$NAME" 'Wrong number of parameters'
exit 1
fi
if [[ "$2" == "--event" ]]; then
text=$(jq -r '.chunkText' <<< "$3")
else
text="$2"
fi
text=${text//$'\n'/}
res=$(curl -s -X POST \
-d '{"text":"'"$text"'","source_lang":"JA","target_lang":"'"$1"'"}' \
$DEEPLX_ENDPOINT)
# NOTE: Alternative translations (`alternatives`) are currently ignored
translation=$(jq -r '.data' <<< "$res" | sed "s/\"/\\\\\"/g")
if [[ $translation == 'null' ]]; then
notify-send "$NAME" 'Error fetching translation'
exit 1
fi
curl -i -X POST -d '{"translation":"'"$translation"'"}' "$KAMITE_ENDPOINT"