-
Notifications
You must be signed in to change notification settings - Fork 4
/
gowikidata.go
312 lines (274 loc) · 9.42 KB
/
gowikidata.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
package gowikidata
import (
"errors"
"fmt"
"strconv"
"github.com/Navid2zp/easyreq"
)
const (
wikipediaQueryURL = "https://en.wikipedia.org/w/api.php?action=query&prop=pageprops&titles=%s&format=json"
wikiDataAPIURL = "https://www.wikidata.org/w/api.php?action=%s&format=json"
imageResizerURL = "https://commons.wikimedia.org/w/thumb.php?width=%d&f=%s"
)
// GetPageItem returns Wikipedia page item ID
func GetPageItem(slug string) (string, error) {
url := fmt.Sprintf(wikipediaQueryURL, slug)
wikipediaQuery := WikiPediaQuery{}
res, err := easyreq.Make("get", url, nil, "", "json", &wikipediaQuery, nil)
if err != nil {
return "", err
}
if res.StatusCode() != 200 {
return "", fmt.Errorf("request failed with status code %d", res.StatusCode())
}
item := ""
// Get the first item
for _, value := range wikipediaQuery.Query.Pages {
item = value.PageProps.WikiBaseItem
break
}
return item, nil
}
// NewGetEntities creates and returns a new WikiDataGetEntitiesRequest
// Use this function to initialize a request.
// It will return a pointer to WikiDataGetEntitiesRequest.
// You can make configurations on response.
func NewGetEntities(ids []string) (*WikiDataGetEntitiesRequest, error) {
if len(ids) < 1 {
return nil, errors.New("no ids provided")
}
req := WikiDataGetEntitiesRequest{
URL: fmt.Sprintf(wikiDataAPIURL, "wbgetentities"),
}
req.setParam("ids", &ids)
return &req, nil
}
// SetSites sets sites parameter for entities request
func (r *WikiDataGetEntitiesRequest) SetSites(sites []string) *WikiDataGetEntitiesRequest {
r.setParam("sites", &sites)
return r
}
// SetTitles sets titles parameter for entities request
func (r *WikiDataGetEntitiesRequest) SetTitles(titles []string) *WikiDataGetEntitiesRequest {
r.setParam("titles", &titles)
return r
}
// SetRedirects sets redirects parameter for entities request
func (r *WikiDataGetEntitiesRequest) SetRedirects(redirect bool) *WikiDataGetEntitiesRequest {
redirectString := "yes"
if !redirect {
redirectString = "no"
}
r.URL += "&redirects=" + redirectString
return r
}
// SetProps sets props parameter for entities request
// Default: info|sitelinks|aliases|labels|descriptions|claims|datatype
func (r *WikiDataGetEntitiesRequest) SetProps(props []string) *WikiDataGetEntitiesRequest {
r.setParam("props", &props)
return r
}
// SetLanguages sets languages parameter for entities request
func (r *WikiDataGetEntitiesRequest) SetLanguages(languages []string) *WikiDataGetEntitiesRequest {
r.setParam("languages", &languages)
return r
}
// SetLanguageFallback sets languagefallback parameter for entities request
func (r *WikiDataGetEntitiesRequest) SetLanguageFallback(fallback bool) *WikiDataGetEntitiesRequest {
if fallback {
r.URL += "&languagefallback="
}
return r
}
// SetNormalize sets normalize parameter for entities request
func (r *WikiDataGetEntitiesRequest) SetNormalize(normalize bool) *WikiDataGetEntitiesRequest {
if normalize {
r.URL += "&normalize="
}
return r
}
// SetSiteFilter sets sitefilter parameter for entities request
func (r *WikiDataGetEntitiesRequest) SetSiteFilter(sites []string) *WikiDataGetEntitiesRequest {
r.setParam("sitefilter", &sites)
return r
}
// Get makes a entities request and returns the response or an error
// Call this function after you finished configuring the request.
// It will send the request and unmarshales the response.
func (r *WikiDataGetEntitiesRequest) Get() (*map[string]Entity, error) {
responseData := GetEntitiesResponse{}
res, err := easyreq.Make("GET", r.URL, nil, "", "json", &responseData, nil)
if err != nil {
return nil, err
}
if res.StatusCode() != 200 {
return nil, fmt.Errorf("request failed with status code %d", res.StatusCode())
}
return &responseData.Entities, nil
}
// NewGetClaims creates a new claim request for the given entity or claim
// WikiData action: wbgetclaims
// WikiData API page: https://www.wikidata.org/w/api.php?action=help&modules=wbgetclaims
// Either entity or claim must be provided
func NewGetClaims(entity, claim string) (*WikiDataGetClaimsRequest, error) {
if entity == "" && claim == "" {
return nil, errors.New("either entity or claim must be provided")
}
req := WikiDataGetClaimsRequest{
URL: fmt.Sprintf(wikiDataAPIURL, "wbgetclaims"),
}
if entity != "" {
req.setParam("entity", &[]string{entity})
} else {
req.setParam("claim", &[]string{claim})
}
return &req, nil
}
// NewGetClaims creates a new claim request for an entity
func (e *Entity) NewGetClaims() (*WikiDataGetClaimsRequest, error) {
return NewGetClaims(e.ID, "")
}
// SetProperty sets property parameter for claims request
func (r *WikiDataGetClaimsRequest) SetProperty(property string) *WikiDataGetClaimsRequest {
r.setParam("property", &[]string{property})
return r
}
// SetRank sets rank parameter for claims request
// One of the following values: deprecated, normal, preferred
func (r *WikiDataGetClaimsRequest) SetRank(rank string) *WikiDataGetClaimsRequest {
r.setParam("rank", &[]string{rank})
return r
}
// SetProps sets props parameter for claims request
// Default: references
func (r *WikiDataGetClaimsRequest) SetProps(props []string) *WikiDataGetClaimsRequest {
r.setParam("props", &props)
return r
}
// Get creates a new request for claims and returns the response or an error
func (r *WikiDataGetClaimsRequest) Get() (*map[string][]Claim, error) {
responseData := GetClaimsResponse{}
res, err := easyreq.Make("GET", r.URL, nil, "", "json", &responseData, nil)
if err != nil {
return nil, err
}
if res.StatusCode() != 200 {
return nil, fmt.Errorf("request failed with status code %d", res.StatusCode())
}
return &responseData.Claims, nil
}
// GetAvailableBadges returns a pointer to a list of strings.
// WikiData action: wbavailablebadges
// WikiData API page: https://www.wikidata.org/w/api.php?action=help&modules=wbavailablebadges
func GetAvailableBadges() ([]string, error) {
var data struct{Badges []string}
url := fmt.Sprintf(wikiDataAPIURL, "wbavailablebadges")
res, err := easyreq.Make("GET", url, nil, "", "json", &data, nil)
if err != nil {
return nil, err
}
if res.StatusCode() != 200 {
return nil, fmt.Errorf("request failed with status code %d", res.StatusCode())
}
return data.Badges, nil
}
// NewSearch creates a new request for entities search and returns response or an error
// Create a request
// Both search and language are required
// WikiData action: wbsearchentities
// WikiData API page: https://www.wikidata.org/w/api.php?action=help&modules=wbsearchentities
func NewSearch(search, language string) (*WikiDataSearchEntitiesRequest, error) {
req := WikiDataSearchEntitiesRequest{
URL: fmt.Sprintf(wikiDataAPIURL, "wbsearchentities"),
Language: language,
// default api value
Limit: 7,
}
req.setParam("search", &[]string{search})
req.setParam("language", &[]string{language})
return &req, nil
}
// SetLimit sets limit parameter
// Default: 7
func (r *WikiDataSearchEntitiesRequest) SetLimit(limit int) *WikiDataSearchEntitiesRequest {
r.Limit = limit
r.setParam("limit", &[]string{strconv.Itoa(limit)})
return r
}
// SetStrictLanguage sets strictlanguage parameter
func (r *WikiDataSearchEntitiesRequest) SetStrictLanguage(strictLanguage bool) *WikiDataSearchEntitiesRequest {
if strictLanguage {
r.URL += "&strictlanguage="
}
return r
}
// SetType sets type parameter
// One of the following values: item, property, lexeme, form, sense
// Default: item
func (r *WikiDataSearchEntitiesRequest) SetType(t string) *WikiDataSearchEntitiesRequest {
r.setParam("type", &[]string{t})
return r
}
// SetProps sets props parameter
// Default: url
func (r *WikiDataSearchEntitiesRequest) SetProps(props []string) *WikiDataSearchEntitiesRequest {
r.setParam("props", &props)
return r
}
// SetContinue sets continue parameter
// Default: 0
func (r *WikiDataSearchEntitiesRequest) SetContinue(c int) *WikiDataSearchEntitiesRequest {
r.setParam("continue", &[]string{strconv.Itoa(c)})
return r
}
// Get makes a entity search request and returns the response or an error
func (r *WikiDataSearchEntitiesRequest) Get() (*SearchEntitiesResponse, error) {
responseData := SearchEntitiesResponse{}
res, err := easyreq.Make("GET", r.URL, nil, "", "json", &responseData, nil)
if err != nil {
return nil, err
}
if res.StatusCode() != 200 {
return nil, fmt.Errorf("request failed with status code %d", res.StatusCode())
}
responseData.SearchRequest = *r
return &responseData, nil
}
// Next page of results for search
// Will use the same configurations as previous request
func (r *SearchEntitiesResponse) Next() (*SearchEntitiesResponse, error) {
res, err := NewSearch(r.SearchRequest.Search, r.SearchRequest.Language)
if err != nil {
return nil, err
}
if r.SearchRequest.Limit > 0 {
res.SetLimit(r.SearchRequest.Limit)
}
if r.SearchRequest.Type != "" {
res.SetType(r.SearchRequest.Type)
}
if len(r.SearchRequest.Props) > 0 {
res.SetProps(r.SearchRequest.Props)
}
if r.SearchRequest.StrictLanguage {
res.SetStrictLanguage(true)
}
res.SetContinue(r.CurrentContinue + r.SearchRequest.Limit)
response, err := res.Get()
return response, err
}
// ImageResizer returns the url for resizing a wikimedia image to the given size
func ImageResizer(imageName string, size int) string {
return fmt.Sprintf(imageResizerURL, size, imageName)
}
func createParam(param string, values []string) string {
newString := "&" + param + "="
valuesLen := len(values)
for i, value := range values {
newString += value
if i+1 < valuesLen {
newString += "|"
}
}
return newString
}