-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
63 lines (48 loc) · 1.35 KB
/
index.js
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
const low = require('lowdb')
const puppeteer = require('puppeteer')
const FileSync = require('lowdb/adapters/FileSync')
const schedule = require('node-schedule')
const adapter = new FileSync('db.json')
const db = low(adapter)
db.defaults({ waits: [] }).write()
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
const isEmpty = v => v === '0:00'
const namesById = {
599: 'Daly City',
503: 'San Francisco',
}
const main = async (id, isRetrying) => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto(`https://www.dmv.ca.gov/portal/dmv/detail/fo/offices/fieldoffice?number=${id}`)
const waits = await page.evaluate(() =>
[...document.getElementById('WaitTimesData').children].map(o => o.children[1].innerText),
)
await page.close()
await browser.close()
if (!waits[0] || !waits[1]) {
if (!isRetrying) {
await sleep(500)
main(id, true)
}
return
}
if (isEmpty(waits[0]) && isEmpty(waits[1])) {
const last = db
.get('waits')
.takeRight(1)
.value()[0]
if (isEmpty(last.withApt) && isEmpty(last.withoutApt)) {
return
}
}
const time = Date.now()
db
.get('waits')
.push({ time, id, withApt: waits[0], withoutApt: waits[1] })
.write()
}
schedule.scheduleJob('*/5 * * * *', () => {
main(599)
main(503)
})