This repository has been archived by the owner on Jan 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
linguee.go
156 lines (122 loc) · 3.27 KB
/
linguee.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
package main
import (
"fmt"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/djimenez/iconv-go"
alfred "github.com/pascalw/go-alfred"
)
type Request struct {
URL string
Lang string
Query string
Result string
}
type Translation struct {
Meaning string
Href string
Phrase []string
}
func main() {
base := "http://www.linguee.com/%s/search?qe=%s&source=auto"
args := params(base, os.Args)
selection := filterDocument(transformToDocument(request(args)))
response := alfred.NewResponse()
match := false
for index, translation := range parseTranslations(selection) {
if strings.ToLower(args.Query) == strings.ToLower(translation.Meaning) {
match = true
}
response.AddItem(&alfred.AlfredResponseItem{
Valid: true,
Uid: strconv.Itoa(index),
Title: translation.Meaning,
Subtitle: strings.Join(translation.Phrase, ", "),
Arg: result(args, translation),
})
}
// Add query itself to give the ability for a direct search if there is no match
if match == false {
origin, _ := url.QueryUnescape(args.Query)
response.AddItem(&alfred.AlfredResponseItem{
Valid: true,
Uid: "origin",
Title: origin,
Arg: fmt.Sprintf("http://www.linguee.com/%s/search?source=auto&query=%s", args.Lang, args.Query),
})
}
response.Print()
}
func params(base string, args []string) Request {
if len(args) == 1 {
log.Fatal("Missing Arguments")
}
query := args[1]
lang := "deutsch-englisch"
result := "url"
if len(args) > 2 {
lang = args[2]
}
if len(args) > 3 {
result = args[3]
}
return Request{base, lang, url.QueryEscape(query), result}
}
func result(req Request, translation Translation) string {
switch req.Result {
case "meaning":
return translation.Meaning
case "phrase":
return strings.Join(translation.Phrase, ", ")
default:
return fmt.Sprintf("http://www.linguee.com%s", translation.Href)
}
}
func request(req Request) *iconv.Reader {
// Http request
res, err := http.Get(fmt.Sprintf(req.URL, req.Lang, req.Query))
if err != nil {
log.Fatal(err)
}
// charset conversion
body, err := iconv.NewReader(res.Body, "iso-8859-15", "utf-8")
if err != nil {
log.Fatal(err)
}
return body
}
func transformToDocument(reader *iconv.Reader) *goquery.Document {
doc, err := goquery.NewDocumentFromReader(reader)
if err != nil {
log.Fatal(err)
}
return doc
}
func filterDocument(doc *goquery.Document) *goquery.Selection {
doc.Find(".wordtype, .sep, .grammar_info").ReplaceWithHtml("")
return doc.Find(".autocompletion_item")
}
func parseTranslations(elements *goquery.Selection) (results []Translation) {
elements.Each(func(index int, element *goquery.Selection) {
results = append(results, Translation{parseMeaning(element), parseHref(element), parsePhrase(element)})
})
return
}
func parseMeaning(selection *goquery.Selection) string {
return strings.TrimSpace(selection.Find(".main_item").Text())
}
func parseHref(selection *goquery.Selection) string {
href, _ := selection.Find(".main_item").Attr("href")
return href
}
func parsePhrase(selection *goquery.Selection) (result []string) {
selection.Find(".translation_item").Each(func(index int, meaning *goquery.Selection) {
result = append(result, strings.TrimSpace(meaning.Text()))
})
return
}