forked from henryjameslau/automatic-cpi-weights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
50 lines (38 loc) · 1.16 KB
/
run.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
import data from './inflation_data.js'
import fetch from 'node-fetch';
import { createArrayCsvWriter } from 'csv-writer'
import * as fs from 'fs'
let date_ob = new Date().toDateString()
fs.writeFile('./timestamp.txt', date_ob, err => {
if (err) {
console.error(err);
}
// file written successfully
});
const weights = []
for (let i = 0; i < data.length; i++) {
console.log('getting inflation weight ' + data[i].weight_cdid)
var url = "https://www.ons.gov.uk/economy/inflationandpriceindices/timeseries/" + data[i].weight_cdid + "/data"
const response = await fetch(url)
const ons = await response.json()
weights.push(ons)
if (i == data.length - 1) {
processWeights(weights)
}
}
function processWeights(everything) {
const headers = ['description', 'year', 'value']
const rows = []
everything.forEach(
d => d.years.forEach(
e => rows.push([d.description.title, e.year, e.value])
)
)
const csvWriter = createArrayCsvWriter({
header: headers,
path: './data.csv'
});
csvWriter.writeRecords(rows).then(() => {
console.log('written csv')
})
}