Flawless Victory
Signed-off-by: Aoi K <koizumi.aoi@kyoko-project.wer.ee> git-svn-id: file:///srv/svn/repo/suwako/trunk@3 0b558ee1-521d-8b46-a41b-40029c97c055
This commit is contained in:
47
main.go
47
main.go
@@ -1,11 +1,12 @@
|
||||
// Yet another command-line client for SimplyTranslate
|
||||
// All Rites Reversed (ĸ) 3188 Aoi Koizumi, Czar of KST, mutefall, shokara
|
||||
// All Rites Reversed (ĸ) 3188 Aoi Koizumi, Czar of KST, mutefall, shokara, Baobab
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -14,14 +15,17 @@ import (
|
||||
|
||||
var (
|
||||
instanceURL string
|
||||
engine string
|
||||
fromLang string
|
||||
toLang string
|
||||
text string
|
||||
engine string
|
||||
fromLang string
|
||||
toLang string
|
||||
text string
|
||||
)
|
||||
|
||||
type TranslateAPI struct {
|
||||
OutText string `json:"translated-text"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Point flags to variables
|
||||
flag.StringVar(&instanceURL, "i", "https://translate.bus-hit.me/api/translate/", "Instance for SimplyTranslate (default: https://translate.bus-hit.me)")
|
||||
flag.StringVar(&engine, "e", "google", "Translation engine (default: google)")
|
||||
flag.StringVar(&fromLang, "f", "auto", "Source language (default: auto)")
|
||||
@@ -29,10 +33,8 @@ func init() {
|
||||
flag.StringVar(&text, "T", "", "Text to translate (default: unset)")
|
||||
}
|
||||
func main() {
|
||||
// Start parsing
|
||||
flag.Parse()
|
||||
|
||||
// I assume this works?
|
||||
if len(text) == 0 {
|
||||
log.Printf("Text to translate is required. \n")
|
||||
os.Exit(1)
|
||||
@@ -40,26 +42,25 @@ func main() {
|
||||
log.Printf("Target language is missing. \n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Hand-craft the request URL
|
||||
queryURL := instanceURL + "?engine=" + engine + "&from=" + fromLang + "&to=" + toLang + "&text=" + text
|
||||
|
||||
// Make a request to said URL
|
||||
resp, err1 := http.Get(queryURL)
|
||||
var queryURL = instanceURL + "?engine=" + engine + "&from=" + fromLang + "&to=" + toLang + "&text=" + text
|
||||
|
||||
req, err1 := http.Get(queryURL)
|
||||
var o TranslateAPI
|
||||
|
||||
if err1 != nil {
|
||||
log.Printf("Request could not be processed due to %s\n", err1)
|
||||
log.Printf("Couldn't process request %s\n", err1)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
|
||||
output, err2 := io.ReadAll(resp.Body)
|
||||
defer req.Body.Close()
|
||||
resp, err2 := io.ReadAll(req.Body)
|
||||
|
||||
if err2 != nil {
|
||||
panic(errno)
|
||||
log.Printf("Couldn't process response %s\n", err2)
|
||||
}
|
||||
|
||||
// Well...
|
||||
fmt.Printf("Input: %s \n", text)
|
||||
fmt.Printf("Output: %s \n", output)
|
||||
_ = json.Unmarshal([]byte(resp), &o)
|
||||
|
||||
fmt.Printf("Input: %s (%s)\n",text,fromLang)
|
||||
fmt.Printf("Output: %s (%s)\n",o.OutText,toLang)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user