-
Notifications
You must be signed in to change notification settings - Fork 30
/
combine.js
84 lines (66 loc) · 1.6 KB
/
combine.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const getJSON = require('get-json')
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const figlet = require('figlet')
const outputFile = process.argv.slice(2)[0]
const dataDir = 'data/'
console.log(chalk.green(figlet.textSync('Bitcoin Chart Scraper', {
font: 'Pepper',
kerning: 'fitted'
})))
console.log(chalk.dim(`Combining data to=${chalk.yellow(outputFile)} \n`))
const files = []
function compare (a, b) {
if (a.time < b.time) {
return -1
}
if (a.time > b.time) {
return 1
}
return 0
}
fs.readdir(dataDir, (err, list) => {
list.forEach(file => {
let date
let time
try {
date = new Date(file.split('bitstampUSD-')[1].split('.json')[0])
time = date.getTime()
} catch (err) {
return
}
if (!date) {
return
}
files.push({
file,
date,
time
})
})
files.sort(compare)
let combinedData = []
process.stdout.write('\n')
files.forEach(day => {
const filePath = path.join(dataDir, day.file)
const contents = fs.readFileSync(path.join(__dirname, filePath)).toString()
const data = JSON.parse(contents)
if (data.length > 0) {
// const prettyDate = day.date.toString().split(':')[0].substr(0, 15)
// const usd = '$' + data[0][7]
// console.log(chalk.yellow(prettyDate), chalk.red(usd))
combinedData = combinedData.concat(data)
}
process.stdout.write('.')
})
const outputData = JSON.stringify(combinedData);
fs.writeFile(outputFile, outputData, 'utf8', err => {
process.stdout.write('\n')
if(err) {
return console.error(chalk.red(err))
} else {
console.log(`Saved to: ${chalk.blue(outputFile)}`)
}
})
})