feat: stop hardcoding default instance and engine, also other minor changes.

git-svn-id: file:///srv/svn/repo/suwako/trunk@12 0b558ee1-521d-8b46-a41b-40029c97c055
This commit is contained in:
koizumi.aoi
2023-03-25 16:43:22 +00:00
parent 13be100b46
commit 393a3db9bc
3 changed files with 18 additions and 71 deletions

59
main.go
View File

@@ -1,59 +0,0 @@
// $TheSupernovaDuo: stcli,v 1.4.0 2023/03/13 08:14:55 akoizumi Exp
// Command line client for SimplyTranslate, a privacy friendly frontend to other translation engines
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"net/url"
)
var (
engine string
instance string
input string
source string
target string
)
type Translate struct {
Output string `json:"translated-text"`
}
func init() {
flag.StringVar(&engine, "e", "google", "Translation engine to use")
flag.StringVar(&source, "f", "auto", "Set the language to translate from. This can be skipped as it will autodetect the language you're translating from")
flag.StringVar(&instance, "i", "https://translate.bus-hit.me", "Instance to use)")
flag.StringVar(&input, "I", "", "Enter the text to be translated")
flag.StringVar(&target, "t", "en", "Set the language to translate to")
}
func main() {
// Begin flag parsing
flag.Parse()
// Check if there's any input, otherwise bail out.
if len(input) == 0 {
log.Fatal("Missing input.")
}
// Map a variable to the struct
var translate Translate
// Build the full URL to query
var encInput = url.PathEscape(input)
var apiEndpoint = "/api/translate"
var queryURL = instance + apiEndpoint + "?engine=" + engine + "&from=" + source + "&to=" + target + "&text=" + encInput
// Begin the request and process the response
resp, err := http.Get(queryURL)
sanityCheck(err)
defer resp.Body.Close()
// JSON decoding
_ = json.NewDecoder(resp.Body).Decode(&translate)
sanityCheck(err)
// Pretty-print both the input and the output given.
fmt.Printf("Input: %v\n", input)
fmt.Printf("Output: %v\n",translate.Output)
}
func sanityCheck(err error) {
if err != nil {
log.Fatal(err)
}
}