-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·158 lines (136 loc) · 3.74 KB
/
cli.js
File metadata and controls
executable file
·158 lines (136 loc) · 3.74 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env node
import { faceToSvgString } from "facesjs";
import sharp from "sharp";
import Pick from "stream-json/filters/Pick.js";
import StreamArray from "stream-json/streamers/StreamArray.js";
import { unusedFilename } from "unused-filename";
import * as fs from "node:fs";
import * as path from "node:path";
import { pipeline } from "node:stream/promises";
import { parseArgs } from "node:util";
const options = {
league: {
type: "string",
},
folder: {
type: "string",
default: "exported-faces",
},
season: {
type: "string",
default: "current",
},
};
const parsedArgs = parseArgs({ options });
const inputFilename = parsedArgs.values.league;
const exportFolder = await unusedFilename(parsedArgs.values.folder);
let season = parseInt(parsedArgs.values.season);
if (Number.isNaN(season)) {
season = "current";
}
if (!fs.existsSync(exportFolder)){
fs.mkdirSync(exportFolder);
}
const teams = [];
const processPlayer = async (p) => {
let tid;
if (season === "current") {
if (p.tid < -2) {
return;
}
tid = p.tid;
} else {
const stats = p.stats.findLast(row => row.season === season);
if (!stats) {
return;
}
tid = stats.tid;
}
if (p.imgURL) {
// Ignore imgURL ones for now - could download, but I don't want people to accidentally use this and DDOS someone
return;
}
let t;
if (teams[tid]) {
t = teams[tid];
} else if (tid === -1) {
t = {
abbrev: "FA",
colors: ["#000", "#00F", "#F00"],
jersey: "jersey3",
};
} else if (tid === -2) {
t = {
abbrev: `DP${p.draft.year}`,
colors: ["#000", "#00F", "#F00"],
jersey: "jersey3",
};
} else {
throw new Error(`Invalid tid ${tid}`)
}
const svg = faceToSvgString(p.face, {
teamColors: t.colors,
jersey: {
id: t.jersey,
},
});
const outputFilename = `${t.abbrev} - ${p.lastName}, ${p.firstName}.png`;
await sharp(Buffer.from(svg))
.png()
.toFile(path.join(exportFolder, outputFilename));
}
const isGzip = inputFilename.endsWith(".gz");
console.log(`Loading ${season} team data...`);
const teamStreams = [
Pick.withParser({ filter: "teams" }),
StreamArray.streamArray(),
async function* (rows) {
for await (const row of rows) {
const t = row.value;
let found = false;
if (season !== "current") {
const ts = t.seasons.findLast(row => row.season === season);
if (ts) {
found = true;
teams[t.tid] = {
abbrev: ts.abbrev,
colors: ts.colors,
jersey: ts.jersey,
};
}
}
if (!found) {
teams[t.tid] = {
abbrev: t.abbrev,
colors: t.colors,
jersey: t.jersey,
};
}
}
},
];
if (isGzip) {
teamStreams.unshift(new DecompressionStream("gzip"));
}
await pipeline(
fs.createReadStream(inputFilename),
...teamStreams,
);
console.log(`Exporting images to "${exportFolder}"...`);
const playerStreams = [
Pick.withParser({ filter: "players" }),
StreamArray.streamArray(),
async function* (rows) {
for await (const row of rows) {
yield await processPlayer(row.value);
}
},
];
if (isGzip) {
playerStreams.unshift(new DecompressionStream("gzip"));
}
await pipeline(
fs.createReadStream(inputFilename),
...playerStreams,
);
console.log("Done!");