rep: hard-coding api endpoint, change variable names to be less confusing, etc
git-svn-id: file:///srv/svn/repo/suwako/trunk@10 0b558ee1-521d-8b46-a41b-40029c97c055
This commit is contained in:
23
main.go
23
main.go
@@ -1,4 +1,4 @@
|
|||||||
// $TheSupernovaDuo: stcli,v 1.3.1 2023/02/11 07:54:25 akoizumi Exp
|
// $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
|
// Command line client for SimplyTranslate, a privacy friendly frontend to other translation engines
|
||||||
package main
|
package main
|
||||||
|
|
||||||
@@ -10,36 +10,37 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
engine string
|
engine string
|
||||||
from string
|
|
||||||
instance string
|
instance string
|
||||||
input string
|
input string
|
||||||
to string
|
source string
|
||||||
|
target string
|
||||||
)
|
)
|
||||||
type Translate struct {
|
type Translate struct {
|
||||||
Output string `json:"translated-text"`
|
Output string `json:"translated-text"`
|
||||||
}
|
}
|
||||||
func init() {
|
func init() {
|
||||||
flag.StringVar(&engine, "e", "google", "Translation engine to use (default: google)")
|
flag.StringVar(&engine, "e", "google", "Translation engine to use")
|
||||||
flag.StringVar(&from, "f", "auto", "Set the language to translate from. This can be skipped as it will autodetect the language you're translating from")
|
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://simplytranslate.org/api/translate/", "Instance to use (default: https://simplytranslate.org/api/translate/)")
|
flag.StringVar(&instance, "i", "https://translate.bus-hit.me", "Instance to use)")
|
||||||
flag.StringVar(&input, "I", "", "Enter the text to be translated")
|
flag.StringVar(&input, "I", "", "Enter the text to be translated")
|
||||||
flag.StringVar(&to, "t", "en", "Set the language to translate to (default: en)")
|
flag.StringVar(&target, "t", "en", "Set the language to translate to")
|
||||||
}
|
}
|
||||||
func main() {
|
func main() {
|
||||||
// Begin flag parsing
|
// Begin flag parsing
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
// Check if any of those two variables is empty.
|
// Check if there's any input, otherwise bail out.
|
||||||
// It actually needs the two to have content.
|
|
||||||
if len(input) == 0 {
|
if len(input) == 0 {
|
||||||
log.Fatal("Missing input.")
|
log.Fatal("Missing input.")
|
||||||
}
|
}
|
||||||
// Map a variable to the struct
|
// Map a variable to the struct
|
||||||
var translate Translate
|
var translate Translate
|
||||||
// Build the full URL to query
|
// Build the full URL to query
|
||||||
var encinput = url.PathEscape(input)
|
var encInput = url.PathEscape(input)
|
||||||
var queryURL = instance + "?engine=" + engine + "&from=" + from + "&to=" + to + "&text=" + encinput
|
var apiEndpoint = "/api/translate"
|
||||||
|
var queryURL = instance + apiEndpoint + "?engine=" + engine + "&from=" + source + "&to=" + target + "&text=" + encInput
|
||||||
// Begin the request and process the response
|
// Begin the request and process the response
|
||||||
resp, err := http.Get(queryURL)
|
resp, err := http.Get(queryURL)
|
||||||
sanityCheck(err)
|
sanityCheck(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user