-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
228 lines (187 loc) · 4.93 KB
/
main.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
)
func main() {
tfm := template.FuncMap{
"ts": func(u int64) string {
return time.Unix(u, 0).Format(time.RFC822)
},
"spacer": func(l uint) uint {
return 20 * l
},
}
tmpl, err := template.New("who-is-hiring.tmpl.html").Funcs(tfm).ParseFiles("who-is-hiring.tmpl.html")
if err != nil {
log.Fatalf("Error parsing template: %v", err)
}
fmt.Println("Getting story ID...")
storyID, err := getStoryID()
if err != nil {
log.Fatalf("Error getting story ID: %v", err)
}
fmt.Printf("Got story ID (%d).\n", storyID)
fmt.Println("Getting story...")
story, err := getStory(storyID)
if err != nil {
log.Fatalf("Error getting story %d: %v", storyID, err)
}
fmt.Println("Got story.")
const filename string = "index.html"
f, err := os.Create(filename)
if err != nil {
log.Fatalf("Error creating %s: %v", filename, err)
}
defer f.Close()
if err := tmpl.Execute(f, story); err != nil {
log.Fatalf("Error executing template: %v", err)
}
}
const searchURL string = "https://hn.algolia.com/api/v1/search?query=%%22ask%%20hn:%%20who%%20is%%20hiring%%3F%%20(%s)%%22"
type algoliaRes struct {
NbHits int `json:"nbHits"`
Hits []struct {
StoryID uint `json:"story_id"`
}
}
func getStoryID() (uint, error) {
now := time.Now()
curMon := now.Format("January 2006")
lastMon := now.AddDate(0, -1, 0).Format("January 2006")
for _, mon := range []string{curMon, lastMon} {
res, err := http.Get(fmt.Sprintf(searchURL, url.QueryEscape(mon)))
if err != nil {
return 0, fmt.Errorf("error searching Algolia for article in %s: %w", mon, err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return 0, fmt.Errorf("error reading Algolia res body for article in %s: %w", mon, err)
}
var data algoliaRes
if err := json.Unmarshal(body, &data); err != nil {
return 0, fmt.Errorf("error unmarshaling Algolia res data for article in %s: %w", mon, err)
}
if data.NbHits > 0 {
return data.Hits[0].StoryID, nil
}
}
return 0, fmt.Errorf("could not find story for %s or %s", curMon, lastMon)
}
const itemURL string = "https://hacker-news.firebaseio.com/v0/item/%d.json?print=pretty"
func getItem(id uint) ([]byte, error) {
const maxTries int = 3 // Because the API can be a bit flaky, we will try a few times.
for i := 0; i < maxTries; i++ {
res, err := http.Get(fmt.Sprintf(itemURL, id))
if err != nil {
log.Printf("error getting item %d from Firebase API: %v", id, err)
continue
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
log.Printf("error reading item %d Firebase API res body: %v", id, err)
continue
}
return body, nil
}
return nil, fmt.Errorf("could not get item %d after %d tries", id, maxTries)
}
type comment struct {
ID uint `json:"id"`
Time int64
Text template.HTML
By string
Dead bool
Deleted bool
Kids []uint
Level uint
Remote bool
Interns bool
Visa bool
NextID uint
PrevID uint
}
type story struct {
ID uint `json:"id"`
Title string
Text template.HTML
By string
Kids []uint
Comments []comment
FetchedAt int64
}
func getComments(id, level uint) ([]comment, error) {
commentJSON, err := getItem(id)
if err != nil {
return nil, fmt.Errorf("error getting comment %d JSON: %w", id, err)
}
var c comment
if err := json.Unmarshal(commentJSON, &c); err != nil {
return nil, fmt.Errorf("error unmarshaling comment %d JSON: %w", id, err)
}
if c.Dead || c.Deleted {
return nil, nil
}
lowText := strings.ToLower(string(c.Text))
if strings.Contains(lowText, "remote") {
c.Remote = true
}
if strings.Contains(lowText, "interns") {
c.Interns = true
}
if strings.Contains(lowText, "visa") {
c.Visa = true
}
c.Level = level
var comments = make([]comment, 0, len(c.Kids)+1)
comments = append(comments, c)
for _, kid := range c.Kids {
sc, err := getComments(kid, level+1)
if err != nil {
return nil, fmt.Errorf("error getting subcomments rooted at %d: %w", kid, err)
}
comments = append(comments, sc...)
}
return comments, nil
}
func getStory(id uint) (story, error) {
now := time.Now()
storyJSON, err := getItem(id)
if err != nil {
return story{}, fmt.Errorf("error getting story %d JSON: %w", id, err)
}
var s story
if err := json.Unmarshal(storyJSON, &s); err != nil {
return story{}, fmt.Errorf("error unmarshaling story %d JSON: %w", id, err)
}
for i, kid := range s.Kids {
if i%25 == 0 {
fmt.Printf("Getting top-level comment %d...\n", i+1)
}
cs, err := getComments(kid, 0)
if err != nil {
return story{}, fmt.Errorf("error getting story comments rooted at %d: %w", kid, err)
}
if len(cs) > 0 {
if i > 0 {
cs[0].PrevID = s.Kids[i-1]
}
if i < len(s.Kids)-1 {
cs[0].NextID = s.Kids[i+1]
}
}
s.Comments = append(s.Comments, cs...)
}
s.FetchedAt = now.Unix()
return s, nil
}