-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
221 lines (175 loc) · 4.48 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"bytes"
"bufio"
"encoding/json"
"fmt"
"flag"
"log"
"net/http"
"net/url"
"os"
"io/ioutil"
)
var (
verbose bool
to string
from string
txt string
apiKey string
filename string
)
const endpoint = "https://api.cognitive.microsofttranslator.com"
// Trans is the type use to unmarshal service response
type Trans struct {
Translations []TransData `json:"translations"`
}
// Data unmarshals the content of the reponse
type TransData struct {
Text string `json:"text"`
To string `json:"to"`
}
type ErrorData struct {
Code int `json:"code"`
Message string `json:"message"`
}
type Error struct {
Data ErrorData `json:"error"`
}
func init() {
flag.Usage = func() {
h := "-------------------------------------------------------\n"
h += "gotrans - Translate text using Azure Cognitive Services\n"
h += "-------------------------------------------------------\n"
h += "\nUsage:\n"
h += " ./gotrans -t nl \"<text-to-translate>\"\n"
h += " ./gotrans -t es -file <file>\n"
h += " echo \"text\" | gotrans [OPTIONS]\n"
h += " cat <file> | gotrans [OPTIONS]\n"
h += "\nOptions:\n"
h += " -h, --help\n Show help\n"
fmt.Fprintf(os.Stderr, h)
flag.PrintDefaults()
}
}
func parseFlags(){
flag.StringVar(&txt, "", "", "Text to translate")
flag.BoolVar(&verbose, "v", false, "Run in verbose mode")
flag.StringVar(&to, "t", "", "Target language. See list here: https://bit.ly/37o3PFX")
flag.StringVar(&from, "f", "", "Source Language (optional). Set empty to auto-detect")
flag.StringVar(&filename, "file", "", "File name")
flag.Parse()
txt = flag.Arg(0)
d("Parsing flags...")
d(len(os.Args), "args:", os.Args)
d("Source language:", from)
d("Target language:", to)
d("Verbose:", verbose)
d("File name:", filename)
apiKey = os.Getenv("TRKEY")
d("ApiKey:", apiKey)
if apiKey == "" {
log.Fatal("Please set/export the environment variable TRKEY.")
}
// read source file if specified
if filename != "" {
content, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
txt = string(content)
}
// if nothing else, load it from stdin
if txt == "" {
txt = readStdin()
}
}
func err(msg string){
fmt.Printf("\nError: %v\n\n", msg)
flag.Usage()
os.Exit(1)
}
func main() {
parseFlags()
if txt == ""{
err("No text to translate provided. Please specify it via command line, file or stdin")
} else if to == "" {
err("No target language provided. Please specify a target language.\nExample: gotrans -t fr \"text to translate\"")
}
uri := endpoint + "/translate?api-version=3.0"
translate(apiKey, uri, txt, from, to);
os.Exit(0)
}
func readStdin() string {
var in string
s := bufio.NewScanner(os.Stdin)
for s.Scan(){
in += s.Text() + "\n"
}
if l := len(in); l > 0 {
in = in[:l-1]
}
d("Stdin:", in)
return in
}
// translate Translates the string `txt` using `from` as source
// language, `to` as target language. Set `from` empty to let
// Cognitive Services to auto-determine source language
func translate(apiKey, uri, txt, from, to string) {
u, _ := url.Parse(uri)
q := u.Query()
if from != "" {
q.Add("from", from);
}
if to != "" {
q.Add("to", to);
}
d("Text to translate:", txt)
u.RawQuery = q.Encode()
// Create an anonymous struct for your request body and encode it to JSON
body := []struct {
Text string
}{
{Text: txt},
}
b, _ := json.Marshal(body)
d("Request Url:", u)
d("Payload:", string(b))
// Build the HTTP POST request
req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(b))
if err != nil {
log.Fatal(err)
}
// Add required headers to the request
req.Header.Add("Ocp-Apim-Subscription-Key", apiKey)
req.Header.Add("Content-Type", "application/json")
// Call the Translator Text API
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
d("Request Status:", res.Status)
d("Headers:", res.Header)
resp, _ := ioutil.ReadAll(res.Body)
d("Response:", string(resp))
// unmarshall the response
var t []Trans
err = json.Unmarshal([]byte(resp), &t)
if err != nil {
// try to cast as error if response was not a valid request
var e Error
err = json.Unmarshal([]byte(resp), &e)
if err != nil {
log.Fatal(err)
}
log.Fatal("Error: ", e.Data.Message)
}
fmt.Println(t[0].Translations[0].Text)
}
func d(v ...interface{}) {
if !verbose {
return
}
log.Println("[debug]", v)
}