generated from fun-stack/example
-
Notifications
You must be signed in to change notification settings - Fork 4
/
scraper.go
330 lines (276 loc) · 7.96 KB
/
scraper.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package main
import (
"context"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/gocolly/colly/v2"
"github.com/pkg/errors"
"golang.org/x/exp/slog"
)
type rawStory struct {
ID string
row1
row2
}
type row1 struct {
Title string `selector:"span.titleline a"`
FullTitle string `selector:"span.titleline"`
URL string `selector:"span.titleline a" attr:"href"`
Rank string `selector:"span.rank"`
}
type row2 struct {
Author string `selector:"a.hnuser"`
Score string `selector:"span.score"`
SubmissionTime string `selector:"span.age" attr:"title"`
AgeApprox string `selector:"span.age"`
Links []string `selector:"a"`
}
type ScrapedStory struct {
Story
Rank int
Source string
}
func (rs rawStory) Clean() (ScrapedStory, error) {
story := ScrapedStory{
Story: Story{
Title: rs.Title,
By: rs.Author,
URL: rs.URL,
},
Source: "scraper",
}
// parse id
{
id, err := strconv.Atoi(rs.ID)
if err != nil {
return story, errors.Wrapf(err, "parse story id %s", rs.ID)
}
story.ID = id
}
// fix url
if strings.HasPrefix(story.Story.URL, "item?id=") {
story.Story.URL = "https://news.ycombinator.com/" + story.Story.URL
}
// parse score. This field will look like "4 points"
{
if fs := strings.Fields(rs.Score); len(fs) > 0 {
scoreStr := strings.Fields(rs.Score)[0]
score, err := strconv.Atoi(scoreStr)
story.Score = score
if err != nil {
return story, errors.Wrapf(err, "parse story score %s", rs.Score)
}
} else {
// if there is no upvotes field, then this is an HN job.
// we want to include these in the database because they get ranked
story.Job = true
}
}
// parse submission time
{
// submission times now contain a timestamp string, followed by a
// space then a unix timestamp with what looks like the *current*
// time which I suppose we can just ignore. For
// example "2024-10-23T16:44:01 1729713776"
parts := strings.Split(rs.SubmissionTime, " ")
var submissionTime time.Time
var err error
if strings.HasSuffix(parts[0], "Z") {
// Old format with "Z" indicating UTC
submissionTime, err = time.Parse("2006-01-02T15:04:05Z", parts[0])
} else {
// New format without "Z"
submissionTime, err = time.Parse("2006-01-02T15:04:05", parts[0])
}
if err != nil {
return story, errors.Wrapf(err, "parse submission time %s", rs.SubmissionTime)
}
story.SubmissionTime = submissionTime.Unix()
story.OriginalSubmissionTime = story.SubmissionTime
}
// parse approximate age
{
// this will be something like "1 minute ago" or "3 hours ago"
if fs := strings.Fields(rs.AgeApprox); len(fs) > 1 {
n, err := strconv.Atoi(fs[0])
if err != nil {
return story, errors.Wrapf(err, "parse relative age %s", rs.AgeApprox)
}
var units int64
if strings.HasPrefix(fs[1], "minute") { // "minute" or "minutes"
units = 60
} else if strings.HasPrefix(fs[1], "hour") {
units = 3600
} else if strings.HasPrefix(fs[1], "day") {
units = 3600 * 24
} else if strings.HasPrefix(fs[1], "month") {
units = 3600 * 24 * 30
} else if strings.HasPrefix(fs[1], "year") {
units = 3600 * 24 * 364
}
story.AgeApprox = int64(n) * units
} else {
return story, fmt.Errorf("Parse age %s", rs.AgeApprox)
}
// parse rank. we know the rank because of the order it appears in.
// we just use this to do an integrity check later.
{
tRank := strings.Trim(rs.Rank, ".")
var err error
story.Rank, err = strconv.Atoi(tRank)
if err != nil || story.Rank == 0 {
return story, errors.Wrapf(err, "parse rank %s", rs.Rank)
}
}
// parse the number of comments
{
// if there are comments, this will be the last <a> tag. Unfortunately, it doesn't have an id or class.
commentString := rs.Links[len(rs.Links)-1]
// this string will be a single word like "comment" or "hide" if there are no comments.
// otherwise it will be something like "12 comments"
if fs := strings.Fields(commentString); len(fs) > 1 {
c, err := strconv.Atoi(fs[0])
if err != nil {
return story, errors.Wrapf(err, "parse comments %s", commentString)
}
story.Comments = c
}
}
// parse [flagged] and [dupe] tags
{
if strings.Contains(rs.FullTitle, "[flagged]") {
story.Flagged = true
}
if strings.Contains(rs.FullTitle, "[dupe]") {
story.Dupe = true
}
}
return story, nil
}
}
func (app app) newScraper(resultCh chan ScrapedStory, errCh chan error, moreLinkCh chan string) *colly.Collector {
c := colly.NewCollector()
c.SetClient(app.httpClient)
var rs rawStory
c.OnHTML("a.morelink", func(e *colly.HTMLElement) {
moreLinkCh <- e.Attr("href")
})
c.OnHTML("tr table", func(e *colly.HTMLElement) {
n := 0
lastStoryRownum := 0
e.ForEach("tr", func(i int, e *colly.HTMLElement) {
class := e.Attr("class")
// stories will always start with a tr of class athing
if class == "athing" && n < 30 {
n = n + 1
lastStoryRownum = i
if n > 30 {
return
}
rs = rawStory{
ID: e.Attr("id"),
}
err := e.Unmarshal(&rs.row1)
if err != nil {
errCh <- err
}
} else if class == "" && i == lastStoryRownum+1 && n > 0 && n <= 30 {
// the first tr after the "athing" contains the second row of
// details for the story. Note also we must skip any trs
// before the first athing because sometimes they contain
// general page content.
err := e.Unmarshal(&rs.row2)
if err != nil {
errCh <- err
} else {
st, err := rs.Clean()
rank := st.Rank
// Do an integrity check. If the row shown for the story equals the row
// count we are keeping, we area all good.
if err == nil && ((rank-1)%30)+1 != n {
err = fmt.Errorf("Ranks out of order. Expected %d but parsed %d", n, (rank-1)%30+1)
}
if err != nil {
Debugf(app.logger, "Failed to parse story %d. Raw story %#v", n, rs)
errCh <- err
} else {
resultCh <- st
}
}
}
})
})
c.OnError(func(r *colly.Response, err error) {
err = errors.Wrapf(err, "Failed to parse page %s", r.Request.URL)
errCh <- err
})
return c
}
func (app app) scrapeHN(pageType string, resultCh chan ScrapedStory, errCh chan error) {
baseUrl := "https://news.ycombinator.com/"
url := baseUrl
if pageType == "new" {
url = url + "newest"
} else if pageType != "top" {
url = url + pageType
}
for p := 1; p <= 3; p++ {
moreLinkCh := make(chan string, 1)
c := app.newScraper(resultCh, errCh, moreLinkCh)
err := c.Visit(url)
if err != nil {
errCh <- err
}
select {
case relativeURL := <-moreLinkCh:
url = baseUrl + relativeURL
default:
// there won't always be a next link, in particular the show page could have less than 3 pages worth of stories
}
}
close(resultCh)
close(errCh)
}
func (app app) scrapeFrontPageStories(ctx context.Context) (map[int]ScrapedStory, error) {
app.logger.Info("Scraping front page stories")
stories := map[int]ScrapedStory{}
pageTypeName := "top"
nSuccess := 0
resultCh := make(chan ScrapedStory)
errCh := make(chan error)
var wg sync.WaitGroup
t := time.Now()
// scrape in a goroutine. the scraper will write results to the channel
// we provide
wg.Add(1)
go func() {
defer wg.Done()
app.scrapeHN(pageTypeName, resultCh, errCh)
}()
// read from the error channel in print errors in a separate goroutine.
// The scraper will block writing to the error channel if nothing is reading
// from it.
wg.Add(1)
go func() {
defer wg.Done()
for err := range errCh {
app.logger.Error("Error parsing story", err)
crawlErrorsTotal.Inc()
}
}()
for story := range resultCh {
id := story.ID
stories[id] = story
nSuccess += 1
}
if nSuccess == 0 {
return stories, fmt.Errorf("Didn't successfully parse any stories from %s page", pageTypeName)
}
Debugf(app.logger, "Crawled %d stories on %s page", nSuccess, pageTypeName)
wg.Wait()
app.logger.Info("Scraped stories", "pageTypeName", pageTypeName, slog.Duration("elapsed", time.Since(t)))
return stories, nil
}