/
catinfo.go
57 lines (55 loc) · 1.55 KB
/
catinfo.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
package main
import (
mwclient "cgt.name/pkg/go-mwclient"
"cgt.name/pkg/go-mwclient/params"
"github.com/garyhouston/takenwith/mwlib"
)
// Examine the specified categories in the Wiki. For each one that actually
// exists, return the number of files that it contains. The result arrays
// give category names (in arbitrary order) and the corresponding count,
// and will have fewer entries than the input array if some categories
// were duplicated or didn't exist.
func catNumFiles(categories []string, client *mwclient.Client) ([]string, []int32) {
params := params.Values{
"action": "query",
"titles": mwlib.MakeTitleString(categories),
"prop": "categoryinfo",
}
json, err := client.Post(params) // Get may fail on long queries.
if err != nil {
panic(err)
}
pages, err := json.GetObjectArray("query", "pages")
if err != nil {
panic(err)
}
var resultCats = make([]string, len(pages))
var resultCounts = make([]int32, len(pages))
found := 0
for idx, _ := range pages {
pageObj, err := pages[idx].Object()
if err != nil {
panic(err)
}
missing, err := pageObj.GetBoolean("missing")
if err == nil && missing {
continue
}
resultCats[found], err = pageObj.GetString("title")
if err != nil {
panic(err)
}
info, err := pageObj.GetObject("categoryinfo")
// An error here means that the category is probably
// empty, so just leave count at 0.
if err == nil {
files, err := info.GetInt64("files")
if err != nil {
panic(err)
}
resultCounts[found] = int32(files)
}
found++
}
return resultCats, resultCounts
}