[1] | 1 | // Yet another command-line client for SimplyTranslate
|
---|
| 2 | // All Rites Reversed (ĸ) 3188 Aoi Koizumi, Czar of KST, mutefall, shokara
|
---|
| 3 |
|
---|
| 4 | package main
|
---|
| 5 |
|
---|
| 6 | import (
|
---|
| 7 | "fmt"
|
---|
| 8 | "net/http"
|
---|
| 9 | "flag"
|
---|
| 10 | "io"
|
---|
| 11 | "path/filepath"
|
---|
| 12 | "os"
|
---|
| 13 | )
|
---|
| 14 |
|
---|
| 15 | var (
|
---|
| 16 | instanceURL string
|
---|
| 17 | engine string
|
---|
| 18 | fromLang string
|
---|
| 19 | toLang string
|
---|
| 20 | text string
|
---|
| 21 | )
|
---|
| 22 |
|
---|
| 23 | func init() {
|
---|
| 24 | // Point flags to variables
|
---|
| 25 | flag.StringVar(&instanceURL, "i", "https://translate.bus-hit.me/api/translate/", "Instance for SimplyTranslate (default: https://translate.bus-hit.me)")
|
---|
| 26 | flag.StringVar(&engine, "e", "google", "Translation engine (default: google)")
|
---|
| 27 | flag.StringVar(&fromLang, "f", "auto", "Source language (default: auto)")
|
---|
| 28 | flag.StringVar(&toLang, "t", "", "Target language")
|
---|
| 29 | flag.StringVar(&text, "T", "", "Text to translate")
|
---|
| 30 | }
|
---|
| 31 | func main() {
|
---|
| 32 | // Start parsing
|
---|
| 33 | flag.Parse()
|
---|
| 34 |
|
---|
| 35 | // Hand-craft the request URL
|
---|
| 36 | queryURL := instanceURL + "?engine=" + engine + "&from=" + fromLang + "&to=" + toLang + "&text=" + text
|
---|
| 37 |
|
---|
| 38 | // Make a request to said URL
|
---|
| 39 | resp, err := http.Get(queryURL)
|
---|
| 40 | if err != nil {
|
---|
| 41 | panic(err)
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | defer resp.Body.Close()
|
---|
| 45 | body, err := io.ReadAll(resp.Body)
|
---|
| 46 | output := body
|
---|
| 47 |
|
---|
| 48 | fmt.Printf("Input: %s \n", text)
|
---|
| 49 | fmt.Printf("Output: %s \n", output)
|
---|
| 50 | }
|
---|
| 51 | func printUsage() {
|
---|
| 52 | fmt.Printf("Usage for %s : \n", filepath.Base(os.Args[0]))
|
---|
| 53 | fmt.Printf("-e \t Use the specified translation engine (default: google)\n")
|
---|
| 54 | fmt.Printf("-f \t (default: auto)\n")
|
---|
| 55 | fmt.Printf("-t \t (default: none)\n")
|
---|
| 56 | fmt.Printf("-T \t Text to translate\n")
|
---|
| 57 |
|
---|
| 58 | os.Exit(1)
|
---|
| 59 | }
|
---|