dict/dict.go

68 lines
1.2 KiB
Go

package main
import (
"bufio"
"fmt"
"net/http"
"os"
"strings"
)
const (
BASE = "dict.cc/?s="
VAR1 = "var c1Arr = new Array("
VAR2 = "var c2Arr = new Array("
SUFF = ");"
)
func main() {
if len(os.Args) < 2 {
println("usage:", os.Args[0], "word [{de|es|fr|en...}{de|es|fr|en|...}]")
return
}
fromto := ""
if len(os.Args) > 2 {
fromto = os.Args[2] + "."
}
r, err := http.Get("http://" + fromto + BASE + os.Args[1])
if err != nil {
println("error:", err.Error())
return
}
defer r.Body.Close()
byline := bufio.NewScanner(r.Body)
var left, right []string
for byline.Scan() {
line := byline.Text()
if strings.HasPrefix(line, VAR1) {
line = strings.TrimPrefix(line, VAR1)
line = strings.TrimSuffix(line, SUFF)
left = strings.Split(line, ",")
} else if strings.HasPrefix(line, VAR2) {
line = strings.TrimPrefix(line, VAR2)
line = strings.TrimSuffix(line, SUFF)
right = strings.Split(line, ",")
break
}
}
if len(left) != len(right) {
println("error: arrays of different length")
return
}
for i, _ := range left {
a := strings.Trim(right[i], `"`)
b := strings.Trim(left[i], `"`)
if a == "" {
continue
}
fmt.Printf("%25s → %s\n", a, b)
}
}