-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.js
60 lines (56 loc) · 3.33 KB
/
convert.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
var fs = require('fs');
var path = require('path');
var async = require('async');
var colors = require('colors/safe');
var param2 = process.argv[2];
var param3 = process.argv[3];
const season = param2;
const game = param3;
const dataFolder = `./data/${season}/`;
// Additional parameter indicates individual game to convert (parse) from json to csv
if (process.argv[3] != null) {
var exec = require('child_process').exec;
var cmd = `jq -r --arg season ${season} --arg gameid ${game} '.data.game as $p | .data.game.plays.play[] | . as $c | [$season | tonumber, $gameid | tonumber] +
[([$c.aoi[] | tostring] | join(","))] + [$p.awayteamid, $p.awayteamname, $p.awayteamnick, $p.hometeamid, $p.hometeamname, $p.hometeamnick] +
[$c.as, $c.asog, $c.desc, $c.eventid, $c.formalEventId] + [([$c.hoi[] | tostring] | join(","))] + [$c.hs, $c.hsog, $c.localtime, $c.p1name, $c.p2name,
$c.p3name, $c.period, $c.pid, $c.pid1, $c.pid2, $c.pid3, $c.playername, $c.strength, $c.sweater, $c.teamid, $c.time, $c.type, $c.xcoord, $c.ycoord] |
@csv' ./data/${season}/${game}.json > ./data/${season}/${game}.csv`;
exec(cmd, function (error, stdout, stderr) {
if (error) {
console.log(colors.red(`exec error: ${error}`));
return;
} else {
console.log(colors.green(`Converted ${game}.json to ${game}.csv successfully`));
}
})
} else {
fs.readdir(dataFolder, (err, files) => {
if (err) {
console.log(colors.red(`exec error: ${err}`));
} else {
files = files.filter(item => !(/^(.*\.(?!(json)$))?[^.]*$/i).test(item)); // Read only *.json files
files = files.filter(item => !(/^.*schedule.*$/i).test(item)); // Remove, filter out *schedule* file
async.eachSeries(files, (file, done) => {
//console.log(file)
var game = path.parse(file).name;
var exec = require('child_process').exec;
var cmd = `jq -r --arg season ${season} --arg gameid ${game} '.data.game as $p | .data.game.plays.play[] | . as $c | [$season | tonumber, $gameid | tonumber] +
[([$c.aoi[] | tostring] | join(","))] + [$p.awayteamid, $p.awayteamname, $p.awayteamnick, $p.hometeamid, $p.hometeamname, $p.hometeamnick] +
[$c.as, $c.asog, $c.desc, $c.eventid, $c.formalEventId] + [([$c.hoi[] | tostring] | join(","))] + [$c.hs, $c.hsog, $c.localtime, $c.p1name, $c.p2name,
$c.p3name, $c.period, $c.pid, $c.pid1, $c.pid2, $c.pid3, $c.playername, $c.strength, $c.sweater, $c.teamid, $c.time, $c.type, $c.xcoord, $c.ycoord] |
@csv' ./data/${season}/${game}.json > ./data/${season}/${game}.csv`;
exec(cmd, function (error, stdout, stderr) {
if (error) {
console.log(colors.red(`exec error: ${error}`));
} else {
console.log(colors.green(`Converted ${game}.json to ${game}.csv successfully`));
}
// Must call the done callback to signify this iteration os done
done(error);
})
}, () => {
//console.log('Now the whole collection is done!');
})
}
})
}