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:
koizumi.aoi
2022-12-13 03:10:11 +00:00
parent 82dd32fba9
commit 9647dae5fa
6 changed files with 63 additions and 23 deletions

4
Makefile Normal file
View File

@@ -0,0 +1,4 @@
build:
go build
clean:
rm -f stcli-go

3
README Normal file
View File

@@ -0,0 +1,3 @@
stcli-go
========
Command-line client for SimplyTranslate in Go.

2
go.mod
View File

@@ -1,3 +1,5 @@
module git.kyoko-project.wer.ee/koizumi.aoi/stcli-go module git.kyoko-project.wer.ee/koizumi.aoi/stcli-go
go 1.18 go 1.18
require github.com/astaxie/bat v0.0.2

2
go.sum Normal file
View File

@@ -0,0 +1,2 @@
github.com/astaxie/bat v0.0.2 h1:EOl52CfuFEnFqu9/mzex93mh7JWdtEGbGQlpih00GWQ=
github.com/astaxie/bat v0.0.2/go.mod h1:poAI7wDJd5LpfwfBXsGWYIqmVO66dhBx31zhn0ZXdMI=

37
main.go
View File

@@ -1,11 +1,12 @@
// Yet another command-line client for SimplyTranslate // 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 package main
import ( import (
"fmt" "encoding/json"
"flag" "flag"
"fmt"
"io" "io"
"log" "log"
"net/http" "net/http"
@@ -20,8 +21,11 @@ var (
text string text string
) )
type TranslateAPI struct {
OutText string `json:"translated-text"`
}
func init() { 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(&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(&engine, "e", "google", "Translation engine (default: google)")
flag.StringVar(&fromLang, "f", "auto", "Source language (default: auto)") flag.StringVar(&fromLang, "f", "auto", "Source language (default: auto)")
@@ -29,10 +33,8 @@ func init() {
flag.StringVar(&text, "T", "", "Text to translate (default: unset)") flag.StringVar(&text, "T", "", "Text to translate (default: unset)")
} }
func main() { func main() {
// Start parsing
flag.Parse() flag.Parse()
// I assume this works?
if len(text) == 0 { if len(text) == 0 {
log.Printf("Text to translate is required. \n") log.Printf("Text to translate is required. \n")
os.Exit(1) os.Exit(1)
@@ -41,25 +43,24 @@ func main() {
os.Exit(1) os.Exit(1)
} }
// Hand-craft the request URL var queryURL = instanceURL + "?engine=" + engine + "&from=" + fromLang + "&to=" + toLang + "&text=" + text
queryURL := instanceURL + "?engine=" + engine + "&from=" + fromLang + "&to=" + toLang + "&text=" + text
req, err1 := http.Get(queryURL)
var o TranslateAPI
// Make a request to said URL
resp, err1 := http.Get(queryURL)
if err1 != nil { 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() defer req.Body.Close()
resp, err2 := io.ReadAll(req.Body)
output, err2 := io.ReadAll(resp.Body)
if err2 != nil { if err2 != nil {
panic(errno) log.Printf("Couldn't process response %s\n", err2)
} }
// Well... _ = json.Unmarshal([]byte(resp), &o)
fmt.Printf("Input: %s \n", text)
fmt.Printf("Output: %s \n", output) fmt.Printf("Input: %s (%s)\n",text,fromLang)
fmt.Printf("Output: %s (%s)\n",o.OutText,toLang)
} }

28
stcli-go.1 Normal file
View File

@@ -0,0 +1,28 @@
.Dd Aftermath 55, 3188
.Dt STCLI-GO 1
.Os
.Sh NAME
.Nm stcli-go
.Nd Command-line client for SimplyTranslate in Go
.Sh SYNOPSIS
.Nm
.Fl e Ar engine
.Fl f Ar from
.Fl i Ar instance
.Fl t Ar to
.Fl T Ar text
.Sh DESCRIPTION
Self-explanatory, besides, this was made as
a rewrite from a shell script that had curl
and awk for dependencies. It fully serves
as a drop-in replacement.
.Sh AUTHORS
.An Aoi K. Aq Mt koizumi.aoi@kyoko-project.wer.ee
.Pp
.An Czar of KST Aq Mt czar@kalli.st
.Pp
.An mutefall
.Pp
.An Shokara Kou Aq Mt kou@clearnet.fqdn
.Pp
.An Baobab Aq Mt baobab@honeypot.im