SimplyTranslate broke the API again, also add another flavor

Signed-off-by: Izuru Yakumo <yakumo.izuru@chaotic.ninja>

git-svn-id: file:///srv/svn/repo/suwako/trunk@18 0b558ee1-521d-8b46-a41b-40029c97c055
This commit is contained in:
yakumo.izuru 2023-09-23 20:41:57 +00:00
parent 8071aee295
commit f8d0324c54
5 changed files with 80 additions and 21 deletions

View File

@ -1,7 +1,7 @@
PREFIX ?= /usr/local
GOARCH ?= amd64
GOFLAGS ?= -v -ldflags "-w -X `go list`.Version=$(VERSION) -X `go list`.Commit=$(COMMIT) -X `go list`.Build=$(BUILD)" -tags "static_build"
GOFLAGS ?= -v -ldflags "-w -X `go list`.Version=${VERSION} -X `go list`.Commit=${COMMIT} -X `go list`.Build=${BUILD}" -tags "static_build"
GOOS ?= linux
BRANCH = `git rev-parse --abbrev-ref HEAD`
@ -9,14 +9,19 @@ BUILD = `git show -s --pretty=format:%cI`
COMMIT = `git rev-parse --short HEAD || echo "$COMMIT"`
VERSION = `git describe --abbrev=0 --tags 2>/dev/null || echo "$VERSION"`
build:
go build $(GOFLAGS) ./cmd/suwako
strip suwako
all: suwako suwako-mozhi
suwako:
go build ${GOFLAGS} ./cmd/suwako
suwako-mozhi:
go build ${GOFLAGS} ./cmd/suwako-mozhi
clean:
rm -f suwako
rm -f suwako suwako-mozhi
install:
install -Dm0755 suwako ${PREFIX}/bin/suwako
install -Dm0755 suwako-mozhi ${PREFIX}/bin/suwako-mozhi
install -Dm0644 suwako.1 ${PREFIX}/share/man/man1/suwako.1
uninstall:
rm -f ${PREFIX}/bin/suwako
rm -f ${PREFIX}/bin/suwako-mozhi
rm -f ${PREFIX}/share/man/man1/suwako.1

14
README
View File

@ -1,14 +0,0 @@
suwako
=====
Command-line client for SimplyTranslate in Go.
Usage
-----
-f [lang] (default: auto)
-i [input]
-t [lang] (default: en)
Environment variables
---------------------
* SUWAKO_ENGINE
* SUWAKO_INSTANCE

68
cmd/suwako-mozhi/main.go Normal file
View File

@ -0,0 +1,68 @@
// $TheSupernovaDuo: suwako,v 1.5.1 2023/4/15 23:9:28 yakumo_izuru Exp $
// Command line client for Mozhi, a (yet another) privacy friendly frontend to other translation engines
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"marisa.chaotic.ninja/suwako"
"os"
)
var (
engine string
instance string
input string
source string
target string
)
type Translate struct {
Output string `json:"translated-text"`
}
func init() {
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(&input, "i", "", "Enter the text to be translated")
flag.StringVar(&target, "t", "en", "Set the language to translate to")
flag.Usage = func() {
fmt.Printf("usage: suwako -f [lang] -i [text] -t [lang]\nversion: %v\n", suwako.FullVersion())
}
}
func main() {
engine = os.Getenv("SUWAKO_ENGINE")
instance = os.Getenv("SUWAKO_INSTANCE")
flag.Parse()
if len(engine) == 0 || len(instance) == 0 {
log.Println("SUWAKO_ENGINE and/or SUWAKO_INSTANCE are unset")
log.Println("Defaulting to mozhi.aryak.me with engine 'google'")
engine = "google"
instance = "https://mozhi.aryak.me"
}
if len(input) == 0 {
log.Fatal("Missing input text.")
}
// 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)
fmt.Printf("%v\n",translate.Output)
}
func sanityCheck(err error) {
if err != nil {
log.Fatal(err)
}
}

View File

@ -21,7 +21,7 @@ var (
target string
)
type Translate struct {
Output string `json:"translated-text"`
Output string `json:"translated_text"`
}
func init() {
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")

View File

@ -3,7 +3,7 @@
.Os
.Sh NAME
.Nm suwako
.Nd Command-line client for SimplyTranslate in Go
.Nd Command-line client for privacy friendly translation frontends
.Sh SYNOPSIS
.Nm
.Fl e Ar engine