Skip to content

Commit

Permalink
fetch-btp script: add --backup and --tournament-name options
Browse files Browse the repository at this point in the history
  • Loading branch information
phihag committed Jun 23, 2019
1 parent 25a31c5 commit a1c175d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,3 +1,4 @@
backups
data
ticker_data

Expand All @@ -7,4 +8,3 @@ config.json
ticker_config.json

mail.*

3 changes: 2 additions & 1 deletion TODO
@@ -1,6 +1,5 @@
script to download (raw / json / ...)
convert from formats
add --backup option
add --umpires
add --match-id

Expand Down Expand Up @@ -81,6 +80,8 @@ prevent setting a match to 0:0 (and log in console or database!) if match is alr

display next matches

autosync tournament name from BTP

check if switching to other tab and back works

ticker: allow overriding all from different tournaments (clear if != tournament)
Expand Down
1 change: 1 addition & 0 deletions bts/utils.js
Expand Up @@ -118,6 +118,7 @@ function make_index(ar, index_func) {

function pad(n, width, z) {
z = z || '0';
width = width || 2;
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
Expand Down
66 changes: 52 additions & 14 deletions fetch-btp.js
Expand Up @@ -4,11 +4,20 @@
const argparse = require('argparse');
const {promisify} = require('util');
const {DOMParser} = require('xmldom');
const fs = require('fs');
const path = require('path');

const btp_proto = require('./bts/btp_proto.js');
const btp_conn = require('./bts/btp_conn.js');
const utils = require('./bts/utils.js');
const {serialize_pretty} = require('./bts/xml_utils.js');

async function _ensure_dir(path) {
if (await promisify(fs.exists)(path)) return;

await promisify(fs.mkdir)(path);
}

async function main() {
const send_raw_request = promisify(btp_conn.send_raw_request);

Expand All @@ -25,6 +34,11 @@ async function main() {
metavar: 'PASSWORD',
help: 'The TPNetwork password. Empty for no password.',
});
parser.addArgument(['-b', '--backup'], {
action: 'storeTrue',
help: 'Create a backup file in ./backups/',
});

const output_group = parser.addArgumentGroup({title: 'Output'});
output_group.addArgument(['-r', '--raw'], {
action: 'storeConst',
Expand Down Expand Up @@ -61,6 +75,13 @@ async function main() {
defaultValue: 'json',
help: 'Do not output received data (used for debugging)',
});
output_group.addArgument(['--tournament-name'], {
action: 'storeConst',
dest: 'output',
constant: 'tournament_name',
defaultValue: 'json',
help: 'Output tournament name',
});
const args = parser.parseArgs();
const ip = args.IP;
if (!ip) {
Expand All @@ -73,31 +94,48 @@ async function main() {
const xml_request = btp_proto.get_info_request(args.password);
const raw_request = btp_proto.encode(xml_request);

const raw_response = await send_raw_request(ip, port, raw_request);
if (args.output === 'none') {
return;
}
const response_raw = await send_raw_request(ip, port, raw_request);
if (args.output === 'raw') {
process.stdout.write(raw_response);
return;
process.stdout.write(response_raw);
}
const response_str = await promisify(btp_proto.decode_string)(raw_response);

const response_str = await promisify(btp_proto.decode_string)(response_raw);
if (args.output === 'str') {
process.stdout.write(response_str);
return;
}

const xml_parser = new DOMParser();
const doc = xml_parser.parseFromString(response_str);
const response_doc = xml_parser.parseFromString(response_str);
if (args.output === 'xml') {
console.log(serialize_pretty(doc));
return;
console.log(serialize_pretty(response_doc));
}

const response = btp_proto.el2obj(doc.documentElement);
const response_obj = btp_proto.el2obj(response_doc.documentElement);
if (args.output === 'json') {
console.log(JSON.stringify(response, null, 2));
return;
console.log(JSON.stringify(response_obj, null, 2));
}

const tournament_name = (
response_obj
.Result[0].Tournament[0].Settings[0].Setting
.filter(setting => setting.ID[0] == 1001)[0]
.Value[0]);
if (args.output === 'tournament_name') {
console.log(tournament_name);
}

if (args.backup) {
const BACKUPS_DIR = './backups';
await _ensure_dir(BACKUPS_DIR);
const clean_tournament_name = tournament_name.replace(/[^-_\u00C0-\u017Fa-zA-Z0-9]/g, '_');
const now = new Date();
const timestamp = (
now.getFullYear() + utils.pad(now.getMonth() + 1) + utils.pad(now.getDate()) +
'_' +
utils.pad(now.getHours()) + utils.pad(now.getMinutes()) + utils.pad(now.getSeconds())
);
const fn = path.join(BACKUPS_DIR, `btp-${clean_tournament_name}_${timestamp}.raw`);
await promisify(fs.writeFile)(fn, response_raw);
}
}

Expand Down

0 comments on commit a1c175d

Please sign in to comment.