-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider_wolfram.go
164 lines (142 loc) · 3.31 KB
/
provider_wolfram.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
157
158
159
160
161
162
163
164
package know
import (
"encoding/base64"
"encoding/xml"
"errors"
"html"
"io"
"net/http"
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
)
type WolframProvider struct {
ApiKey string
}
func NewWolframProvider() *WolframProvider {
return &WolframProvider{}
}
func (p *WolframProvider) SetApiKey(key string) {
p.ApiKey = key
}
func (p *WolframProvider) Name() string {
if p.ApiKey != "" {
return "Wolfram API"
}
return "Wolfram"
}
type apiQueryResult struct {
Success bool `xml:"success,attr"`
HasError bool `xml:"error,attr"`
Error string `xml:"error>msg"`
Pods []apiPod `xml:"pod"`
}
type apiPod struct {
Title string `xml:"title,attr"`
SubPods []apiSubPod `xml:"subpod"`
}
type apiSubPod struct {
Title string `xml:"attr"`
Plaintext string `xml:"plaintext"`
}
func (p *WolframProvider) Ask(question string) (*Answer, error) {
if p.ApiKey == "" {
return p.askPublic(question)
}
u, err := url.Parse("https://api.wolframalpha.com/v2/query")
if err != nil {
return nil, err
}
v := url.Values{}
v.Set("input", question)
v.Set("appid", p.ApiKey)
u.RawQuery = v.Encode()
resp, err := http.Get(u.String())
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New("Unexpected status: " + resp.Status)
}
defer resp.Body.Close()
dec := xml.NewDecoder(resp.Body)
result := apiQueryResult{}
if err := dec.Decode(&result); err != nil {
return nil, err
}
if result.HasError {
return nil, errors.New(result.Error)
}
ans := &Answer{}
for _, pod := range result.Pods {
for _, s := range pod.SubPods {
switch {
case strings.Contains(pod.Title, "Input"):
ans.Question = s.Plaintext
case strings.Contains(pod.Title, "Result"):
ans.Answer = s.Plaintext
case ans.Answer == "":
ans.Answer = s.Plaintext
}
}
}
return ans, nil
}
func (p *WolframProvider) askPublic(question string) (*Answer, error) {
u, err := url.Parse("https://www.wolframalpha.com/input/")
if err != nil {
return nil, err
}
v := url.Values{}
v.Set("i", question)
u.RawQuery = v.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0")
if err != nil {
return nil, err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New("Unexpected status: " + resp.Status)
}
defer resp.Body.Close()
ans, err := p.parse(resp.Body)
if ans != nil && ans.Answer == "" {
return nil, err
}
return ans, err
}
func (p *WolframProvider) parse(body io.Reader) (*Answer, error) {
doc, err := goquery.NewDocumentFromReader(body)
if err != nil {
return nil, err
}
ans := &Answer{}
pods := doc.Find("#answers section.pod")
for i := 0; i < pods.Length(); i++ {
pod := pods.Eq(i)
title := strings.TrimSpace(pod.Find("header h2").Text())
plain, ok := pod.Find(".sub[data-s]").Attr("data-s")
if !ok {
continue
}
b, err := base64.StdEncoding.DecodeString(plain)
if err != nil {
return nil, err
}
value := html.UnescapeString(string(b))
switch {
case strings.Contains(title, "Input"):
ans.Question = value
case strings.Contains(title, "Result"):
ans.Answer = value
case ans.Answer == "":
ans.Answer = value
}
}
return ans, nil
}