Skip to content

Commit 53c8b86

Browse files
Merge pull request #30 from vitorgalvao/patch-2
Replace jq with osascript (JavaScript)
2 parents aac2c43 + 5fe415a commit 53c8b86

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

deepl.sh

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ POSTFIX="${DEEPL_POSTFIX:-.}"
1111
VERSION="1.13"
1212
PATH="$PATH:/usr/local/bin/"
1313
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
14-
PARSER="jq"
15-
if ! type "$PARSER" >/dev/null 2>&1; then
16-
echo "Binary '$PARSER' not found. Install it via 'brew install jq'." >&2
17-
exit 1
18-
fi
1914
###############################################################################
2015

2116
# helper functions ############################################################
@@ -92,16 +87,30 @@ if [ -n "$KEY" ]; then
9287
url="https://api-free.deepl.com/v2/translate"
9388
fi
9489
result=$(curl -s -X POST "$url" -H "Authorization: DeepL-Auth-Key $KEY" -d "text=$query" -d "target_lang=${LANGUAGE:-EN}")
95-
echo "$result" | "$PARSER" -r '{items: [.translations[] | {uid: null, arg:.text, valid: "yes", autocomplete: "autocomplete",title: .text}]}'
90+
osascript -l JavaScript -e 'function run(argv) {
91+
const translations = JSON.parse(argv[0])["translations"].map(item => ({
92+
title: item["text"],
93+
arg: item["text"]
94+
}))
95+
96+
return JSON.stringify({ items: translations })
97+
}' "$result"
9698
else
9799
result=$(curl -s 'https://www2.deepl.com/jsonrpc' \
98100
"${HEADER[@]}" \
99101
--data-binary $"$data")
100102
if [[ $result == *'"error":{"code":'* ]]; then
101-
message=$(echo "$result" | "$PARSER" -r '.["error"]|.message')
103+
message="$(osascript -l JavaScript -e 'function run(argv) { return JSON.parse(argv[0])["error"]["message"] }')"
102104
printJson "Error: $message"
103105
else
104-
echo "$result" | "$PARSER" -r '{items: [.result.translations[0].beams[] | {uid: null, arg:.postprocessed_sentence, valid: "yes", autocomplete: "autocomplete",title: .postprocessed_sentence}]}'
106+
osascript -l JavaScript -e 'function run(argv) {
107+
const translations = JSON.parse(argv[0])["result"]["translations"][0]["beams"].map(item => ({
108+
title: item["postprocessed_sentence"],
109+
arg: item["postprocessed_sentence"]
110+
}))
111+
112+
return JSON.stringify({ items: translations })
113+
}' "$result"
105114
fi
106115
fi
107116
###############################################################################

0 commit comments

Comments
 (0)