/
categories.go
71 lines (68 loc) · 1.69 KB
/
categories.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
package main
import (
mwclient "cgt.name/pkg/go-mwclient"
"cgt.name/pkg/go-mwclient/params"
"fmt"
"github.com/antonholmquist/jason"
"github.com/garyhouston/takenwith/mwlib"
)
func requestCategories(page string, client *mwclient.Client) *jason.Object {
params := params.Values{
"action": "query",
"titles": page,
"prop": "categories",
"cllimit": "max",
}
json, err := client.Get(params)
if err != nil {
panic(err)
}
return json
}
// Given an array of page titles, return a mapping from page title to the array
// of categories which the page is a member of.
// If the page doesn't exist, or has no categories, it will map to nil.
func getPageCategories(pages []string, client *mwclient.Client) map[string][]string {
params := params.Values{
"action": "query",
"titles": mwlib.MakeTitleString(pages),
"prop": "categories",
"cllimit": "max",
"continue": "",
}
json, err := client.Post(params) // Get may fail on long queries.
if err != nil {
fmt.Println(params)
panic(err)
}
pagesArray, err := json.GetObjectArray("query", "pages")
if err != nil {
panic(err)
}
result := make(map[string][]string)
for _, page := range pagesArray {
pageObj, err := page.Object()
if err != nil {
panic(err)
}
title, err := pageObj.GetString("title")
if err != nil {
panic(err)
}
categories, err := pageObj.GetObjectArray("categories")
if err != nil {
// Presumably the page is deleted or has no categories.
result[title] = nil
continue
}
catArray := make([]string, len(categories))
for i := range categories {
catArray[i], err = categories[i].GetString("title")
if err != nil {
panic(err)
}
}
result[title] = catArray
}
return result
}