File size: 1,378 Bytes
23b39f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fs from 'node:fs/promises';
import * as zlib from "zlib";
import readline from 'node:readline';

let currentTime = 1593561599000;

await fs.appendFile("test.csv", "timestamp,bid\n");

for await (const dayFile of fs.glob("./binance-btcusdt-futures-2020-2021/*.csv.gz")) {
  console.log("Reading", dayFile, "...");

  let outputRows = [];

  const fd = await fs.open(dayFile);

  let lineReader = readline.createInterface({
    input: fd.createReadStream().pipe(zlib.createGunzip())
  });

  let n = 0;
  for await (const line of lineReader) {
    if (n > 0) {
      let lineParts = line.split(',');
      let timestamp = parseInt(lineParts[1]);
      if (timestamp >= currentTime + 1000) {
        currentTime = Math.floor(timestamp / 1000) * 1000;
        outputRows.push([unixTime(currentTime), lineParts[3]]);
      }
    }
    n++;
  }

  console.log("Done");

  let output = "";

  outputRows.forEach((row) => {
    output += row.join(",") + "\n";
  });

  await fs.appendFile("test.csv", output);

  lineReader.close();
}


function unixTime(unixtime) {

  var u = new Date(unixtime);

  return u.getUTCFullYear() +
      '-' + ('0' + (u.getUTCMonth() + 1)).slice(-2) +
      '-' + ('0' + u.getUTCDate()).slice(-2) +
      ' ' + ('0' + u.getUTCHours()).slice(-2) +
      ':' + ('0' + u.getUTCMinutes()).slice(-2) +
      ':' + ('0' + u.getUTCSeconds()).slice(-2);
}