Changeset 5 in code for trunk/main.go
- Timestamp:
- Dec 13, 2022, 1:18:40 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/main.go
r4 r5 1 // $KyokoNet: stcli-go,v 1.1 2022/12/13 10:07:00 akoizumi Exp 2 // Command line client for SimplyTranslate, a privacy friendly frontend to Google Translate 1 3 package main 2 4 … … 10 12 "os" 11 13 ) 12 13 14 var ( 14 instanceURLstring15 enginestring16 fromLangstring17 toLangstring18 t extstring15 engine string 16 from string 17 instance string 18 input string 19 to string 19 20 ) 20 21 type TranslateAPI struct { 22 OutText string `json:"translated-text"` 21 type Translate struct { 22 Output string `json:"translated-text"` 23 23 } 24 25 24 func init() { 26 flag.StringVar(& instanceURL, "i", "https://translate.bus-hit.me/api/translate/", "Instance for SimplyTranslate (default: https://translate.bus-hit.me)")27 flag.StringVar(& engine, "e", "google", "Translation engine (default: google)")28 flag.StringVar(& fromLang, "f", "auto", "Source language (default: auto)")29 flag.StringVar(& toLang, "t", "", "Target language (default: unset)")30 flag.StringVar(&t ext, "T", "", "Text to translate (default: unset)")25 flag.StringVar(&engine, "e", "google", "Translation engine to use (default: google)") 26 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") 27 flag.StringVar(&instance, "i", "https://simplytranslate.org/api/translate/", "Instance to use (default: https://simplytranslate.org/api/translate/)") 28 flag.StringVar(&input, "I", "", "Enter the text to be translated") 29 flag.StringVar(&to, "t", "en", "Set the language to translate to (default: en)") 31 30 } 32 31 func main() { 32 // Begin flag parsing 33 33 flag.Parse() 34 if len(text) == 0 || len(toLang) == 0 { 34 // Check if any of those two variables is empty. 35 // It actually needs the two to have content. 36 if len(input) == 0 || len(to) == 0 { 35 37 log.Fatal("Missing either the text or the target language.") 36 38 os.Exit(1) 37 39 } 38 var o TranslateAPI 39 var queryURL = instanceURL + "?engine=" + engine + "&from=" + fromLang + "&to=" + toLang + "&text=" + text 40 // Map a variable to the struct 41 var translate Translate 42 // Build the full URL to query 43 var queryURL = instance + "?engine=" + engine + "&from=" + from + "&to=" + to + "&text=" + input 44 // Begin the request and process the response 40 45 req, err := http.Get(queryURL) 41 46 sanityCheck(err) 42 47 defer req.Body.Close() 43 48 resp, err := io.ReadAll(req.Body) 44 _ = json.Unmarshal([]byte(resp), & o)49 _ = json.Unmarshal([]byte(resp), &translate) 45 50 sanityCheck(err) 46 fmt.Printf("Input: %s (%s)\n",text,fromLang) 47 fmt.Printf("Output: %s (%s)\n",o.OutText,toLang) 51 // Pretty-print both the input and the output given. 52 fmt.Printf("Input: %v\n", input) 53 fmt.Printf("Output: %v\n",translate.Output) 48 54 } 49 55 func sanityCheck(err error) {
Note:
See TracChangeset
for help on using the changeset viewer.