-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
164 lines (124 loc) · 3.56 KB
/
item.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
package hkn
import (
"encoding/json"
"fmt"
"html"
"net/http"
"net/url"
"sync"
)
// Item represents a Hacker News item
type Item struct {
ID int `json:"id"`
Deleted bool `json:"deleted"`
Type string `json:"type"`
By string `json:"by"`
Time int32 `json:"time"`
Text string `json:"text"`
Dead bool `json:"dead"`
Parent int `json:"parent"`
Poll int `json:"poll"`
Kids []int `json:"kids"`
URL string `json:"url"`
Score int `json:"score"`
Title string `json:"title"`
Parts []int `json:"parts"`
Descendants int `json:"descendants"`
}
// Items represents an array of items
type Items []Item
const (
voteURLRegex = `<a\s+id=['"]%s_%d['"]\s+(?:[^>]*?\s+)?href=['"]([^'"]*)['"]`
commentURLRegex = `<input\s+type=['"]hidden['"]\s+name=['"]hmac['"]\s+(?:[^>]*?\s+)?value=['"]([^'"]*)['"]`
)
// Get an item given an id
func getItem(id int, apiURL string) (Item, error) {
reqURL := fmt.Sprintf("%s/%s/%d", apiURL, "item", id) + jsonSuffix
resp, err := getBody(reqURL)
var item Item
if err != nil {
return item, err
}
err = json.Unmarshal(resp, &item)
return item, err
}
// Get items given a slice of ids
// This function is concurrent and thus does not guarantee order
func getItems(ids []int, apiURL string) (Items, error) {
var (
items Items
wg sync.WaitGroup
)
// Add length of ids to the WaitGroup
wg.Add(len(ids))
for _, id := range ids {
// Spawn a thread for each item
go func(id int, url string) {
defer wg.Done()
item, err := getItem(id, url)
if err != nil {
return
}
items = append(items, item)
}(id, apiURL)
}
// Wait until all threads are done
wg.Wait()
return items, nil
}
// Get the most recent item id
func getMaxItemID(apiURL string) (int, error) {
reqURL := fmt.Sprintf("%s/%s", apiURL, "maxitem") + jsonSuffix
resp, err := getBody(reqURL)
var id int
if err != nil {
return id, err
}
err = json.Unmarshal(resp, &id)
return id, err
}
func vote(id int, cookie *http.Cookie, webURL string, voteType string) (bool, error) {
reqURL := fmt.Sprintf("%s/%s?id=%d", webURL, "item", id)
upvoteRegex := fmt.Sprintf(voteURLRegex, voteType, id)
voteAuth, err := matchRegexFromBody(reqURL, upvoteRegex, cookie)
if err != nil {
return false, err
}
voteURL := fmt.Sprintf("%s/%s", webURL, voteAuth)
unescaped := html.UnescapeString(voteURL)
resp, err := getBodyWithCookie(unescaped, cookie)
if err == nil && resp != nil {
return true, nil
}
return false, err
}
// Upvote an item given an id and a cookie
func upvote(id int, cookie *http.Cookie, webURL string) (bool, error) {
return vote(id, cookie, webURL, "up")
}
// Unvote a comment given an id and a cookie
func unvote(id int, cookie *http.Cookie, webURL string) (bool, error) {
return vote(id, cookie, webURL, "un")
}
// Create a comment on an item given an id and content
func comment(id int, content string, cookie *http.Cookie, webURL string) (bool, error) {
if len(content) == 0 {
return false, ErrEmptyContent
}
reqURL := fmt.Sprintf("%s/%s?id=%d", webURL, "item", id)
commentAuth, err := matchRegexFromBody(reqURL, commentURLRegex, cookie)
if err != nil {
return false, err
}
commentURL := fmt.Sprintf("%s/%s", webURL, "comment")
body := url.Values{}
body.Set("parent", fmt.Sprintf("%d", id))
body.Set("goto", fmt.Sprintf("item?id=%d", id))
body.Set("hmac", commentAuth)
body.Set("text", content)
resp, err := postWithCookie(commentURL, body, cookie)
if err == nil && resp != nil {
return true, nil
}
return false, err
}