-
Notifications
You must be signed in to change notification settings - Fork 11
/
gowiki.go
344 lines (282 loc) · 7.94 KB
/
gowiki.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package gowiki
import (
"errors"
"fmt"
"strconv"
"time"
"github.com/trietmn/go-wiki/cache"
"github.com/trietmn/go-wiki/page"
"github.com/trietmn/go-wiki/utils"
)
/*
Change the user-agent that you use to crawl Wikipedia data
*/
func SetUserAgent(user string) {
utils.UserAgent = user
}
/*
Change the language of the API being requested.
Set `prefix` to one of the two letter prefixes found on the
list of all Wikipedia <http://meta.wikimedia.org/wiki/List_of_Wikipedias>.
Then clear all of the cache
*/
func SetLanguage(lang string) {
utils.WikiLanguage = lang
utils.Cache.Clear()
}
/*
Change the language of the API being requested.
Set `prefix` to one of the two letter prefixes found on the
list of all Wikipedia <http://meta.wikimedia.org/wiki/List_of_Wikipedias>.
Then clear all of the cache
*/
func SetURL(url string) {
utils.WikiURL = url
utils.Cache.Clear()
}
/*
Change the max number of the request responses stored in the Cache
*/
func SetMaxCacheMemory(n int) {
cache.MaxCacheMemory = n
}
/*
Change the max duration of the request responses exist in the Cache
*/
func SetCacheDuration(x time.Duration) {
cache.CacheExpiration = x
}
/*
List all the currently supported language prefixes (usually ISO language code).
Can be inputted to `set_lang` to change the Mediawiki that `wikipedia` requests results from.
Returns: Map of <prefix>: <local_lang_name> pairs.
*/
func GetAvailableLanguage() (map[string]string, error) {
args := map[string]string{
"action": "query",
"meta": "siteinfo",
"siprop": "languages",
}
res, err := utils.WikiRequester(args)
if err != nil {
return map[string]string{}, err
}
if res.Error.Code != "" {
return map[string]string{}, errors.New(res.Error.Info)
}
result := map[string]string{}
for _, v := range res.Query.Language {
result[v["code"]] = v["*"]
}
return result, nil
}
/*
Do a Wikipedia search for `query`.
Keyword arguments:
* _input: The query used to search Ex:"Who invented the lightbulb"
* limit: The maxmimum number of results returned. Use -1 to use default setting
* suggest: If True, return results and suggestion (if any) in a tuple. Fasle is defalt
Return:
* A slice of Wikipedia titles from the search engine
* Suggestion if `suggest` is being set True
* Error
*/
func Search(_input string, limit int, suggest bool) ([]string, string, error) {
if limit < 0 {
limit = 10
}
args := map[string]string{
"action": "query",
"list": "search",
"srprop": "",
"srlimit": strconv.Itoa(limit),
"srsearch": _input,
}
res, err := utils.WikiRequester(args)
if err != nil {
return []string{}, "", err
}
if res.Error.Code != "" {
return []string{}, "", errors.New(res.Error.Info)
}
result := make([]string, 0, len(res.Query.Search))
for _, s := range res.Query.Search {
result = append(result, s.Title)
}
if suggest {
return result, res.Query.SearchInfo.Suggestion, nil
}
return result, "", nil
}
/*
Get a Wikipedia search suggestion for `_input`.
Returns a string or "" if no suggestion was found.
*/
func Suggest(_input string) (string, error) {
args := map[string]string{
"action": "query",
"list": "search",
"srlimit": "1",
"srprop": "",
"srinfo": "suggestion",
"srsearch": _input,
}
res, err := utils.WikiRequester(args)
if err != nil {
return "", err
}
if res.Error.Code != "" {
return "", errors.New(res.Error.Info)
}
return res.Query.SearchInfo.Suggestion, nil
}
/*
Do a wikipedia geo search for `latitude` and `longitude`
using HTTP API described in http://www.mediawiki.org/wiki/Extension:GeoData
Arguments:
* latitude: Latitude of the searched place
* longitude: longitude of the searched place
* title(optional): The title of an article to search for. Use "" to use the default setting
* limit(optional): The maximum number of results returned. Use -1 to use the default setting
* radius(optional): Search radius in meters. The value must be between 10 and 10000. Use -1 to use the default setting
Return:
* A slice of geosearch titles
* Error
*/
func GeoSearch(latitude float32, longitude float32, radius float32, title string, limit int) ([]string, error) {
if radius <= 0 {
radius = 1000
}
if limit < 0 {
limit = 10
}
args := map[string]string{
"action": "query",
"list": "geosearch",
"gsradius": fmt.Sprintf("%v", radius),
"gscoord": fmt.Sprintf("%v|%v", latitude, longitude),
"gslimit": strconv.Itoa(limit),
}
if title != "" {
args["titles"] = title
}
res, err := utils.WikiRequester(args)
if err != nil {
return []string{}, err
}
if res.Error.Code != "" {
return []string{}, errors.New(res.Error.Info)
}
result := make([]string, 0, len(res.Query.GeoSearch))
if len(res.Query.Page) > 0 {
for k, v := range res.Query.Page {
if k != "-1" {
result = append(result, v.Title)
}
}
} else {
for _, s := range res.Query.GeoSearch {
result = append(result, s.Title)
}
}
return result, nil
}
/*
Get a list of random Wikipedia article titles.
**Note:: Random only gets articles from namespace 0, meaning no Category, User talk, or other meta-Wikipedia pages.
Keyword arguments:
* limit: The number of random pages returned (max of 10)
*/
func GetRandom(limit int) ([]string, error) {
if limit < 0 {
limit = 5
}
args := map[string]string{
"action": "query",
"list": "random",
"rnnamespace": "0",
"rnlimit": strconv.Itoa(limit),
}
res, err := utils.WikiRequester(args)
if err != nil {
return []string{}, err
}
if res.Error.Code != "" {
return []string{}, errors.New(res.Error.Info)
}
result := make([]string, 0, len(res.Query.Random))
for _, s := range res.Query.Random {
result = append(result, s.Title)
}
return result, nil
}
/*
Get a WikipediaPage object for the page with title `title` or the pageid `pageid` (mutually exclusive).
Keyword arguments:
* title: The title of the page to load
* pageid: The numeric pageid of the page to load
* auto_suggest: Let Wikipedia find a valid page title for the query. Default should be False
* redirect: Allow redirection. Default should be True
Return:
* A WikipediaPage object
* Error
*/
func GetPage(title string, pageid int, suggest bool, redirect bool) (page.WikipediaPage, error) {
if pageid >= 0 {
return page.MakeWikipediaPage(pageid, "", "", redirect)
}
if title != "" {
titles, suggestion, err := Search(title, 1, suggest)
if err != nil {
return page.MakeWikipediaPage(-1, title, "", redirect)
}
var pagetitle string
if suggest {
pagetitle = suggestion
}
if pagetitle == "" && len(titles) > 0 {
pagetitle = titles[0]
}
if pagetitle == "" {
return page.WikipediaPage{}, errors.New("page not exist")
}
return page.MakeWikipediaPage(-1, pagetitle, "", redirect)
}
return page.WikipediaPage{}, errors.New("must have either title or pageid to work")
}
/*
Return a string summary of a page
**Note:: This is a convenience wrapper - auto_suggest and redirect are enabled by default\
Keyword arguments:
* title: Title of the page you want to get the summary
* numsentence: If set, return the first `numsentence` sentences (can be no greater than 10).
* numchar: If set, return only the first `numchar` characters (actual text returned may be slightly longer).
* auto_suggest: Let Wikipedia find a valid page title for the query. Default is False
* redirect: Allow redirection without raising RedirectError. Defalt is True
*/
func Summary(title string, numsentence int, numchar int, suggest bool, redirect bool) (string, error) {
page, err := GetPage(title, -1, suggest, redirect)
if err != nil {
return "", err
}
args := map[string]string{
"prop": "extracts",
"explaintext": "",
"titles": page.Title,
}
if numsentence > 0 {
args["exsentences"] = strconv.Itoa(numsentence)
} else if numchar > 0 {
args["exchars"] = strconv.Itoa(numchar)
} else {
args["exintro"] = ""
}
res, err := utils.WikiRequester(args)
if err != nil {
return "", err
}
if res.Error.Code != "" {
return "", errors.New(res.Error.Info)
}
return res.Query.Page[strconv.Itoa(page.PageID)].Extract, nil
}