Changeset 3 in code


Ignore:
Timestamp:
Dec 13, 2022, 3:10:11 AM (2 years ago)
Author:
koizumi.aoi
Message:

Flawless Victory

Signed-off-by: Aoi K <koizumi.aoi@…>

Location:
trunk
Files:
4 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/go.mod

    r2 r3  
    22
    33go 1.18
     4
     5require github.com/astaxie/bat v0.0.2
  • trunk/main.go

    r2 r3  
    11// Yet another command-line client for SimplyTranslate
    2 // All Rites Reversed (ĸ) 3188 Aoi Koizumi, Czar of KST, mutefall, shokara
     2// All Rites Reversed (ĸ) 3188 Aoi Koizumi, Czar of KST, mutefall, shokara, Baobab
    33
    44package main
    55
    66import (
     7        "encoding/json"
     8        "flag"
    79        "fmt"
    8         "flag"
    910        "io"
    1011        "log"
     
    1516var (
    1617        instanceURL string
    17         engine string
    18         fromLang string
    19         toLang string
    20         text string
     18        engine      string
     19        fromLang    string
     20        toLang      string
     21        text        string
    2122)
    2223
     24type TranslateAPI struct {
     25        OutText string `json:"translated-text"`
     26}
     27
    2328func init() {
    24         // Point flags to variables
    2529        flag.StringVar(&instanceURL, "i", "https://translate.bus-hit.me/api/translate/", "Instance for SimplyTranslate (default: https://translate.bus-hit.me)")
    2630        flag.StringVar(&engine, "e", "google", "Translation engine (default: google)")
     
    3034}
    3135func main() {
    32         // Start parsing
    3336        flag.Parse()
    3437
    35         // I assume this works?
    3638        if len(text) == 0 {
    3739                log.Printf("Text to translate is required. \n")
     
    4143                os.Exit(1)
    4244        }
    43        
    44         // Hand-craft the request URL
    45         queryURL := instanceURL + "?engine=" + engine + "&from=" + fromLang + "&to=" + toLang + "&text=" + text
    4645
    47         // Make a request to said URL
    48         resp, err1 := http.Get(queryURL)
     46        var queryURL = instanceURL + "?engine=" + engine + "&from=" + fromLang + "&to=" + toLang + "&text=" + text
     47
     48        req, err1 := http.Get(queryURL)
     49        var o TranslateAPI
     50
    4951        if err1 != nil {
    50                 log.Printf("Request could not be processed due to %s\n", err1)
     52                log.Printf("Couldn't process request %s\n", err1)
    5153        }
    52        
    53         defer resp.Body.Close()
    5454
    55 
    56         output, err2 := io.ReadAll(resp.Body)
     55        defer req.Body.Close()
     56        resp, err2 := io.ReadAll(req.Body)
    5757
    5858        if err2 != nil {
    59                 panic(errno)
     59                log.Printf("Couldn't process response %s\n", err2)
    6060        }
    6161
    62         // Well...
    63         fmt.Printf("Input: %s \n", text)
    64         fmt.Printf("Output: %s \n", output)
     62        _ = json.Unmarshal([]byte(resp), &o)
     63
     64        fmt.Printf("Input: %s (%s)\n",text,fromLang)
     65        fmt.Printf("Output: %s (%s)\n",o.OutText,toLang)
    6566}
Note: See TracChangeset for help on using the changeset viewer.