commit a3f3e65049de3068f69f8de699d7e0e4a6dcc036 Author: Oezguer Kesim Date: Tue Jul 28 10:09:26 2015 +0200 initial, fertige Loesung diff --git a/dict.go b/dict.go new file mode 100644 index 0000000..e1b05a1 --- /dev/null +++ b/dict.go @@ -0,0 +1,67 @@ +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) + } +}