-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscraper.js
More file actions
51 lines (48 loc) · 1.3 KB
/
scraper.js
File metadata and controls
51 lines (48 loc) · 1.3 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import {
normalizeJobBoard,
diffJobBoards,
boardId,
sortDiffItemByTimestamp,
} from './jobboard'
export const scrapeHandler = async () => {
const now = new Date().toISOString()
const currentKey = boardId + ':' + now
const raw = await fetchJobBoard(boardId)
const after = normalizeJobBoard(raw)
const stateKey = boardId + ':~'
let state = await KV.get(stateKey)
if (state === null) {
state = {}
} else {
state = JSON.parse(state)
}
const snapshotKey = state.snapshot
let before
if (snapshotKey) {
const snapshot = await KV.get(snapshotKey)
before = normalizeJobBoard(snapshot)
} else {
before = []
}
const diff = diffJobBoards(before, after, now)
if (diff.length) {
state.history = state.history ? state.history.concat(diff) : [] // ignore first diff
state.history.sort(sortDiffItemByTimestamp)
state.snapshot = currentKey
await KV.put(currentKey, raw)
}
state.last = now
await KV.put(stateKey, JSON.stringify(state))
return new Response(JSON.stringify(diff), {
headers: { 'content-type': 'text/plain' },
})
}
async function fetchJobBoard(boardId) {
const res = await fetch(
`https://boards-api.greenhouse.io/v1/boards/${boardId}/jobs`,
{
headers: { 'content-type': 'application/json' },
},
)
return await res.text()
}