-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchSeaLevels.js
59 lines (50 loc) · 1.38 KB
/
fetchSeaLevels.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
const cheerio = require('cheerio')
const fetch = require('node-fetch')
const fs = require('fs')
const SEA_LEVEL_URL = 'https://climate.nasa.gov/vital-signs/sea-level/'
async function pullHtml() {
return new Promise(async (resolve, reject) => {
fetch(SEA_LEVEL_URL)
.then((res) => res.text())
.then(resolve)
.catch(reject)
})
}
function parse(html) {
const $ = cheerio.load(html)
const latestMeasurement = $('.latest_measurement .value')
.contents()
.get(0)
.nodeValue.trim()
const uncertainty = $('.latest_measurement .value .error')
.contents()
.get(0)
.nodeValue.trim()
.match(/([0-9.])+/g)[0]
const script = $('.graph_container').prev().html()
const chartExtract = script
.match(/var data = charts\.createData.*/gi)[0]
.replace('var data = charts.createData("SeaLevel",', '')
.replace(', "mm");', '')
const chartData = JSON.parse(chartExtract).map((o) => {
const { month, day, year, x, y } = o
return {
month: +month,
day: +day,
year: +year,
year_fraction: +x,
level: +y,
min: +y - +uncertainty,
max: +y + +uncertainty,
}
})
return { unit: 'mm', levels: chartData }
}
; (async () => {
const html = await pullHtml()
const json = parse(html)
fs.writeFileSync(
`${process.env.LOCATION || './src/data/'}sea_levels.json`,
JSON.stringify(json)
)
})()