Browse Source

Add 'deepl-cli'

First commit
netico 2 years ago
parent
commit
1964671ab8
1 changed files with 54 additions and 0 deletions
  1. 54 0
      deepl-cli

+ 54 - 0
deepl-cli

@@ -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