-
Notifications
You must be signed in to change notification settings - Fork 0
/
APIs.go
136 lines (113 loc) · 2.96 KB
/
APIs.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
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"sort"
"strconv"
)
const (
ProtocolDomain = "https://pkuhelper.pku.edu.cn"
GetlistUri = "/services/pkuhole/api.php"
TOKEN = "TODOTODO"
)
var (
cli *http.Client
)
func init() {
cli = &http.Client{}
}
type GetListResp struct {
Code int `json:"code"`
Data []*Post `json:"data"`
}
func GetLists(numOfPages int) ([]*Post, error) {
var result []*Post
for p := 1; p <= numOfPages; p++ {
pageResult, err := GetList(p)
if err != nil {
return result, err
}
result = append(pageResult, result...)
}
// Remove dup
var resultAfterDup []*Post
dupMap := make(map[string]bool)
for _, p := range result {
if _, ok := dupMap[p.Pid]; !ok {
resultAfterDup = append(resultAfterDup, p)
dupMap[p.Pid] = true
}
}
return resultAfterDup, nil
}
func GetList(page int) ([]*Post, error) {
params := url.Values{}
GetListURL, _ := url.Parse(ProtocolDomain + GetlistUri)
params.Set("action", "getlist")
params.Set("PKUHelperAPI", "3.0")
params.Set("user_token", TOKEN)
params.Set("p", strconv.Itoa(page))
GetListURL.RawQuery = params.Encode()
GetListURLRaw := GetListURL.String()
req, err := http.NewRequest("GET", GetListURLRaw, nil)
if err != nil {
return []*Post{}, err
}
resp, err := cli.Do(req)
if err != nil {
return []*Post{}, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []*Post{}, err
}
if resp.StatusCode != 200 {
logger.Printf("error status_code=%v, resp=%v\n", resp.StatusCode, string(body))
return []*Post{}, errors.New("error status code")
}
var getListResp GetListResp
if err := json.Unmarshal(body, &getListResp); err != nil {
panic(err)
}
sort.Sort(PostList(getListResp.Data))
logger.Printf("Received posts from pid=%v, len=%v\n", getListResp.Data[0].Pid, len(getListResp.Data))
return getListResp.Data, nil
}
type GetCommentResp struct {
Data []*Comment `json:"data"`
}
func GetComment(pid string) ([]*Comment, error) {
params := url.Values{}
GetCommentURL, _ := url.Parse(ProtocolDomain + GetlistUri)
params.Set("action", "getcomment")
params.Set("PKUHelperAPI", "3.0")
params.Set("user_token", TOKEN)
params.Set("pid", pid)
GetCommentURL.RawQuery = params.Encode()
GetCommentURLRaw := GetCommentURL.String()
req, err := http.NewRequest("GET", GetCommentURLRaw, nil)
if err != nil {
return []*Comment{}, err
}
resp, err := cli.Do(req)
if err != nil {
return []*Comment{}, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []*Comment{}, err
}
if resp.StatusCode != 200 {
logger.Printf("error status_code=%v, resp=%v\n", resp.StatusCode, string(body))
return []*Comment{}, errors.New("error status code in get comment")
}
var getCommentResp GetCommentResp
if err := json.Unmarshal(body, &getCommentResp); err != nil {
panic(err)
}
logger.Printf("Received comments of pid=%v, num of comments=%v\n", pid, len(getCommentResp.Data))
return getCommentResp.Data, nil
}