/
content.go
91 lines (80 loc) · 3.03 KB
/
content.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
package ybtools
//
// Yapperbot Tools, the internal system bits for Yapperbot and co.
// Copyright (C) 2020 Naypta
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
import (
"log"
"github.com/antonholmquist/jason"
)
// GetContentFromPage turns a *jason.Object for a Page into the main slot content,
// and/or an applicable error.
//
// Yes, there is a function to do this already in the library (GetPagesByName).
// No, I don't want to use it. Why? We've already got the page content here -
// making another request to get it again is wasteful when we could just locally
// parse what we already have.
func GetContentFromPage(page *jason.Object) (content string, err error) {
rev, err := page.GetObjectArray("revisions")
if err != nil {
log.Println("Failed to get revisions from page, erroring getContentFromPage. Error was ", err)
return
}
content, err = rev[0].GetString("slots", "main", "content")
if err != nil {
log.Println("Failed to get main slot content from page, erroring getContentFromPage. Error was", err)
return
}
return
}
// GetPagesFromQuery takes a query and returns an array of Pages.
// Convenience wrapper for GetThingFromQuery.
func GetPagesFromQuery(resp *jason.Object) []*jason.Object {
return GetThingFromQuery(resp, "pages")
}
// GetThingFromQuery takes a query and a key that's being looked for,
// and returns the inner thing array.
func GetThingFromQuery(resp *jason.Object, thing string) []*jason.Object {
query, err := resp.GetObject("query")
if err != nil {
switch err.(type) {
case jason.KeyNotFoundError:
// no query means no results
return []*jason.Object{}
default:
panic(err)
}
}
pages, err := query.GetObjectArray(thing)
if err != nil {
panic(err)
}
return pages
}
// GetCategorisationTimestampFromPage takes a page,
// and gets the timestamp at which the page was categorised.
// All the errors in this function are Fatal, because frankly,
// if something's gone wrong with the timestamp reading, we're not really
// going to be able to run the algorithm correctly anyway.
func GetCategorisationTimestampFromPage(page *jason.Object, category string) (timestamp string) {
itemCategories, err := page.GetObjectArray("categories")
if err != nil {
PanicErr("Failed to get categories with error message ", err)
}
relevantCategory := itemCategories[0]
timestamp, err = relevantCategory.GetString("timestamp")
if err != nil {
PanicErr("Failed to get categorisation timestamp with error message ", err)
}
return
}