|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/PuerkitoBio/goquery" |
| 11 | + "github.com/fatih/color" |
| 12 | + proxier "golang.org/x/net/proxy" |
| 13 | +) |
| 14 | + |
| 15 | +func query(word string) { |
| 16 | + var url string |
| 17 | + var doc *goquery.Document |
| 18 | + isChinese := isChinese(word) |
| 19 | + |
| 20 | + if isChinese { |
| 21 | + url = "http://dict.youdao.com/w/eng/%s" |
| 22 | + } else { |
| 23 | + url = "http://dict.youdao.com/w/%s" |
| 24 | + } |
| 25 | + |
| 26 | + //Check proxy |
| 27 | + if proxy != "" { |
| 28 | + client := &http.Client{} |
| 29 | + dialer, err := proxier.SOCKS5("tcp", proxy, nil, proxier.Direct) |
| 30 | + |
| 31 | + if err != nil { |
| 32 | + color.Red("Can't connect to the proxy: %s", err) |
| 33 | + os.Exit(1) |
| 34 | + } |
| 35 | + |
| 36 | + httpTransport := &http.Transport{} |
| 37 | + client.Transport = httpTransport |
| 38 | + httpTransport.Dial = dialer.Dial |
| 39 | + |
| 40 | + resp, err := client.Get(fmt.Sprintf(url, word)) |
| 41 | + |
| 42 | + if err != nil { |
| 43 | + color.Red("Query failed with err: %s", err.Error()) |
| 44 | + os.Exit(1) |
| 45 | + } |
| 46 | + |
| 47 | + doc, _ = goquery.NewDocumentFromResponse(resp) |
| 48 | + |
| 49 | + } else { |
| 50 | + var err error |
| 51 | + doc, err = goquery.NewDocument(fmt.Sprintf(url, word)) |
| 52 | + if err != nil { |
| 53 | + log.Fatal(err) |
| 54 | + os.Exit(1) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if isChinese { |
| 59 | + // Find the result |
| 60 | + fmt.Println() |
| 61 | + doc.Find(".trans-container > ul > p > span.contentTitle").Each(func(i int, s *goquery.Selection) { |
| 62 | + color.Green(" %s", s.Find(".search-js").Text()) |
| 63 | + }) |
| 64 | + } else { |
| 65 | + // Find the result |
| 66 | + result := doc.Find("div#phrsListTab > div.trans-container > ul").Text() |
| 67 | + color.Green(result) |
| 68 | + } |
| 69 | + |
| 70 | + // Show examples |
| 71 | + sentences := getSentences(doc, isChinese) |
| 72 | + if len(sentences) > 0 { |
| 73 | + fmt.Println() |
| 74 | + for _, sentence := range sentences { |
| 75 | + color.Green(" %s", sentence[0]) |
| 76 | + color.Magenta(" %s", sentence[1]) |
| 77 | + } |
| 78 | + fmt.Println() |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func getSentences(doc *goquery.Document, isChinese bool) [][]string { |
| 83 | + result := [][]string{} |
| 84 | + doc.Find("#bilingual ul li").Each(func(_ int, s *goquery.Selection) { |
| 85 | + r := []string{} |
| 86 | + s.Children().Each(func(ii int, ss *goquery.Selection) { |
| 87 | + // Ignore source |
| 88 | + if ii == 2 { |
| 89 | + return |
| 90 | + } |
| 91 | + var sentence string |
| 92 | + ss.Children().Each(func(iii int, sss *goquery.Selection) { |
| 93 | + if text := strings.TrimSpace(sss.Text()); text != "" { |
| 94 | + addSpace := (ii == 1 && isChinese) || (ii == 0 && !isChinese) |
| 95 | + if addSpace && iii != 0 && text != "." { |
| 96 | + text = " " + text |
| 97 | + } |
| 98 | + sentence += text |
| 99 | + } |
| 100 | + }) |
| 101 | + r = append(r, sentence) |
| 102 | + }) |
| 103 | + if len(r) == 2 { |
| 104 | + result = append(result, r) |
| 105 | + } |
| 106 | + }) |
| 107 | + return result |
| 108 | +} |
0 commit comments