-
Notifications
You must be signed in to change notification settings - Fork 4
/
fetch.js
executable file
·74 lines (63 loc) · 2.33 KB
/
fetch.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
#!/usr/bin/env node
const { program, InvalidArgumentError } = require('commander');
/**
*
* @param {string} value value entered
* @returns {number}
*/
function optionIsIntGreaterThanZero(value) {
const num = parseInt(value);
if (isNaN(num)) {
throw new InvalidArgumentError('Not a number');
}
if (num <= 0) {
throw new InvalidArgumentError('Must be higher than 0');
}
return num;
}
/**
*
* @param {string} value value entered
* @returns {0|1|2|3}
*/
function optionIsGameMode(value) {
const num = parseInt(value);
if (isNaN(num)) {
throw new InvalidArgumentError('Not a number');
}
if (num < 0 || num > 3) {
throw new InvalidArgumentError('Must be either 0, 1, 2, or 3');
}
return num;
}
program
.requiredOption('-k, --api-key <k>', 'osu api key')
.option('-m, --mode <m>', 'the mode the score was played in', optionIsGameMode, 0)
.requiredOption('-b, --beatmap-id <b>', 'the beatmap ID (not beatmap set ID!) in which the replay was played', optionIsIntGreaterThanZero)
.option('-h, --beatmap-hash <md5-hash>', 'the beatmap MD5-hash of the beatmap. When provided it saves an api call. If not provided it fetches it from the api.')
.requiredOption('-u, --user-id <u>', 'the user that has played the beatmap', optionIsIntGreaterThanZero)
.option('--mods <mods-bitmask>', 'bitmask of the used mods. If not specified, a random score will be picked (I think the one with highest pp), so it does NOT default to nomod!', optionIsIntGreaterThanZero)
.option('-o, --output-file <file>', 'the osr-file to save (if not specified it uses stdout)')
.parse();
const fs = require('fs');
const downloader = require('./downloader');
const WritestreamWrapper = require('./WritestreamWrapper');
const opts = program.opts();
async function run() {
const data = await downloader.download(opts.apiKey, opts.userId, opts.beatmapId, opts.beatmapHash, opts.mode, opts.mods);
// create output stream
let out;
if (opts.outputFile) {
out = new WritestreamWrapper(fs.createWriteStream(opts.outputFile), true);
} else {
out = new WritestreamWrapper(process.stdout, false);
}
// write the data
out.writeOsrData(data);
// close the stream
out.end();
}
run().catch(err => {
console.error(err);
process.exitCode = 1;
});