-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprocessHTML.js
More file actions
37 lines (29 loc) · 1.25 KB
/
Copy pathprocessHTML.js
File metadata and controls
37 lines (29 loc) · 1.25 KB
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
const cheerio = require('cheerio')
const processHTML = (html, filterValues, maxDaysBack) => {
const $ = cheerio.load(html)
const data = []
let nextPage = 'https://news.ycombinator.com/' + $('.morelink').attr('href')
//.each to loop over all class athing elements
$('.athing').each((index, element) => {
const title = $(element).find('.titleline > a').text()
const link = $(element).find('.titleline > a').attr('href')
// .age carries the exact post time in its title attribute,
// e.g. title="2026-07-31T09:12:04 1754... "
const ageTitle = $(element).next().find('.age').attr('title')
const postedISO = ageTitle ? ageTitle.split(' ')[0] : null
const titleLower = title.toLowerCase()
const hasFilterItem = filterValues.some((item) => titleLower.includes(item))
const daysAgo = postedISO
? (Date.now() - new Date(postedISO).getTime()) / (1000 * 60 * 60 * 24)
: Infinity
// rows are in reverse-chronological order, so the first row past the
// cutoff means every later page is older too. Stop paginating there.
if (daysAgo <= maxDaysBack) {
data.push({ link, title, postedISO, hasFilterItem })
} else {
nextPage = null
}
})
return { data, nextPage }
}
module.exports = processHTML