Add 'deepl-cli'

First commit
This commit is contained in:
netico 2021-12-22 11:00:53 +01:00
parent 15197c0355
commit 1964671ab8

54
deepl-cli Normal file
View file

@ -0,0 +1,54 @@
#!/bin/bash
# Configuration
# You need an API key. You can get it for free from https://www.deepl.com/pro-api
key=""
usage="usage: $0 [-h] [-l] target language [-t] text"
lang="IT" # Target language (EN, FR, ES, RU...)
# API request
request () {
response=$(curl -s https://api-free.deepl.com/v2/translate -d auth_key="$key" -d "text=$2" -d "target_lang=$1")
echo $response
}
# Main
while getopts ":hl:t:" option; do
case $option in
h)
echo $usage;
exit
;;
l)
lang=${OPTARG^^} # Uppercase
;;
t)
text=${OPTARG} # Text to translate
;;
\?)
echo "error: option -$OPTARG is not implemented";
exit
;;
esac
done
# Get stdin
if [ -z "$text" ] && [ -p /dev/stdin ]; then
text=$(cat /dev/stdin)
fi
if [ -z "$text" ]; then
echo "error: text not provided or empty"
echo $usage
exit
fi
if [ -z "$key" ]; then
echo "error: you need an API key. You can get it for free from https://www.deepl.com/pro-api"
echo $usage
exit
fi
response=$(request $lang "$text")
# You can use jq, alternatively
echo $response | cut -d: -f4 | cut -d'"' -f2