-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.go
91 lines (74 loc) · 2.05 KB
/
scrape.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
package rld
import (
"encoding/xml"
"io/ioutil"
"net/http"
"strings"
)
func ScrapeLeo(from, to, word string) ([]Section, error) {
var v LeoXML
ret := make([]Section, 0)
url := "http://dict.leo.org/dictQuery/m-vocab/ende/query.xml?" +
"&tolerMode=nof" +
"&lp=" + from + to +
"&lang=" + from +
"&rmWords=off" +
"&rmSearch=on" +
"&search=" + word +
"&resultOrder=basic" +
"&multiwordShowSingle=on" +
"§LenMax=16" +
"&n=1"
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = xml.Unmarshal(body, &v)
if err != nil {
return nil, err
}
/**********************************************************************
* translations
**********************************************************************/
for i := range v.Sectionlist.Section {
s := Section{}
s.Title = v.Sectionlist.Section[i].SctTitle
for j := range v.Sectionlist.Section[i].Entry {
e := v.Sectionlist.Section[i].Entry[j]
entry := make(map[string]string)
for k := range e.Side {
entry[e.Side[k].Lang] = strings.Join(e.Side[k].Words.Word, ", ")
}
s.Entries = append(s.Entries, entry)
}
ret = append(ret, s)
}
/**********************************************************************
* simliar
**********************************************************************/
s := Section{}
s.Title = "similar"
entry := make(map[string]string)
for k := range v.Similar.Side {
entry[v.Similar.Side[k].Lang] = strings.Join(v.Similar.Side[k].Word, ", ")
}
s.Entries = append(s.Entries, entry)
ret = append(ret, s)
/**********************************************************************
* synonym
**********************************************************************/
s = Section{}
s.Title = "synonyms"
entry = make(map[string]string)
for k := range v.Ffsynlist.Side {
entry[v.Ffsynlist.Side[k].Lang] = strings.Join(v.Ffsynlist.Side[k].Word, ", ")
}
s.Entries = append(s.Entries, entry)
ret = append(ret, s)
return ret, nil
}