Refactor, now require instance URL to include API endpoint
Signed-off-by: Izuru Yakumo <yakumo.izuru@chaotic.ninja> git-svn-id: file:///srv/svn/repo/suwako/trunk@22 0b558ee1-521d-8b46-a41b-40029c97c055
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
// $TheSupernovaDuo: suwako,v 1.5.1 2023/4/15 23:9:28 yakumo_izuru Exp $
|
// $TheSupernovaDuo: suwako,v 1.5.2 2023/10/21 00:58:44 yakumo_izuru 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
|
||||||
|
|
||||||
@@ -22,9 +22,17 @@ var (
|
|||||||
source string
|
source string
|
||||||
target string
|
target string
|
||||||
)
|
)
|
||||||
|
|
||||||
type Translate struct {
|
type Translate struct {
|
||||||
Output string `json:"translated_text"`
|
Output string `json:"translated_text"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func errCheck(err error) {
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Something happened :(", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func loadCfg() {
|
func loadCfg() {
|
||||||
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(&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(&input, "i", "", "Enter the text to be translated")
|
||||||
@@ -35,39 +43,45 @@ func loadCfg() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
home, err := os.UserHomeDir()
|
home, err := os.UserHomeDir()
|
||||||
sanityCheck(err)
|
errCheck(err)
|
||||||
conf := home + "/.suwako/suwako.conf"
|
conf := home + "/.suwako/suwako.conf"
|
||||||
err = godotenv.Load(conf)
|
err = godotenv.Load(conf)
|
||||||
sanityCheck(err)
|
errCheck(err)
|
||||||
|
|
||||||
engine = os.Getenv("SUWAKO_ENGINE")
|
engine = os.Getenv("SUWAKO_ENGINE")
|
||||||
instance = os.Getenv("SUWAKO_INSTANCE")
|
instance = os.Getenv("SUWAKO_INSTANCE")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
}
|
}
|
||||||
func check() {
|
|
||||||
if len(input) == 0 || len(target) == 0 {
|
|
||||||
log.Fatal("Either there is no input or there is no target language")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// Load configuration and parse flags
|
||||||
loadCfg()
|
loadCfg()
|
||||||
check()
|
|
||||||
|
|
||||||
|
// Verify command-line inputs
|
||||||
|
if len(input) == 0 {
|
||||||
|
log.Fatal("There is no input")
|
||||||
|
}
|
||||||
|
if len(target) == 0 {
|
||||||
|
log.Fatal("No target language")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map variable to struct
|
||||||
var translate Translate
|
var translate Translate
|
||||||
var encInput = url.PathEscape(input)
|
|
||||||
var apiEndpoint = "/api/translate"
|
|
||||||
var queryURL = instance + apiEndpoint + "?engine=" + engine + "&from=" + source + "&to=" + target + "&text=" + encInput
|
|
||||||
|
|
||||||
|
// Encode input just in case
|
||||||
|
var encInput = url.PathEscape(input)
|
||||||
|
|
||||||
|
// Construct the final path to query
|
||||||
|
var queryURL = instance + "?engine=" + engine + "&from=" + source + "&to=" + target + "&text=" + encInput
|
||||||
|
|
||||||
|
// Shoot danmaku to path
|
||||||
resp, err := http.Get(queryURL)
|
resp, err := http.Get(queryURL)
|
||||||
sanityCheck(err)
|
errCheck(err)
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// Decode JSON response, discard everything else, print to standard output
|
||||||
_ = json.NewDecoder(resp.Body).Decode(&translate)
|
_ = json.NewDecoder(resp.Body).Decode(&translate)
|
||||||
sanityCheck(err)
|
errCheck(err)
|
||||||
fmt.Printf("%v\n",translate.Output)
|
fmt.Printf("%v\n",translate.Output)
|
||||||
}
|
}
|
||||||
func sanityCheck(err error) {
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user